-
Notifications
You must be signed in to change notification settings - Fork 30
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
128 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
#!/usr/bin/env python | ||
from torrentool.cli import main | ||
|
||
|
||
if __name__ == '__main__': | ||
main() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,98 @@ | ||
import click | ||
from os import path, getcwd | ||
from sys import exit | ||
|
||
from torrentool import VERSION | ||
from torrentool.api import Torrent | ||
|
||
|
||
@click.group() | ||
@click.version_option(version='.'.join(map(str, VERSION))) | ||
def start(): | ||
"""Torrentool command line utilities.""" | ||
|
||
|
||
@start.group() | ||
def torrent(): | ||
"""Torrent-related commands.""" | ||
|
||
|
||
@torrent.command() | ||
@click.argument('source') | ||
@click.option('--dest', default=None, help='Destination path to put .torrent file into. Default: current directory.') | ||
@click.option('--tracker', default=None, help='Tracker announce URL (multiple comma-separated values supported).') | ||
@click.option('--open_trackers', default=False, is_flag=True, help='Add open trackers announce URLs.') | ||
@click.option('--comment', default=None, help='Arbitrary comment.') | ||
def create(source, dest, tracker, open_trackers, comment): | ||
"""Create torrent file from a single file or a directory.""" | ||
|
||
def check_path(fpath): | ||
fpath = path.abspath(fpath) | ||
if not path.exists(fpath): | ||
click.secho('Path is not found: %s' % fpath, fg='red', err=True) | ||
exit(1) | ||
return fpath | ||
|
||
if not dest: | ||
dest = getcwd() | ||
|
||
source = check_path(source) | ||
source_title = path.basename(source).replace('.', '_').replace(' ', '_') | ||
|
||
dest = check_path(dest) | ||
dest = '%s.torrent' % path.join(dest, source_title) | ||
|
||
click.secho('Creating torrent from %s ...' % source) | ||
|
||
my_torrent = Torrent.create_from(source) | ||
|
||
if comment: | ||
my_torrent.comment = comment | ||
|
||
urls = [] | ||
|
||
if tracker: | ||
urls = tracker.split(',') | ||
|
||
if open_trackers: | ||
urls.extend(get_open_trackers()) | ||
|
||
if urls: | ||
my_torrent.announce_urls = urls | ||
|
||
my_torrent.to_file(dest) | ||
|
||
click.secho('Torrent file created: %s' % dest, fg='green') | ||
|
||
|
||
def get_open_trackers(): | ||
"""Returns open trackers announce URLs list from remote repo or local backup.""" | ||
|
||
ourl = 'https://raw.githubusercontent.com/idlesign/torrentool/master/torrentool/repo' | ||
ofile = 'open_trackers.ini' | ||
|
||
click.secho('Fetching an up-to-date open tracker list ...') | ||
|
||
try: | ||
import requests | ||
|
||
response = requests.get('%s/%s' % (ourl, ofile), timeout=3) | ||
|
||
response.raise_for_status() | ||
open_trackers = response.text.splitlines() | ||
|
||
except (ImportError, requests.RequestException) as e: | ||
|
||
if isinstance(e, ImportError): | ||
click.secho('`requests` package is unavailable.', fg='red', err=True) | ||
|
||
click.secho('Failed. Using built-in open tracker list.', fg='red', err=True) | ||
|
||
with open(path.join(path.dirname(__file__), 'repo', ofile)) as f: | ||
open_trackers = map(str.strip, f.readlines()) | ||
|
||
return open_trackers | ||
|
||
|
||
def main(): | ||
start(obj={}) |