예제 #1
0
def make_meta_file(loc, url, params=None, flag=None,
                   progress=lambda x: None, progress_percent=True):
    """Make a single .torrent file for a given location"""
    if params is None:
        params = {}
    if flag is None:
        flag = threading.Event()

    tree = BTTree(loc, [])

    # Extract target from parameters
    if 'target' not in params or params['target'] == '':
        fname, ext = os.path.split(loc)
        if ext == '':
            target = fname + '.torrent'
        else:
            target = os.path.join(fname, ext + '.torrent')
        params['target'] = target

    info = tree.makeInfo(flag=flag, progress=progress,
                         progress_percent=progress_percent, **params)

    if flag is not None and flag.isSet():
        return

    metainfo = MetaInfo(announce=url, info=info, **params)
    metainfo.write(params['target'])
예제 #2
0
def make_meta_file(loc,
                   url,
                   params=None,
                   flag=None,
                   progress=lambda x: None,
                   progress_percent=True):
    """Make a single .torrent file for a given location"""
    if params is None:
        params = {}
    if flag is None:
        flag = threading.Event()

    tree = BTTree(loc, [])

    # Extract target from parameters
    if 'target' not in params or params['target'] == '':
        fname, ext = os.path.split(loc)
        if ext == '':
            target = fname + '.torrent'
        else:
            target = os.path.join(fname, ext + '.torrent')
        params['target'] = target

    info = tree.makeInfo(flag=flag,
                         progress=progress,
                         progress_percent=progress_percent,
                         **params)

    if flag is not None and flag.isSet():
        return

    metainfo = MetaInfo(announce=url, info=info, **params)
    metainfo.write(params['target'])
예제 #3
0
def make_meta_file(loc, url, params = {}, flag = Event(),
                   progress = lambda x: None, progress_percent = True):
    tree = BTTree(loc, [])

    # Extract target from parameters
    if 'target' not in params or params['target'] == '':
        a, b = os.path.split(loc)
        if b == '':
            target = a + '.torrent'
        else:
            target = os.path.join(a, b + '.torrent')
        params['target'] = target

    info = tree.makeInfo(   flag = flag,
                            progress = progress,
                            progress_percent = progress_percent,
                            **params)

    if flag.isSet():
        return

    info.write(tracker = url, **params)
예제 #4
0
def completedir(directory,
                url,
                params=None,
                flag=None,
                progress=lambda x: None,
                filestat=lambda x: None):
    """Make a .torrent file for each entry in a directory"""
    if params is None:
        params = {}
    if flag is None:
        flag = threading.Event()

    files = sorted(os.listdir(directory))
    ext = '.torrent'

    togen = [
        os.path.join(directory, fname) for fname in files
        if (fname + ext) not in files and not fname.endswith(ext)
    ]

    trees = [BTTree(loc, []) for loc in togen]

    def subprog(update,
                subtotal=[0],
                total=sum(tree.size for tree in trees),
                progress=progress):
        """Aggregate progress callback
        Uses static subtotal to track across files"""
        subtotal[0] += update
        progress(float(subtotal[0]) / total)

    for fname in togen:
        filestat(fname)
        try:
            base = os.path.basename(fname)
            if base not in ignore and base[0] != '.':
                subparams = params.copy()
                if 'target' in params and params['target'] != '':
                    subparams['target'] = os.path.join(params['target'],
                                                       base + ext)
                make_meta_file(fname,
                               url,
                               subparams,
                               flag,
                               progress=subprog,
                               progress_percent=False)
        except ValueError:
            print_exc()