Skip to content

Instantly share code, notes, and snippets.

@rohanrudra55
Last active February 15, 2023 04:59
Show Gist options
  • Select an option

  • Save rohanrudra55/409a5feacefa7eb0f69b43dfef4d25f1 to your computer and use it in GitHub Desktop.

Select an option

Save rohanrudra55/409a5feacefa7eb0f69b43dfef4d25f1 to your computer and use it in GitHub Desktop.
Torrent To Google Drive Downloader
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"cell_type": "markdown",
"metadata": {
"colab_type": "text",
"id": "view-in-github"
},
"source": [
"<a href=\"https://colab.research.google.com/gist/rohanrudra55/409a5feacefa7eb0f69b43dfef4d25f1/torrent-to-google-drive-downloader-roh-techz.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 "
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "qYk44mBwJf6E"
},
"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"
},
"source": [
"### Install libtorrent and Initialize Session"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"!python -m pip install --upgrade pip setuptools wheel\n",
"!python -m pip install lbry-libtorrent\n",
"!apt install python3-libtorrent"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Start Connecting"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "m6hF0emftx4h",
"outputId": "4e5072c2-be4c-4be6-c8f2-2818b95c3553"
},
"outputs": [],
"source": [
"\"\"\"\n",
"6881 : TCP Port for torrent client\n",
"downloads : To store the downloads\n",
"\"\"\"\n",
"import libtorrent as lt\n",
"\n",
"ses = lt.session()\n",
"ses.listen_on(6881, 6891)\n",
"downloads = []"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "Uf90U73y9YOj"
},
"source": [
"### Mount Google Drive\n",
"To stream files we need to connect Google Drive. \n",
"(You may need to grant permission)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "oWM9l2fvtuvO",
"outputId": "20d508ae-b6f1-4aaf-e805-2a826fd8f8c8"
},
"outputs": [],
"source": [
"from google.colab import drive\n",
"\n",
"drive.mount(\"/content/drive\")\n",
"dest_name = \"Torrent\"\n",
"dest = \"/content/drive/My Drive/\"+dest_name"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "R_1XuuIf9YOn"
},
"source": [
"### Add From Torrent File\n",
"You can run this cell to add more files as many times as you want"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"id": "0et2A6N3udA0"
},
"outputs": [],
"source": [
"from google.colab import files\n",
"source = files.upload()\n",
"params = {\n",
" \"save_path\": dest,\n",
" \"ti\": lt.torrent_info(list(source.keys())[0]),\n",
"}\n",
"downloads.append(ses.add_torrent(params))"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "WD-6M6eZyzmj"
},
"source": [
"### Add From Magnet Link\n",
"You can run this cell to add more files as many times as you want. \n",
"\n",
"Type `exit` when finish adding files."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "Cwi1GMlxy3te",
"outputId": "4c01143b-f927-4f34-fe99-eae143023e9f"
},
"outputs": [],
"source": [
"params = {\"save_path\": dest}\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"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "m-a1CUP39YOu"
},
"source": [
"### Start Download"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"id": "DBNoYYoSuDBT"
},
"outputs": [],
"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() \n",
" download_bars.remove(bar)\n",
" print(download.name(), \"complete\")\n",
" time.sleep(1)\n"
]
}
],
"metadata": {
"accelerator": "GPU",
"colab": {
"include_colab_link": true,
"provenance": []
},
"kernelspec": {
"display_name": "Python 3 (ipykernel)",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.10.9"
}
},
"nbformat": 4,
"nbformat_minor": 1
}
@rohanrudra55
Copy link
Author

Updated

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment