Skip to content

Instantly share code, notes, and snippets.

@1x6
Last active April 17, 2023 10:59
Show Gist options
  • Save 1x6/8ac7f6a36a69ef2fb78d7ad0055273fd to your computer and use it in GitHub Desktop.
Save 1x6/8ac7f6a36a69ef2fb78d7ad0055273fd to your computer and use it in GitHub Desktop.
Torrent To Google Drive Downloader v3
Display the source blob
Display the rendered blob
Raw
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Display the source blob
Display the rendered blob
Raw
{
"nbformat": 4,
"nbformat_minor": 0,
"metadata": {
"colab": {
"name": "Torrent To Google Drive Downloader v2",
"provenance": [],
"collapsed_sections": [],
"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/1x6/8ac7f6a36a69ef2fb78d7ad0055273fd/torrent-to-google-drive-downloader-v2.ipynb\" target=\"_parent\"><img src=\"https://colab.research.google.com/assets/colab-badge.svg\" alt=\"Open In Colab\"/></a>"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "aQuWDmfm9YOi"
},
"source": [
"# Torrent To Google Drive Downloader v2"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "Uf90U73y9YOj"
},
"source": [
"### Mount Google Drive\n",
"To stream files we need to mount Google Drive."
]
},
{
"cell_type": "code",
"metadata": {
"id": "oWM9l2fvtuvO",
"colab": {
"base_uri": "https://localhost:8080/",
"height": 122
},
"outputId": "c6239492-4600-40ba-9bf0-d1b3d07c6ba3"
},
"source": [
"from google.colab import drive\n",
"drive.mount('/content/drive')"
],
"execution_count": null,
"outputs": [
{
"output_type": "stream",
"text": [
"Go to this URL in a browser: https://accounts.google.com/o/oauth2/auth?client_id=947318989803-6bn6qk8qdgf4n4g3pfee6491hc0brc4i.apps.googleusercontent.com&redirect_uri=urn%3Aietf%3Awg%3Aoauth%3A2.0%3Aoob&scope=email%20https%3A%2F%2Fwww.googleapis.com%2Fauth%2Fdocs.test%20https%3A%2F%2Fwww.googleapis.com%2Fauth%2Fdrive%20https%3A%2F%2Fwww.googleapis.com%2Fauth%2Fdrive.photos.readonly%20https%3A%2F%2Fwww.googleapis.com%2Fauth%2Fpeopleapi.readonly&response_type=code\n",
"\n",
"Enter your authorization code:\n",
"··········\n",
"Mounted at /content/drive\n"
],
"name": "stdout"
}
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "FexiyATtNwTG"
},
"source": [
"###Dependency\n",
"https://www.libtorrent.org/"
]
},
{
"cell_type": "code",
"metadata": {
"id": "m6hF0emftx4h"
},
"source": [
"!python -m pip install --upgrade pip setuptools wheel\r\n",
"!python -m pip install lbry-libtorrent\r\n",
"!apt install python3-libtorrent\r\n"
],
"execution_count": null,
"outputs": []
},
{
"cell_type": "markdown",
"metadata": {
"id": "tqKcWHZcYRIA"
},
"source": [
"### Code to download torrent\n",
"Variable **link** stores the link string."
]
},
{
"cell_type": "code",
"metadata": {
"id": "b09BxnANO2ep"
},
"source": [
"import libtorrent as lt\n",
"import time\n",
"import datetime\n",
"\n",
"ses = lt.session()\n",
"ses.listen_on(6881, 6891)\n",
"params = {\n",
" 'save_path': '/content/drive/Shared drives/Studying Materials',\n",
" 'storage_mode': lt.storage_mode_t(2),\n",
" 'paused': False,\n",
" 'auto_managed': True,\n",
" 'duplicate_is_error': True}\n",
"link = \"\" # PASTE TORRENT/MAGNET LINK HERE\n",
"print(link)\n",
"\n",
"handle = lt.add_magnet_uri(ses, link, params)\n",
"ses.start_dht()\n",
"\n",
"begin = time.time()\n",
"print(datetime.datetime.now())\n",
"\n",
"print ('Downloading Metadata...')\n",
"while (not handle.has_metadata()):\n",
" time.sleep(1)\n",
"print ('Got Metadata, Starting Torrent Download...')\n",
"\n",
"print(\"Starting\", handle.name())\n",
"\n",
"while (handle.status().state != lt.torrent_status.seeding):\n",
" s = handle.status()\n",
" state_str = ['queued', 'checking', 'downloading metadata', \\\n",
" 'downloading', 'finished', 'seeding', 'allocating']\n",
" print ('%.2f%% complete (down: %.1f kb/s up: %.1f kB/s peers: %d) %s ' % \\\n",
" (s.progress * 100, s.download_rate / 1000, s.upload_rate / 1000, \\\n",
" s.num_peers, state_str[s.state]))\n",
" time.sleep(5)\n",
"\n",
"end = time.time()\n",
"print(handle.name(), \"COMPLETE\")\n",
"\n",
"print(\"Elapsed Time: \",int((end-begin)//60),\"min :\", int((end-begin)%60), \"sec\")\n",
"\n",
"print(datetime.datetime.now())"
],
"execution_count": null,
"outputs": []
}
]
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment