Skip to content

Commit

Permalink
Add option to stop after download in CLI
Browse files Browse the repository at this point in the history
  • Loading branch information
Likhithsai2580 authored and iw4p committed Jan 2, 2025
1 parent 42c4bbb commit 987f62e
Show file tree
Hide file tree
Showing 5 changed files with 17 additions and 9 deletions.
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -90,11 +90,12 @@ $ torrentp --link 'test.torrent'
| --download_speed | Download speed with a specific number (kB/s). Default: 0, means unlimited speed | ```int``` |
| --upload_speed | Upload speed with a specific number (kB/s). Default: 0, means unlimited speed | ```int``` |
| --save_path | Path to save the file, default: '.' | ```str``` |
| --stop_after_download | Stop the download immediately after completion without seeding | ```flag``` |
| --help |Show this message and exit | |

Example with all commands:
```sh
$ torrentp --link 'magnet:...' --download_speed 100 --upload_speed 50 --save_path '.'
$ torrentp --link 'magnet:...' --download_speed 100 --upload_speed 50 --save_path '.' --stop_after_download
```

### To do list
Expand Down
2 changes: 1 addition & 1 deletion torrentp/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
from .downloader import Downloader
from .session import Session

__version__ = "0.2.2"
__version__ = "0.2.3"
__author__ = 'Nima Akbarzade'
__author_email__ = "[email protected]"
__license__ = "BSD 2-clause"
Expand Down
5 changes: 3 additions & 2 deletions torrentp/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,10 @@ async def handle_input(torrent_file):
@click.option('--download_speed', default=0, help='Download speed with a specific number (kB/s). Default: 0, means unlimited speed', type=int)
@click.option('--upload_speed', default=0, help='Upload speed with a specific number (kB/s). Default: 0, means unlimited speed', type=int)
@click.option('--save_path', default='.', help="Path to save the file, default: '.' ", type=str)
async def run_cli(link, download_speed, upload_speed, save_path):
@click.option('--stop_after_download', is_flag=True, help="Stop the download immediately after completion without seeding")
async def run_cli(link, download_speed, upload_speed, save_path, stop_after_download):
try:
torrent_file = TorrentDownloader(link, save_path)
torrent_file = TorrentDownloader(link, save_path, stop_after_download=stop_after_download)

input_task = asyncio.create_task(handle_input(torrent_file))
download_task = asyncio.create_task(torrent_file.start_download(download_speed=download_speed, upload_speed=upload_speed))
Expand Down
9 changes: 7 additions & 2 deletions torrentp/downloader.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import time

class Downloader:
def __init__(self, session, torrent_info, save_path, libtorrent, is_magnet):
def __init__(self, session, torrent_info, save_path, libtorrent, is_magnet, stop_after_download=False):
self._session = session
self._torrent_info = torrent_info
self._save_path = save_path
Expand All @@ -16,6 +16,7 @@ def __init__(self, session, torrent_info, save_path, libtorrent, is_magnet):
self._add_torrent_params = None
self._is_magnet = is_magnet
self._paused = False
self._stop_after_download = stop_after_download

def status(self):
if not self._is_magnet:
Expand Down Expand Up @@ -44,7 +45,11 @@ async def download(self):
sys.stdout.flush()

await asyncio.sleep(1)
print('\033[92m' + "\nDownloaded successfully." + '\033[0m')

if self._stop_after_download:
self.stop()
else:
print('\033[92m' + "\nDownloaded successfully." + '\033[0m')

def _get_status_progress(self, s):
_percentage = s.progress * 100
Expand Down
7 changes: 4 additions & 3 deletions torrentp/torrent_downloader.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@


class TorrentDownloader:
def __init__(self, file_path, save_path, port=6881):
def __init__(self, file_path, save_path, port=6881, stop_after_download=False):
self._file_path = file_path
self._save_path = save_path
self._port = port # Default port is 6881
Expand All @@ -15,21 +15,22 @@ def __init__(self, file_path, save_path, port=6881):
self._file = None
self._add_torrent_params = None
self._session = Session(self._lt, port=self._port) # Pass port to Session
self._stop_after_download = stop_after_download

async def start_download(self, download_speed=0, upload_speed=0):
if self._file_path.startswith('magnet:'):
self._add_torrent_params = self._lt.parse_magnet_uri(self._file_path)
self._add_torrent_params.save_path = self._save_path
self._downloader = Downloader(
session=self._session(), torrent_info=self._add_torrent_params,
save_path=self._save_path, libtorrent=lt, is_magnet=True
save_path=self._save_path, libtorrent=lt, is_magnet=True, stop_after_download=self._stop_after_download
)

else:
self._torrent_info = TorrentInfo(self._file_path, self._lt)
self._downloader = Downloader(
session=self._session(), torrent_info=self._torrent_info(),
save_path=self._save_path, libtorrent=None, is_magnet=False
save_path=self._save_path, libtorrent=None, is_magnet=False, stop_after_download=self._stop_after_download
)

self._session.set_download_limit(download_speed)
Expand Down

0 comments on commit 987f62e

Please sign in to comment.