├── .env ├── .gitattributes ├── .gitignore ├── Dockerfile ├── LICENSE ├── README.md ├── driver.py ├── heroku.yml └── requirements.txt /.env: -------------------------------------------------------------------------------- 1 | EMAIL= 2 | PASS= 3 | MAGNET= -------------------------------------------------------------------------------- /.gitattributes: -------------------------------------------------------------------------------- 1 | # Auto detect text files and perform LF normalization 2 | * text=auto 3 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | # Byte-compiled / optimized / DLL files 2 | __pycache__/ 3 | *.py[cod] 4 | *$py.class 5 | 6 | # C extensions 7 | *.so 8 | 9 | # Distribution / packaging 10 | .Python 11 | build/ 12 | develop-eggs/ 13 | dist/ 14 | downloads/ 15 | eggs/ 16 | .eggs/ 17 | lib/ 18 | lib64/ 19 | parts/ 20 | sdist/ 21 | var/ 22 | wheels/ 23 | *.egg-info/ 24 | .installed.cfg 25 | *.egg 26 | MANIFEST 27 | 28 | # PyInstaller 29 | # Usually these files are written by a python script from a template 30 | # before PyInstaller builds the exe, so as to inject date/other infos into it. 31 | *.manifest 32 | *.spec 33 | 34 | # Installer logs 35 | pip-log.txt 36 | pip-delete-this-directory.txt 37 | 38 | # Unit test / coverage reports 39 | htmlcov/ 40 | .tox/ 41 | .nox/ 42 | .coverage 43 | .coverage.* 44 | .cache 45 | nosetests.xml 46 | coverage.xml 47 | *.cover 48 | .hypothesis/ 49 | .pytest_cache/ 50 | 51 | # Translations 52 | *.mo 53 | *.pot 54 | 55 | # Django stuff: 56 | *.log 57 | local_settings.py 58 | db.sqlite3 59 | 60 | # Flask stuff: 61 | instance/ 62 | .webassets-cache 63 | 64 | # Scrapy stuff: 65 | .scrapy 66 | 67 | # Sphinx documentation 68 | docs/_build/ 69 | 70 | # PyBuilder 71 | target/ 72 | 73 | # Jupyter Notebook 74 | .ipynb_checkpoints 75 | 76 | # IPython 77 | profile_default/ 78 | ipython_config.py 79 | 80 | # pyenv 81 | .python-version 82 | 83 | # celery beat schedule file 84 | celerybeat-schedule 85 | 86 | # SageMath parsed files 87 | *.sage.py 88 | 89 | # Environments 90 | .venv 91 | env/ 92 | venv/ 93 | ENV/ 94 | env.bak/ 95 | venv.bak/ 96 | 97 | # Spyder project settings 98 | .spyderproject 99 | .spyproject 100 | 101 | # Rope project settings 102 | .ropeproject 103 | 104 | # mkdocs documentation 105 | /site 106 | 107 | # mypy 108 | .mypy_cache/ 109 | .dmypy.json 110 | dmypy.json 111 | 112 | # Pyre type checker 113 | .pyre/ 114 | -------------------------------------------------------------------------------- /Dockerfile: -------------------------------------------------------------------------------- 1 | FROM ubuntu:latest 2 | 3 | WORKDIR /app 4 | 5 | #installing python,pip and gcc 6 | RUN apt-get update 7 | RUN apt-get install -y python3-pip python3-libtorrent 8 | 9 | #installing reqs 10 | COPY requirements.txt /app/requirements.txt 11 | RUN pip3 install -r requirements.txt 12 | 13 | #running the code 14 | COPY driver.py /app/driver.py 15 | CMD ["python3", "driver.py"] -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2021 Nuzair Rasheed 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Torrent2mega 2 | -------------- 3 | * A simple docker to transfer torrents to your Mega drive. Can be run on Heroku. 4 | 5 | ## Purpose? 6 | ----------- 7 | * This tool is aimed to utilize the high speed of a VPS to download really slow torrents to you Mega drive. 8 | 9 | ## Setup 10 | --------- 11 | * Fork this repo. 12 | * Need to set Env variables: 13 | * EMAIL: "your_mega_email_id" 14 | * PASS: "your_mega_password" 15 | * MAGNET: "magnet_uri" 16 | 17 | * Above variable can be set in heroku `config vars`. Don't use "quotes". 18 | * To run the docker on a VPS: 19 | * Fill the `.env` given. Don't use "quotes". 20 | * `sudo docker build --no-cache -t torrent2mega .` 21 | * `sudo docker run --env-file .env -it torrent2mega` 22 | 23 | * On Heroku, make sure to kill the Dyno after the download is complete, because Heroku will relaunch the code and may lead to overwriting in Mega. 24 | * When using on Heroku, Make sure to deploy the repo everytime rather than just starting the Dyno, this will delete any previously downloaded cache. 25 | * When on a VPS, rebulding from the above command is the best option as it will clear previous caches. 26 | 27 | ### Notes and ToDo. 28 | ------------------- 29 | * On Heroku, the tool may look like its not doing anything. Please wait until the download completes and then you will see all the logs. 30 | 31 | * I wanted the code to directly save contents to the mega drive rather than download and upload, But due to the limitations of finding something for Mega to mount the drives, I could not do it. 32 | * You're most welcome if you can add this feature. 33 | 34 | * I tried Alpine image but it creates some issue with pip, so I discarded it. 35 | 36 | * This is like in a beta stage for now. It may have issues. If you find any, submit an issue. 37 | -------------------------------------------------------------------------------- /driver.py: -------------------------------------------------------------------------------- 1 | import libtorrent as lt 2 | import time 3 | import os 4 | 5 | from os import walk 6 | from mega import Mega 7 | from datetime import datetime 8 | 9 | email = os.environ['EMAIL'] 10 | password = os.environ['PASS'] 11 | magnet = os.environ['MAGNET'] 12 | 13 | class Driver(): 14 | def run(): 15 | mega = Mega() 16 | try: 17 | mClient = mega.login(email, password) 18 | except Exception: 19 | print("If running locally, try if you can ping 'www.mega.co.nz'. if not, use cloudflare dns 1.1.1.1 or any vpn. Else, check your login credentials.") 20 | return 21 | print(f"Transfer Quota: {mClient.get_quota()} KB") 22 | storage = mClient.get_storage_space() 23 | storage_left = int(storage['total']) - int(storage['used']) 24 | storage_left = storage_left / (1073741824) 25 | print(f"Storage Left: {storage_left}GB") 26 | 27 | client = lt.session({'listen_interfaces': '0.0.0.0:6881'}) 28 | minfo = lt.parse_magnet_uri(magnet) 29 | handle = client.add_torrent(minfo) 30 | begin = time.time() 31 | print(datetime.now()) 32 | print("Downloading Metadata...") 33 | while not handle.has_metadata(): 34 | time.sleep(1) 35 | print("Metadata downloaded. Starting Torrent Downloading...") 36 | 37 | mClient.create_folder(f'{handle.name()}') 38 | 39 | print(f"Starting {handle.name()}.") 40 | s = handle.status() 41 | while not s.is_seeding: 42 | s = handle.status() 43 | state_str = ['queued', 'checking', 'downloading metadata', 'downloading', 'finished', 'seeding', 'allocating'] 44 | try: 45 | print ('%.2f%% complete (down: %.1f kb/s up: %.1f kB/s peers: %d) %s ' % \ 46 | (s.progress * 100, s.download_rate / 1000, s.upload_rate / 1000, \ 47 | s.num_peers, state_str[s.state])) 48 | except IndexError: 49 | pass 50 | time.sleep(5) 51 | 52 | end = time.time() 53 | print(f"{handle.name()} completed...") 54 | print("Elapsed Time: ",int((end-begin)//60),"min :", int((end-begin)%60), "sec") 55 | 56 | print(datetime.now()) 57 | f = [] 58 | for (dirpath, dirnames, filenames) in walk(f'{handle.name()}'): 59 | f.extend(filenames) 60 | break 61 | folder = mClient.find(f'{handle.name()}') 62 | print("Uploading to Mega...") 63 | 64 | for i in range(0,len(f)): 65 | print(f"Uploading {f[i]}.") 66 | mClient.upload(f'{handle.name()}/{f[i]}', folder[0]) 67 | 68 | print("Upload Completed...\nHappy Torrenting...") 69 | exit() 70 | if __name__ == '__main__': 71 | Driver.run() -------------------------------------------------------------------------------- /heroku.yml: -------------------------------------------------------------------------------- 1 | build: 2 | docker: 3 | worker: Dockerfile 4 | config: 5 | EMAIL: "${EMAIL}" 6 | PASS: "${PASS}" 7 | MAGNET: "${MAGNET}" -------------------------------------------------------------------------------- /requirements.txt: -------------------------------------------------------------------------------- 1 | mega.py 2 | lbry-libtorrent --------------------------------------------------------------------------------