Last active
November 15, 2022 07:09
-
-
Save Eggsmemory/ad9ea5e4354dc32d1befce2503ed6006 to your computer and use it in GitHub Desktop.
Mega-to-Google-Drive-Chinese-Version.ipynb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"nbformat": 4, | |
"nbformat_minor": 0, | |
"metadata": { | |
"colab": { | |
"name": "Mega-to-Google-Drive-Chinese-Version.ipynb", | |
"provenance": [], | |
"collapsed_sections": [], | |
"machine_shape": "hm", | |
"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/Eggsmemory/ad9ea5e4354dc32d1befce2503ed6006/mega-to-google-drive-chinese-version.ipynb\" target=\"_parent\"><img src=\"https://colab.research.google.com/assets/colab-badge.svg\" alt=\"Open In Colab\"/></a>" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": { | |
"id": "uzNhWbBScpCB" | |
}, | |
"source": [ | |
"##__掛載雲端硬碟到 /content/drive__\n", | |
"#####使用以下方法之一。 使用Google Drive File Stream應用程序掛載更為簡單,但有時候會遇到一些問題。 因此,可以通過使用Rclone來避免它們。" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": { | |
"id": "VxQ1zquNGKOz" | |
}, | |
"source": [ | |
"###__使用Google Drive File Stream應用程序掛載 - (簡單)__" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"metadata": { | |
"id": "tPuLCnbGFmxo", | |
"cellView": "form" | |
}, | |
"source": [ | |
"#@markdown <br><center><img src='https://upload.wikimedia.org/wikipedia/commons/thumb/d/da/Google_Drive_logo.png/600px-Google_Drive_logo.png' height=\"50\" alt=\"Gdrive-logo\"/></center>\n", | |
"#@markdown <center><h3>掛載雲端硬碟到 /content/drive</h3></center><br>\n", | |
"MODE = \"MOUNT\" #@param [\"MOUNT\", \"UNMOUNT\"]\n", | |
"#Mount your Gdrive! \n", | |
"from google.colab import drive\n", | |
"drive.mount._DEBUG = False\n", | |
"if MODE == \"MOUNT\":\n", | |
" drive.mount('/content/drive', force_remount=True)\n", | |
"elif MODE == \"UNMOUNT\":\n", | |
" try:\n", | |
" drive.flush_and_unmount()\n", | |
" except ValueError:\n", | |
" pass\n", | |
" get_ipython().system_raw(\"rm -rf /root/.config/Google/DriveFS\")" | |
], | |
"execution_count": null, | |
"outputs": [] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": { | |
"id": "9NX3YmadHXtb" | |
}, | |
"source": [ | |
"###__用RCLONE掛載 - (More Accurate)__" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"metadata": { | |
"id": "LGCkfIwWHmqw", | |
"cellView": "form" | |
}, | |
"source": [ | |
"#@markdown <h3>📝 備註:請先運行這個指令</h3>\n", | |
"#@markdown <h4>📝 上傳Rclone設定檔案</h4>\n", | |
"Setup_Time_Zone = False \n", | |
"\n", | |
"import os; from google.colab import files; from IPython.display import HTML, clear_output\n", | |
"\n", | |
"def upload_conf():\n", | |
" try:\n", | |
" display(HTML(\"<h2 style=\\\"font-family:Trebuchet MS;color:#446785;\\\">Please upload the config file of rclone (rclone.conf) from your computer.</h2><br>\"))\n", | |
" UploadConfig = files.upload().keys()\n", | |
" clear_output(wait=True)\n", | |
" if len(UploadConfig) == 0:\n", | |
" return display(HTML(\"<center><h2 style=\\\"font-family:Trebuchet MS;color:#ce2121;\\\">File upload has been cancelled during upload file.</h2><br></center>\"))\n", | |
" elif len(UploadConfig) == 1:\n", | |
" for fn in UploadConfig:\n", | |
" if os.path.isfile(\"/content/\" + fn) == True:\n", | |
" os.environ[\"rclone_conf\"] = fn\n", | |
" !mv -f \"$rclone_conf\" /root/.rclone.conf\n", | |
" !chmod 666 /root/.rclone.conf\n", | |
" if Setup_Time_Zone == True:\n", | |
" !sudo dpkg-reconfigure tzdata\n", | |
" clear_output(wait=True)\n", | |
" if os.path.isfile(\"/usr/bin/rclone\") == True:\n", | |
" return display(HTML(\"<center><h2 style=\\\"font-family:Trebuchet MS;color:#00b24c;\\\">Config has been changed.</h2><br></center>\"))\n", | |
" else:\n", | |
" !rm -rf /content/sample_data/\n", | |
" !curl -s https://rclone.org/install.sh | sudo bash\n", | |
" clear_output(wait=True)\n", | |
" return display(HTML(\"<center><h2 style=\\\"font-family:Trebuchet MS;color:#446785;\\\">Installation has been successfully completed.</h2><br></center>\"))\n", | |
" else:\n", | |
" return display(HTML(\"<center><h2 style=\\\"font-family:Trebuchet MS;color:#ce2121;\\\">File upload has been failed during upload file.</h2><br></center>\"))\n", | |
" else:\n", | |
" for fn in UploadConfig:\n", | |
" os.environ[\"rclone_conf\"] = fn\n", | |
" !rm -f \"$rclone_conf\"\n", | |
" return display(HTML(\"<center><h2 style=\\\"font-family:Trebuchet MS;color:#ce2121;\\\">Please uploading only one file at a time.</h2><br></center>\"))\n", | |
" except:\n", | |
" clear_output(wait=True)\n", | |
" return display(HTML(\"<center><h2 style=\\\"font-family:Trebuchet MS;color:#ce2121;\\\">Error occurred during upload file.</h2><br></center>\"))\n", | |
"\n", | |
"upload_conf()" | |
], | |
"execution_count": null, | |
"outputs": [] | |
}, | |
{ | |
"cell_type": "code", | |
"metadata": { | |
"id": "VHgreL9PHXtg", | |
"cellView": "form" | |
}, | |
"source": [ | |
"# ============================= FORM ============================= #\n", | |
"#@markdown <br><center><img src='https://rclone.org/img/logo_on_light__horizontal_color.svg' height=\"50\" alt=\"Gdrive-logo\"/></center>\n", | |
"#@markdown <center><h3>掛載雲端硬碟到 /content/drive</h3></center><br>\n", | |
"rclone_config_name = \"YourDrive\" #@param {type:\"string\"}\n", | |
"local_mount_location = \"/content/drive/\" #@param {type:\"string\"}\n", | |
"# ============================= FORM ============================= #\n", | |
"import time\n", | |
"import os\n", | |
"\n", | |
"# clear nohup\n", | |
"open(\"nohup.out\", 'w').close()\n", | |
"\n", | |
"\n", | |
"# unmount first\n", | |
"\n", | |
"!fusermount -u $local_mount_location 2>/dev/null\n", | |
"\n", | |
"\n", | |
"# mount without waiting for the command to complete\n", | |
"!mkdir $local_mount_location 2>/dev/null\n", | |
"!nohup rclone mount $rclone_config_name: $local_mount_location --buffer-size 96M & \n", | |
" \n", | |
"\n", | |
"\n", | |
"# Show the output that was written to nohup\n", | |
"time.sleep(3)\n", | |
"f = open(r\"nohup.out\", \"r\")\n", | |
"nohupText = f.read()\n", | |
"f.close()\n", | |
"\n", | |
"\n", | |
"dirs = os.listdir(local_mount_location)\n", | |
"\n", | |
"if len(dirs) > 0:\n", | |
" clear_output(wait=True)\n", | |
" print(\"Succeeded. \", str(len(dirs)), \"dirs found at\", local_mount_location)\n", | |
"\n", | |
"else:\n", | |
" print(\"\\n\\nNot succeeded. No files or directories in mounted location. \\nCheck your config name and content. If the rclone command was not found, run the cell above.\\n\\n\")\n", | |
" print(\"log:\\n\", nohupText)" | |
], | |
"execution_count": null, | |
"outputs": [] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": { | |
"id": "kSvUMUuTPX7r" | |
}, | |
"source": [ | |
"##__轉移MEGA檔案到Google雲端硬碟__\n", | |
"\n", | |
"\n", | |
"\n" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"metadata": { | |
"id": "XlDyLy_JZJ03", | |
"cellView": "form" | |
}, | |
"source": [ | |
"import sys, os, urllib.request\n", | |
"import time\n", | |
"import subprocess\n", | |
"import contextlib\n", | |
"from IPython.display import clear_output\n", | |
"#@markdown <br><center><img src='https://mega.nz/favicon.ico?v=3' height=\"50\" alt=\"MEGA-logo\"/></center>\n", | |
"#@markdown <center><h2>轉移MEGA檔案到Google雲端硬碟</h2></center><br>\n", | |
"HOME = os.path.expanduser(\"~\")\n", | |
"if not os.path.exists(f\"{HOME}/.ipython/ocr.py\"):\n", | |
" hCode = \"https://raw.githubusercontent.com/biplobsd/\" \\\n", | |
" \"OneClickRun/master/res/ocr.py\"\n", | |
" urllib.request.urlretrieve(hCode, f\"{HOME}/.ipython/ocr.py\")\n", | |
"\n", | |
"from ocr import (\n", | |
" runSh,\n", | |
" loadingAn,\n", | |
")\n", | |
"#@title MEGA public link download\n", | |
"URL = \"\" #@param {type:\"string\"}\n", | |
"OUTPUT_PATH = \"\" #@param {type:\"string\"}\n", | |
"if not OUTPUT_PATH:\n", | |
" os.makedirs(\"downloads\", exist_ok=True)\n", | |
" OUTPUT_PATH = \"downloads\"\n", | |
"# MEGAcmd installing\n", | |
"if not os.path.exists(\"/usr/bin/mega-cmd\"):\n", | |
" loadingAn()\n", | |
" print(\"Installing MEGA ...\")\n", | |
" runSh('sudo apt-get -y update')\n", | |
" runSh('sudo apt-get -y install libmms0 libc-ares2 libc6 libcrypto++6 libgcc1 libmediainfo0v5 libpcre3 libpcrecpp0v5 libssl1.1 libstdc++6 libzen0v5 zlib1g apt-transport-https')\n", | |
" runSh('sudo curl -sL -o /var/cache/apt/archives/MEGAcmd.deb https://mega.nz/linux/MEGAsync/Debian_9.0/amd64/megacmd-Debian_9.0_amd64.deb', output=True)\n", | |
" runSh('sudo dpkg -i /var/cache/apt/archives/MEGAcmd.deb', output=True)\n", | |
" print(\"MEGA is installed.\")\n", | |
" clear_output()\n", | |
"\n", | |
"# Unix, Windows and old Macintosh end-of-line\n", | |
"newlines = ['\\n', '\\r\\n', '\\r']\n", | |
"\n", | |
"def unbuffered(proc, stream='stdout'):\n", | |
" stream = getattr(proc, stream)\n", | |
" with contextlib.closing(stream):\n", | |
" while True:\n", | |
" out = []\n", | |
" last = stream.read(1)\n", | |
" # Don't loop forever\n", | |
" if last == '' and proc.poll() is not None:\n", | |
" break\n", | |
" while last not in newlines:\n", | |
" # Don't loop forever\n", | |
" if last == '' and proc.poll() is not None:\n", | |
" break\n", | |
" out.append(last)\n", | |
" last = stream.read(1)\n", | |
" out = ''.join(out)\n", | |
" yield out\n", | |
"\n", | |
"\n", | |
"def transfare():\n", | |
" import codecs\n", | |
" decoder = codecs.getincrementaldecoder(\"UTF-8\")()\n", | |
" cmd = [\"mega-get\", URL, OUTPUT_PATH]\n", | |
" proc = subprocess.Popen(\n", | |
" cmd,\n", | |
" stdout=subprocess.PIPE,\n", | |
" stderr=subprocess.STDOUT,\n", | |
" # Make all end-of-lines '\\n'\n", | |
" universal_newlines=True,\n", | |
" )\n", | |
" for line in unbuffered(proc):\n", | |
" print(line)\n", | |
" \n", | |
"\n", | |
"\n", | |
"transfare()\n", | |
"\n" | |
], | |
"execution_count": null, | |
"outputs": [] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": { | |
"id": "7TGCDD4YGd_a" | |
}, | |
"source": [ | |
"####_更新:如果你有MEGA PRO帳號請在這裡填入,會使用帳號傳輸額度_" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"metadata": { | |
"id": "Vgh1xlQYGZCO", | |
"cellView": "form" | |
}, | |
"source": [ | |
"# @markdown <center><h3>填入你的MEGA帳號</h3></center><br>\n", | |
"from functools import wraps\n", | |
"import errno\n", | |
"import os\n", | |
"import signal\n", | |
"import subprocess\n", | |
"import shlex\n", | |
"\n", | |
"\n", | |
"class TimeoutError(Exception):\n", | |
" pass\n", | |
"\n", | |
"\n", | |
"def timeout(seconds=10, error_message=os.strerror(errno.ETIME)):\n", | |
" def decorator(func):\n", | |
" def _handle_timeout(signum, frame):\n", | |
" raise TimeoutError(error_message)\n", | |
"\n", | |
" def wrapper(*args, **kwargs):\n", | |
" signal.signal(signal.SIGALRM, _handle_timeout)\n", | |
" signal.alarm(seconds)\n", | |
" try:\n", | |
" result = func(*args, **kwargs)\n", | |
" finally:\n", | |
" signal.alarm(0)\n", | |
" return result\n", | |
"\n", | |
" return wraps(func)(wrapper)\n", | |
"\n", | |
" return decorator\n", | |
"\n", | |
"\n", | |
"if not os.path.exists(\"/root/.ipython/ocr.py\"):\n", | |
" from subprocess import run\n", | |
" from shlex import split\n", | |
"\n", | |
" shellCmd = \"wget -qq https://raw.githubusercontent.com/biplobsd/OneClickRun/master/res/ocr.py \\\n", | |
" -O /root/.ipython/ocr.py\"\n", | |
" run(split(shellCmd))\n", | |
"from ocr import runSh\n", | |
"\n", | |
"@timeout(10)\n", | |
"def runShT(args):\n", | |
" return runSh(args, output=True)\n", | |
"\n", | |
"\n", | |
"# MEGAcmd installing\n", | |
"if not os.path.exists(\"/usr/bin/mega-cmd\"):\n", | |
" print(\"Installing MEGA ...\")\n", | |
" runSh('sudo apt-get -y update')\n", | |
" runSh('sudo apt-get -y install libmms0 libc-ares2 libc6 libcrypto++6 libgcc1 libmediainfo0v5 libpcre3 libpcrecpp0v5 libssl1.1 libstdc++6 libzen0v5 zlib1g apt-transport-https')\n", | |
" runSh('sudo curl -sL -o /var/cache/apt/archives/MEGAcmd.deb https://mega.nz/linux/MEGAsync/Debian_9.0/amd64/megacmd-Debian_9.0_amd64.deb', output=True)\n", | |
" runSh('sudo dpkg -i /var/cache/apt/archives/MEGAcmd.deb', output=True)\n", | |
" print(\"MEGA is installed.\")\n", | |
"else:\n", | |
" !pkill mega-cmd\n", | |
"\n", | |
"# INPUT YOUR MEGA ID\n", | |
"\n", | |
"USERNAME = \"\" # @param {type:\"string\"}\n", | |
"PASSWORD = \"\" # @param {type:\"string\"}\n", | |
"if not (USERNAME == \"\" or PASSWORD == \"\"):\n", | |
" try:\n", | |
" runShT(f\"mega-login {USERNAME} {PASSWORD}\")\n", | |
" except TimeoutError:\n", | |
" runSh('mega-whoami', output=True)\n", | |
"else:\n", | |
" print(\"Please Input your Mega IDs.\")\n", | |
"# @markdown *登錄後,使用上面的單元格(主單元格)使用您的Mega Pro帳戶的傳輸配額將文件從Mega傳輸到GDrive*" | |
], | |
"execution_count": null, | |
"outputs": [] | |
} | |
] | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment