Skip to content

Instantly share code, notes, and snippets.

@muhammad-alqabbani
Forked from cargo12/addtorrentv1.ipynb
Created May 6, 2020 22:34
Show Gist options
  • Save muhammad-alqabbani/8a1cb581ac944ece11b7ea437390ebb7 to your computer and use it in GitHub Desktop.
Save muhammad-alqabbani/8a1cb581ac944ece11b7ea437390ebb7 to your computer and use it in GitHub Desktop.

Revisions

  1. @cargo12 cargo12 created this gist Apr 5, 2020.
    240 changes: 240 additions & 0 deletions addtorrentv1.ipynb
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,240 @@
    {
    "nbformat": 4,
    "nbformat_minor": 0,
    "metadata": {
    "colab": {
    "name": "Torrent To Google Drive Downloader",
    "provenance": [],
    "collapsed_sections": [],
    "include_colab_link": true
    },
    "kernelspec": {
    "display_name": "Python 3",
    "language": "python",
    "name": "python3"
    }
    },
    "cells": [
    {
    "cell_type": "markdown",
    "metadata": {
    "id": "view-in-github",
    "colab_type": "text"
    },
    "source": [
    "<a href=\"https://colab.research.google.com/github/FKLC/Torrent-To-Google-Drive-Downloader/blob/master/Torrent_To_Google_Drive_Downloader.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",
    "colab_type": "text"
    },
    "source": [
    "# Torrent To Google Drive Downloader "
    ]
    },
    {
    "cell_type": "markdown",
    "metadata": {
    "id": "qYk44mBwJf6E",
    "colab_type": "text"
    },
    "source": [
    "**Important Note:** To get more disk space:\n",
    "> Go to Runtime -> Change Runtime and give GPU as the Hardware Accelerator. You will get around 384GB to download any torrent you want."
    ]
    },
    {
    "cell_type": "markdown",
    "metadata": {
    "id": "NaFa7M-e9YOr",
    "colab_type": "text"
    },
    "source": [
    "### Install libtorrent and Initialize Session"
    ]
    },
    {
    "cell_type": "code",
    "metadata": {
    "colab_type": "code",
    "id": "m6hF0emftx4h",
    "colab": {}
    },
    "source": [
    "!apt install python3-libtorrent\n",
    "\n",
    "import libtorrent as lt\n",
    "\n",
    "ses = lt.session()\n",
    "ses.listen_on(6881, 6891)\n",
    "downloads = []"
    ],
    "execution_count": 0,
    "outputs": []
    },
    {
    "cell_type": "markdown",
    "metadata": {
    "id": "Uf90U73y9YOj",
    "colab_type": "text"
    },
    "source": [
    "### Mount Google Drive\n",
    "To stream files we need to mount Google Drive."
    ]
    },
    {
    "cell_type": "code",
    "metadata": {
    "colab_type": "code",
    "id": "oWM9l2fvtuvO",
    "colab": {}
    },
    "source": [
    "from google.colab import drive\n",
    "\n",
    "drive.mount(\"/content/drive\")"
    ],
    "execution_count": 0,
    "outputs": []
    },
    {
    "cell_type": "markdown",
    "metadata": {
    "id": "R_1XuuIf9YOn",
    "colab_type": "text"
    },
    "source": [
    "### Add From Torrent File\n",
    "You can run this cell to add more files as many times as you want"
    ]
    },
    {
    "cell_type": "code",
    "metadata": {
    "colab_type": "code",
    "id": "0et2A6N3udA0",
    "colab": {}
    },
    "source": [
    "from google.colab import files\n",
    "\n",
    "source = files.upload()\n",
    "params = {\n",
    " \"save_path\": \"/content/drive/Shared drives/3/Torrent\"}\n",
    " \"ti\": lt.torrent_info(list(source.keys())[0]),\n",
    "}\n",
    "downloads.append(ses.add_torrent(params))"
    ],
    "execution_count": 0,
    "outputs": []
    },
    {
    "cell_type": "markdown",
    "metadata": {
    "id": "WD-6M6eZyzmj",
    "colab_type": "text"
    },
    "source": [
    "### Add From Magnet Link\n",
    "You can run this cell to add more files as many times as you want"
    ]
    },
    {
    "cell_type": "code",
    "metadata": {
    "id": "Cwi1GMlxy3te",
    "colab_type": "code",
    "colab": {}
    },
    "source": [
    "params = {\"save_path\": \"/content/drive/Shared drives/3/Torrent\"}\n",
    "\n",
    "while True:\n",
    " magnet_link = input(\"Enter Magnet Link Or Type Exit: \")\n",
    " if magnet_link.lower() == \"exit\":\n",
    " break\n",
    " downloads.append(\n",
    " lt.add_magnet_uri(ses, magnet_link, params)\n",
    " )\n"
    ],
    "execution_count": 0,
    "outputs": []
    },
    {
    "cell_type": "markdown",
    "metadata": {
    "id": "m-a1CUP39YOu",
    "colab_type": "text"
    },
    "source": [
    "### Start Download\n",
    "Source: https://stackoverflow.com/a/5494823/7957705 and [#3 issue](https://github.com/FKLC/Torrent-To-Google-Drive-Downloader/issues/3) which refers to this [stackoverflow question](https://stackoverflow.com/a/6053350/7957705)"
    ]
    },
    {
    "cell_type": "code",
    "metadata": {
    "colab_type": "code",
    "id": "DBNoYYoSuDBT",
    "colab": {}
    },
    "source": [
    "import time\n",
    "from IPython.display import display\n",
    "import ipywidgets as widgets\n",
    "\n",
    "state_str = [\n",
    " \"queued\",\n",
    " \"checking\",\n",
    " \"downloading metadata\",\n",
    " \"downloading\",\n",
    " \"finished\",\n",
    " \"seeding\",\n",
    " \"allocating\",\n",
    " \"checking fastresume\",\n",
    "]\n",
    "\n",
    "layout = widgets.Layout(width=\"auto\")\n",
    "style = {\"description_width\": \"initial\"}\n",
    "download_bars = [\n",
    " widgets.FloatSlider(\n",
    " step=0.01, disabled=True, layout=layout, style=style\n",
    " )\n",
    " for _ in downloads\n",
    "]\n",
    "display(*download_bars)\n",
    "\n",
    "while downloads:\n",
    " next_shift = 0\n",
    " for index, download in enumerate(downloads[:]):\n",
    " bar = download_bars[index + next_shift]\n",
    " if not download.is_seed():\n",
    " s = download.status()\n",
    "\n",
    " bar.description = \" \".join(\n",
    " [\n",
    " download.name(),\n",
    " str(s.download_rate / 1000),\n",
    " \"kB/s\",\n",
    " state_str[s.state],\n",
    " ]\n",
    " )\n",
    " bar.value = s.progress * 100\n",
    " else:\n",
    " next_shift -= 1\n",
    " ses.remove_torrent(download)\n",
    " downloads.remove(download)\n",
    " bar.close() # Seems to be not working in Colab (see https://github.com/googlecolab/colabtools/issues/726#issue-486731758)\n",
    " download_bars.remove(bar)\n",
    " print(download.name(), \"complete\")\n",
    " time.sleep(1)\n"
    ],
    "execution_count": 0,
    "outputs": []
    }
    ]
    }