Skip to content

Commit

Permalink
Implemented basic CLI.
Browse files Browse the repository at this point in the history
  • Loading branch information
idlesign committed Aug 30, 2016
1 parent 77a2ca8 commit 2c16203
Show file tree
Hide file tree
Showing 4 changed files with 128 additions and 0 deletions.
2 changes: 2 additions & 0 deletions MANIFEST.in
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ recursive-include docs .py

recursive-include bin *

recursive-include torrentool/repo *

recursive-include tests *

recursive-exclude * __pycache__
Expand Down
22 changes: 22 additions & 0 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,32 @@ Works on Python 2.7+ and 3.3+.

Includes:

* Command line interface (requires ``click`` package to be installed)
* Torrent utils (file creation, read and modification)
* Bencoding utils (decoder, encoder)


Using CLI
~~~~~~~~~

.. code-block:: bash
; Make .torrent out of `video.mkv`
$ torrentool torrent create /home/my/files_here/video.mkv
; Make .torrent out of entire `/home/my/files_here` dir,
; and put some open trackers announce URLs into it, so it is ready to share.
$ torrentool torrent create /home/my/files_here --open_trackers
Use command line ``--help`` switch to know more.

.. note:: Some commands require ``requests`` package to be installed.


From your Python code
~~~~~~~~~~~~~~~~~~~~~

.. code-block:: python
from torrentool.api import Torrent
Expand Down
6 changes: 6 additions & 0 deletions bin/torrentool
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()
98 changes: 98 additions & 0 deletions torrentool/cli.py
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={})

0 comments on commit 2c16203

Please sign in to comment.