Skip to content

Instantly share code, notes, and snippets.

@hsj51
Last active April 3, 2021 12:28
Show Gist options
  • Save hsj51/36fdd59ebe47d77be6ecd70c66b84683 to your computer and use it in GitHub Desktop.
Save hsj51/36fdd59ebe47d77be6ecd70c66b84683 to your computer and use it in GitHub Desktop.
Colab Notebook to generate credentials.json and/or telegra.ph token & to generate/download SAs, add them to team drive or to upload then to GitHub.
Display the source blob
Display the rendered blob
Raw
{
"nbformat": 4,
"nbformat_minor": 0,
"metadata": {
"colab": {
"name": "aria-telegram-mirror-bot.ipynb",
"provenance": [],
"collapsed_sections": [],
"toc_visible": true,
"authorship_tag": "ABX9TyOsw/Y8gnchSv+/vJEKg+qD",
"include_colab_link": true
},
"kernelspec": {
"name": "python3",
"display_name": "Python 3"
}
},
"cells": [
{
"cell_type": "markdown",
"metadata": {
"id": "view-in-github",
"colab_type": "text"
},
"source": [
"<a href=\"https://colab.research.google.com/gist/hsj51/36fdd59ebe47d77be6ecd70c66b84683\" target=\"_parent\"><img src=\"https://colab.research.google.com/assets/colab-badge.svg\" alt=\"Open In Colab\"/></a>"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "7yGA5mj2pMhb"
},
"source": [
"##**1. Run 1st Cell to generate credentials.json and/or telegra.ph token.**\n",
"\n",
"## **2. Run 2nd cell to generate/download SAs, add them to team drive or to upload then to GitHub.**\n",
"\n",
"\n"
]
},
{
"cell_type": "code",
"metadata": {
"id": "w1JpfE5dj3t3",
"cellView": "form"
},
"source": [
"#@title #<b> credentials.json | telegra.ph token\n",
"\n",
"#@markdown ### <b>Generate `credentials.json`\n",
"\n",
"#@markdown * Follow Step 2 of [heroku_guide](https://github.com/arghyac35/aria-telegram-mirror-bot/blob/master/heroku_guide.md#pre-requisites-not-to-be-followed-while-using-service-account-skip-to-deployment) to get `client_secret.json`.\n",
"\n",
"#@markdown * Paste its code below <b>OR</b> Enter its raw github gist url / direct-link.\n",
"code = \"\" #@param {type:\"string\"}\n",
"url = \"\" #@param {type:\"string\"}\n",
"\n",
"#@markdown ## <b>Generate `TELEGRAPH_TOKEN`\n",
"\n",
"#@markdown Not sure, see `TELEGRAPH_TOKEN` in [Constants Description](https://github.com/arghyac35/aria-telegram-mirror-bot#constants-description) section.\n",
"\n",
"#@markdown * Tick below checkbox to generate Telegraph token\n",
"gen_telegraph_token = False #@param {type:\"boolean\"}\n",
"\n",
"#@markdown * Account name for telegra.ph account.\n",
"account_name =\"MirrorBot\" #@param {type:\"string\"}\n",
"\n",
"#@markdown #<b> Now run this cell to generate `credentials.json` and/or `telegra.ph token`.\n",
"\n",
"\n",
"from google.colab import files\n",
"import subprocess, IPython, urllib.request, os, sys\n",
"\n",
"def prep():\n",
" !apt install nodejs;\n",
" !git clone https://github.com/arghyac35/aria-telegram-mirror-bot bot;cd bot; npm install;\n",
"\n",
"def credential():\n",
" cmd=!cd bot; npm run generateDriveCredentials;\n",
" if os.path.isfile('/content/bot/credentials.json'): pass\n",
" else:\n",
" output(cmd)\n",
" print('Something went wrong, credentials.json not generated')\n",
" sys.stdout.flush()\n",
" os._exit(0)\n",
"\n",
"def post():\n",
" upload = subprocess.check_output(\"curl --upload-file /content/bot/credentials.json https://transfer.sh/credentials.json\", shell=True).decode().split(\"transfer.sh\")\n",
" download_link = \"transfer.sh/get\".join(upload)\n",
" style_code = \\\n",
"'''\n",
"<style>\n",
".pure-material-button-contained{margin:50px;top:10%;display:inline-block;box-sizing:border-box;border:none;border-radius:4px;padding:0 16px;min-width:64px;height:36px;vertical-align:middle;text-align:center;text-overflow:ellipsis;text-transform:uppercase;color:rgb(var(--pure-material-onprimary-rgb,255,255,255));background-color:rgb(var(--pure-material-primary-rgb,33,150,243));box-shadow:0 3px 1px -2px rgba(0,0,0,.2),0 2px 2px 0 rgba(0,0,0,.14),0 1px 5px 0 rgba(0,0,0,.12);font-family:var(--pure-material-font, \"Roboto\", \"Segoe UI\", BlinkMacSystemFont, system-ui, -apple-system);font-size:14px;font-weight:500;line-height:36px;overflow:hidden;outline:0;cursor:pointer;transition:box-shadow .2s}.pure-material-button-contained::-moz-focus-inner{border:none}.pure-material-button-contained::before{content:\"\";position:absolute;top:0;bottom:0;left:0;right:0;background-color:rgb(var(--pure-material-onprimary-rgb,255,255,255));opacity:0;transition:opacity .2s}.pure-material-button-contained::after{content:\"\";position:absolute;left:50%;top:50%;border-radius:50%;padding:50%;width:32px;height:32px;background-color:rgb(var(--pure-material-onprimary-rgb,255,255,255));opacity:0;transform:translate(-50%,-50%) scale(1);transition:opacity 1s,transform .5s}.pure-material-button-contained:focus,.pure-material-button-contained:hover{box-shadow:0 2px 4px -1px rgba(0,0,0,.2),0 4px 5px 0 rgba(0,0,0,.14),0 1px 10px 0 rgba(0,0,0,.12)}.pure-material-button-contained:hover::before{opacity:.08}.pure-material-button-contained:focus::before{opacity:.24}.pure-material-button-contained:hover:focus::before{opacity:.3}.pure-material-button-contained:active{box-shadow:0 5px 5px -3px rgba(0,0,0,.2),0 8px 10px 1px rgba(0,0,0,.14),0 3px 14px 2px rgba(0,0,0,.12)}.pure-material-button-contained:active::after{opacity:.32;transform:translate(-50%,-50%) scale(0);transition:transform 0s}.pure-material-button-contained:disabled{color:rgba(var(--pure-material-onsurface-rgb,0,0,0),.38);background-color:rgba(var(--pure-material-onsurface-rgb,0,0,0),.12);box-shadow:none;cursor:initial}.pure-material-button-contained:disabled::before{opacity:0}.pure-material-button-contained:disabled::after{opacity:0}\n",
"</style>\n",
"'''\n",
" html_code = \\\n",
"'''\n",
"<center><button class=\"pure-material-button-contained\" onclick=\" window.open('{download_link}','_blank')\">Click here to Download your credentials.json</button></center>\n",
"'''\n",
" files.download('/content/bot/credentials.json')\n",
" display(IPython.display.HTML(style_code))\n",
" display(IPython.display.HTML(eval('f'+repr(html_code))))\n",
"\n",
"def output(cmd):\n",
" for i in cmd: print(i)\n",
"\n",
"if url:\n",
" prep()\n",
" urllib.request.urlretrieve(url,'/content/bot/client_secret.json')\n",
" credential()\n",
" post()\n",
"elif code:\n",
" prep()\n",
" with open(\"/content/bot/client_secret.json\",\"w\") as client_secret:\n",
" client_secret.write(code)\n",
" credential()\n",
" post()\n",
"else:\n",
" print(\"Not generating credentials.json.\")\n",
"\n",
"if gen_telegraph_token:\n",
" prep()\n",
" !cd bot; echo \"$account_name\" | npm run generateTelegraphToken\n",
"else:\n",
" print(\"Not generating telegra.ph token.\")"
],
"execution_count": null,
"outputs": []
},
{
"cell_type": "code",
"metadata": {
"cellView": "form",
"id": "nKAmcbtzQVhv"
},
"source": [
"#@markdown #<b>Service Accounts\n",
"\n",
"#@markdown ### **Prerequisite**\n",
"#@markdown * Follow Step 2 of [heroku_guide](https://github.com/arghyac35/aria-telegram-mirror-bot/blob/master/heroku_guide.md#pre-requisites-not-to-be-followed-while-using-service-account-skip-to-deployment) to get `client_secret.json`.\n",
"\n",
"#@markdown * Paste its code below <b>OR</b> Enter its raw github gist url / direct-link.\n",
"client_secret_code = \"\" #@param {type:\"string\"}\n",
"client_secret_url=\"\" #@param {type:\"string\"}\n",
"\n",
"#@markdown ## **Generate Service Accounts**\n",
"#@markdown * Create 1 New project which has 100 SAs\n",
"gen_sas = False #@param {type:\"boolean\"}\n",
"\n",
"#@markdown ## **Re-download the keys**\n",
"#@markdown * If you have created SAs in past, re-download the keys\n",
"re_download_sas= False #@param {type:\"boolean\"}\n",
"#@markdown * Project ID of the project from which to re-download the keys.\n",
"project_id= \"\" #@param {type:\"string\"}\n",
"\n",
"#@markdown ## **Add all the service accounts to the Team Drive**\n",
"add_sas_to_drive=True #@param {type:\"boolean\"}\n",
"\n",
"SharedTeamDriveSrcID=\"\" #@param {type:\"string\"}\n",
"\n",
"#@markdown ## **Upload SAs to GitHub**\n",
"#@markdown * Upload generated/downloaded SAs to github\n",
"upload_to_github= False #@param {type:\"boolean\"}\n",
"#@markdown * `GITHUB_USERNAME` Your github username\n",
"GITHUB_USERNAME= \"\" #@param {type:\"string\"}\n",
"#@markdown * `GITHUB_EMAIL` Your github email\n",
"GITHUB_EMAIL=\"\" #@param {type:\"string\"}\n",
"#@markdown * `REPO_NAME` repo name to which SAs should be uploaded\n",
"REPO_NAME= \"\" #@param {type:\"string\"}\n",
"#@markdown * `GITHUB_API_TOKEN` Create a Personal Access Token with repo scope. Go to github [profile settings->Developer Settings->Personal Access Tokens->Generate new token](https://github.com/settings/tokens/new)->Add any note and then select only repo scope & click on Generate token and copy the token->paste it here.\n",
"GITHUB_API_TOKEN= \"\" #@param {type:\"string\"}\n",
"\n",
"#@markdown #<b> Now run this cell to generate/download SAs and/or upload them to GitHub repo.\n",
"\n",
"\n",
"import urllib.request, os, sys\n",
"if add_sas_to_drive:\n",
" !pip3 install progress >> /dev/null\n",
" import progress\n",
"\n",
"#https://github.com/lzzy12/python-aria-mirror-bot/\n",
"urllib.request.urlretrieve('https://github.com/lzzy12/python-aria-mirror-bot/raw/master/gen_sa_accounts.py','gen_sa_accounts.py')\n",
"urllib.request.urlretrieve('https://github.com/lzzy12/python-aria-mirror-bot/raw/master/add_to_team_drive.py', 'add_to_team_drive.py')\n",
"\n",
"def abort(err=\"\"):\n",
" print(err, '\\nAborting!!!')\n",
" sys.stdout.flush()\n",
" os._exit(0)\n",
"\n",
"def output(cmd):\n",
" for i in cmd:print(i)\n",
"\n",
"def check_client_secret():\n",
" if client_secret_url:\n",
" urllib.request.urlretrieve(client_secret_url,'client_secret.json')\n",
" return True\n",
" elif client_secret_code:\n",
" with open(\"client_secret.json\",\"w\") as client_secret:\n",
" client_secret.write(code)\n",
" return True\n",
" elif os.path.isfile('client_secret.json'): \n",
" return True\n",
" else:\n",
" print('client_secret.json file not found.')\n",
" return False\n",
"\n",
"def generate_sas():\n",
" if gen_sas and re_download_sas:\n",
" print('gen_sas and re_download_sas both ticked. Only one is required.')\n",
" return False\n",
" elif gen_sas:\n",
" !python3 gen_sa_accounts.py --quick-setup 1 --new-only -c client_secret.json\n",
" return True\n",
" elif re_download_sas:\n",
" if project_id:\n",
" project=!python3 gen_sa_accounts.py --list-sas $project_id -c client_secret.json\n",
" if project_id in project[0]:\n",
" !python3 gen_sa_accounts.py --download-keys $project_id -c client_secret.json\n",
" return True\n",
" elif 'No service accounts.' in project:\n",
" print('No service accounts in given project_id!\\nEnter correct project id or Create a new project')\n",
" return False\n",
" else:\n",
" output(project)\n",
" print('Something wrong :(\\nCheck the project_id.')\n",
" return False\n",
" else:\n",
" print('project_id not Specified.')\n",
" return False\n",
" else:\n",
" print('Neither gen_sas nor re_download_sas ticked.')\n",
" return False\n",
"\n",
"def upload_repo():\n",
" check_repo=!curl -H \"Authorization: token $GITHUB_API_TOKEN\" https://api.github.com/repos/$GITHUB_USERNAME/$REPO_NAME\n",
"\n",
" if ' \"message\": \"Not Found\",' in check_repo:\n",
" create_repo='''curl -i -H \"Authorization: token '''+GITHUB_API_TOKEN+'''\" \\\n",
" -d '{ \\\n",
" \"name\": \"'''+REPO_NAME+'''\", \\\n",
" \"private\": true \\\n",
" }' \\\n",
" https://api.github.com/user/repos\n",
" '''\n",
" output=!$create_repo\n",
" if 'HTTP/2 201 ' in output: print('Repo created. Uploading...')\n",
" else:\n",
" for i in output:\n",
" print(i)\n",
" else:print('Repo already exists! Change REPO_NAME.')\n",
"\n",
" #Upload SAs\n",
" !git config --global user.name \"$GITHUB_USERNAME\" ;git config --global user.email \"$GITHUB_EMAIL\";\n",
" !cd accounts ;git init ;git add .;git commit -m \"Initial Commit\" > /dev/null ;git push https://$GITHUB_USERNAME:$GITHUB_API_TOKEN@github.com/$GITHUB_USERNAME/$REPO_NAME HEAD:main -f;\n",
" if '' in check_repo: print('SAs uploaded successfully to https://github.com/%s/%s ' % ( GITHUB_USERNAME, REPO_NAME ))\n",
"\n",
"def github_check():\n",
" if upload_to_github:\n",
" if not GITHUB_USERNAME:\n",
" print('GitHub username not specified!')\n",
" return False\n",
" if not GITHUB_API_TOKEN:\n",
" print('GitHub API Token not specified!')\n",
" return False\n",
" if not GITHUB_EMAIL:\n",
" print('GITHUB_EMAIL not specified!')\n",
" return False\n",
" if not REPO_NAME:\n",
" REPO_NAME='service_accounts'\n",
" return True\n",
"\n",
"if check_client_secret() and generate_sas():\n",
" if os.path.exists('accounts') and os.path.isdir('accounts') and os.listdir('accounts'):\n",
" print('SAs generated successfully.')\n",
" can_upload=True\n",
" if add_sas_to_drive:\n",
" !python3 add_to_team_drive.py -c client_secret.json -d $SharedTeamDriveSrcID\n",
" else:\n",
" can_upload=False\n",
" print('Something went wrong :(\\nSAs not generated!')\n",
"\n",
"elif os.path.exists('accounts') and os.path.isdir('accounts') and os.listdir('accounts'):\n",
" can_upload=True\n",
" if add_sas_to_drive:\n",
" !python3 add_to_team_drive.py -c client_secret.json -d $SharedTeamDriveSrcID\n",
"\n",
"if upload_to_github and can_upload and github_check():\n",
" upload_repo()"
],
"execution_count": null,
"outputs": []
},
{
"cell_type": "markdown",
"metadata": {
"id": "koDRSrmbSBuQ"
},
"source": [
"#<b>If you have any issues regarding this notebook, contact me using Telegram: [@hsj51](https://t.me/hsj51)\n"
]
}
]
}
Display the source blob
Display the rendered blob
Raw
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment