{"cells":[{"cell_type":"markdown","metadata":{"id":"7r0vUW1e9RoB"},"source":["## This Script downloads the current version of Infinity-For-Reddit and compiles it to an APK.\n","\n","Follow the instructions (if any) and then just run the code snippets one after the other. \\\n","You don't have to touch the code at all. \\\n","(When I say API token or API key, I mean the same thing.)\n","\n","**Please consider donating to the author Docile Alligator [here](https://patreon.com/docile_alligator)!**\n","\n","Thanks to all contributors (u/hostilenemy, u/Oha_der_erste, u/StudyGuidex, u/aman207) keeping this Notebook alive!\n","\n","FAQ at the end of this Notebook."]},{"cell_type":"code","execution_count":null,"metadata":{"cellView":"form","collapsed":true,"id":"BdsYmWoe-O5e"},"outputs":[],"source":["#@title 1. Setup the environment\n","#@markdown 1. Create the API application [here](https://old.reddit.com/prefs/apps/) with the following settings: \\\n","#@markdown - name: **`{YourRedditUsername}s-app`** (Replace {YourRedditUsername} with your actual username) \\\n","#@markdown - type: **`Installed app`** \\\n","#@markdown - redirect uri: **`http://127.0.0.1`** \\\n","#@markdown Example: \\\n","#@markdown ![](https://i.ibb.co/HVGrc8M/grafik.png) \\\n","#@markdown The API token is then shown at the bottom of the page under the App name  (\"{YourRedditUsername}s-app\") and is a set of random characters (e.g. 6g5ZHEGEAnKHP3vewUFY3y) \\\n","#@markdown Input the **API token** and **your username** below and go to 2. \\\n","api_token = 'your-api' #@param {type:\"string\"}\n","your_reddit_username = 'your-name' #@param {type:\"string\"}\n","user_agent = f\"android:personal-app:0.0.1 (by /u/{your_reddit_username})\"\n","redirect_uri = 'http://127.0.0.1'\n","\n","\n","#@markdown \\\n","#@markdown 2. Choose where to download the source code from (Standard: GitHub Repository) \\\n","#@markdown If GitHub Repository fails, run Code block 4. and choose [GitHub Archive]\n","version = '[GitHub Repository]' #@param [\"[GitHub Repository]\", \"[GitHub Archive]\"]\n","\n","#@markdown \\\n","#@markdown 3. Run the snippet on the top left. \\\n","#@markdown This will set up the environment to compile the .apk from. \\\n","#@markdown This can take up to 3 minutes.\n","\n","import os, sys, re\n","\n","if not api_token or not your_reddit_username:\n","  print(\"\")\n","  print(\"\\x1b[31m[IMPORTANT]\")\n","  print(\"No settings have been set. Please input your token and username.\\x1b[0m\")\n","  sys.exit()\n","else:\n","  print(\"Following settings have been set:\")\n","  print(\"- User-Agent:\", user_agent)\n","  print(\"- API token:\", api_token)\n","  print(\"- Source location:\", version)\n","\n","\n","\n","#2. Update the VM, install JDK 11, Android SDK and setup sdkmanager (Just click run, don't make any cli inputs)\n","!apt-get --quiet update\n","!apt-get --quiet install openjdk-17-jdk\n","!wget --quiet --output-document=android-sdk.zip https://dl.google.com/android/repository/commandlinetools-linux-7583922_latest.zip\n","!unzip -q android-sdk.zip -d android-sdk\n","\n","os.environ[\"ANDROID_SDK_ROOT\"] = \"/content/android-sdk\"\n","os.environ[\"JAVA_HOME\"] = \"/usr/lib/jvm/java-17-openjdk-amd64\"\n","os.environ[\"PATH\"] += \":/content/android-sdk/tools/bin:/content/android-sdk/platform-tools\"\n","\n","# setup sdkmanager\n","!yes | /content/android-sdk/cmdline-tools/bin/sdkmanager  --sdk_root=$ANDROID_SDK_ROOT \"platforms;android-30\" \"build-tools;30.0.3\"\n","\n","\n","#3. Download the source code\n","if version == \"[GitHub Repository]\":\n","  !git clone \"https://github.com/Docile-Alligator/Infinity-For-Reddit\"\n","else:\n","  !wget \"https://github.com/Docile-Alligator/Infinity-For-Reddit/archive/a46e96f3e48a89f7b683fa7308c39f01c5b5ac21.zip\"\n","  !unzip \"a46e96f3e48a89f7b683fa7308c39f01c5b5ac21.zip\"\n","  !mv -T Infinity-For-Reddit-* Infinity-For-Reddit\n","%cd Infinity-For-Reddit/\n","\n","\n","\n","\n","#4. Change the API token, user agent and Redirect URI to yours and add keystore (Just click run)\n","# Change API\n","apiutils_file = \"/content/Infinity-For-Reddit/app/src/main/java/ml/docilealligator/infinityforreddit/utils/APIUtils.java\"\n","\n","apiutils_code = open(apiutils_file, \"r\", encoding=\"utf-8-sig\").read()\n","apiutils_code = re.sub(r'public static final String CLIENT_ID = \".*?\";', f'public static final String CLIENT_ID = \"{api_token}\";', apiutils_code)\n","apiutils_code = apiutils_code.replace(\"infinity://localhost\", redirect_uri)\n","apiutils_code = re.sub(r'public static final String USER_AGENT = \".*?\";', f'public static final String USER_AGENT = \"{user_agent}\";', apiutils_code)\n","\n","with open(apiutils_file, \"w\", encoding=\"utf-8\") as f:\n","  f.write(apiutils_code)\n","\n","\n","# Add Keystore\n","# Is this written any good? No? I know\n","!wget -P /content/ \"https://github.com/TanukiAI/Infinity-keystore/raw/main/Infinity.jks\"\n","\n","build_gradle_file = \"/content/Infinity-For-Reddit/app/build.gradle\"\n","build_gradle_code = open(build_gradle_file, \"r\", encoding=\"utf-8-sig\").read()\n","build_gradle_code = build_gradle_code.replace(r\"\"\"    buildTypes {\"\"\", r\"\"\"    signingConfigs {\n","        release {\n","            storeFile file(\"/content/Infinity.jks\")\n","            storePassword \"Infinity\"\n","            keyAlias \"Infinity\"\n","            keyPassword \"Infinity\"\n","        }\n","    }\n","    buildTypes {\"\"\")\n","build_gradle_code = build_gradle_code.replace(r\"\"\"    buildTypes {\n","        release {\"\"\", r\"\"\"    buildTypes {\n","        release {\n","            signingConfig signingConfigs.release\"\"\")\n","build_gradle_code = build_gradle_code.replace(r\"\"\"    lint {\n","        disable 'MissingTranslation'\n","    }\"\"\", r\"\"\"    lint {\n","        disable 'MissingTranslation'\n","        baseline = file(\"lint-baseline.xml\")\n","    }\"\"\")\n","\n","with open(build_gradle_file, \"w\", encoding=\"utf-8\") as f:\n","  f.write(build_gradle_code)"]},{"cell_type":"code","execution_count":null,"metadata":{"cellView":"form","colab":{"background_save":true},"collapsed":true,"id":"ViYXGNh0ytup"},"outputs":[],"source":["#@title 2. Build the APK. This can take up to 15 minutes. (Just click run)\n","if not api_token or not your_reddit_username:\n","  print(\"\")\n","  print(\"\\x1b[31m[IMPORTANT]\")\n","  print(\"No settings have been set. Please input your token and username in Step 1.\\x1b[0m\")\n","  sys.exit()\n","!./gradlew updateLintBaseline\n","!./gradlew assembleRelease"]},{"cell_type":"code","execution_count":null,"metadata":{"id":"7TZZ2mPY5ykJ"},"outputs":[],"source":[]},{"cell_type":"code","execution_count":null,"metadata":{"cellView":"form","id":"xoxhFg8K5hZ0"},"outputs":[],"source":["#@title 3. Download the APK directly or via Catbox.moe or file.io\n","#@markdown If you do this on a **phone**, choose **[.apk Direct Download]** for direct .apk download. \\\n","#@markdown If you do this on a **computer**, choose **[Upload to file.io ...]** and it will upload it to file.io and show a QR code which will link you to the download page. \\\n","#@markdown \\\n","#@markdown Of course you can do [.apk Direct Download] on your computer and transfer the .apk file manually to your phone.\n","dl = '[.apk Direct Download]' #@param [\"[.apk Direct Download]\", \"[Upload to Catbox.moe and create and show a QR code]\", \"[Upload to file.io and create and show QR code]\"]\n","\n","import os\n","\n","if not os.path.exists(\"/content/Infinity.apk\"):\n","  !mv /content/Infinity-For-Reddit/app/build/outputs/apk/release/app*.apk /content/Infinity.apk\n","\n","\n","if dl == \"[.apk Direct Download]\":\n","  from google.colab import files\n","  files.download('/content/Infinity.apk')\n","else:\n","  !pip install requests pyqrcode pypng\n","  from IPython.display import clear_output, Image, display\n","  import pyqrcode\n","  import requests\n","\n","  clear_output()\n","  dl_url = None\n","\n","  # Set Upload URL\n","  if \"Catbox\" in dl:\n","    url = \"https://litterbox.catbox.moe/resources/internals/api.php\"\n","    files = {\n","      'fileToUpload': ('Infinity.apk', open('/content/Infinity.apk', 'rb')),\n","      'time': (None, '12h'),\n","      'reqtype': (None, 'fileupload')\n","    }\n","    dl_url = requests.post(url, files=files).text\n","  else:\n","    url = 'https://file.io'\n","\n","    # Upload\n","    with open(\"/content/Infinity.apk\", 'rb') as file:\n","      files = {'file': file}\n","      response = requests.post(url, files=files).json()\n","\n","    ## Check if upload failed\n","    if response[\"success\"]:\n","      dl_url = response[\"link\"]\n","\n","  if dl_url is None:\n","    print(\"Error uploading. Use another download method.\")\n","\n","  else:\n","    # Generate and display QR code\n","    qr = pyqrcode.create(dl_url)\n","    img = \"myqr.png\"\n","    qr.png(img, scale=10)\n","    Image(img)\n","    print(\"Download URL:\", dl_url)\n","    display(Image(img))\n"]},{"cell_type":"code","execution_count":null,"metadata":{"cellView":"form","id":"BNM9Hdc_iHNX"},"outputs":[],"source":["#@title 4. Delete the current Google Colab VM (after you have downloaded the .apk, THIS STEP IS INTENDED TO FAIL!)\n","from google.colab import runtime\n","runtime.unassign()"]}],"metadata":{"colab":{"provenance":[]},"kernelspec":{"display_name":"Python 3","name":"python3"},"language_info":{"name":"python"}},"nbformat":4,"nbformat_minor":0}