'Adds accepted entries to transmission'

def add_to_transmission(self, cli, task, config):

    for entry in task.accepted:
        if task.options.test:
            log.info((u'Would    add    %s    to    transmission' % entry[u'url']))
            continue
        options = self._make_torrent_options_dict(config, entry)
        downloaded = (not entry[u'url'].startswith(u'magnet:'))
        if (downloaded and (u'file' not in entry)):
            entry.fail(u'file    missing?')
            continue
        if (downloaded and (not os.path.exists(entry[u'file']))):
            tmp_path = os.path.join(task.manager.config_base, u'temp')
            log.debug(u'entry:    %s', entry)
            log.debug(u'temp:    %s', u',    '.join(os.listdir(tmp_path)))
            entry.fail((u"Downloaded    temp    file    '%s'    doesn't    exist!?" % entry[u'file']))
            continue
        try:
            if downloaded:
                with open(entry[u'file'], u'rb') as f:
                    filedump = base64.b64encode(f.read()).decode(u'utf-8')
                r = cli.add_torrent(filedump, 30, **options[u'add'])
            else:
                options[u'add'][u'paused'] = False
                r = cli.add_torrent(entry[u'url'], timeout=30, **options[u'add'])
            log.info(u'"%s"    torrent    added    to    transmission', entry[u'title'])
            total_size = cli.get_torrent(r.id, [u'id', u'totalSize']).totalSize
            def _filter_list(list):
                for item in list:
                    if (not isinstance(item, basestring)):
                        list.remove(item)
                return list
            def _find_matches(name, list):
                for mask in list:
                    if fnmatch(name, mask):
                        return True
                return False
            def _wait_for_files(cli, r, timeout):
                from time import sleep
                while (timeout > 0):
                    sleep(1)
                    fl = cli.get_files(r.id)
                    if (len(fl[r.id]) > 0):
                        return fl
                    else:
                        timeout -= 1
                return fl
            skip_files = False
            if (u'skip_files' in options[u'post']):
                skip_files = True
                options[u'post'][u'skip_files'] = _filter_list(options[u'post'][u'skip_files'])
            main_id = None
            find_main_file = (options[u'post'].get(u'main_file_only') or (u'content_filename' in options[u'post']))
            if (find_main_file or skip_files):
                fl = cli.get_files(r.id)
                if ((u'magnetization_timeout' in options[u'post']) and (options[u'post'][u'magnetization_timeout'] > 0) and (not downloaded) and (len(fl[r.id]) == 0)):
                    log.debug(u'Waiting    %d    seconds    for    "%s"    to    magnetize', options[u'post'][u'magnetization_timeout'], entry[u'title'])
                    fl = _wait_for_files(cli, r, options[u'post'][u'magnetization_timeout'])
                    if (len(fl[r.id]) == 0):
                        log.warning(u'"%s"    did    not    magnetize    before    the    timeout    elapsed,    file    list    unavailable    for    processing.', entry[u'title'])
                    else:
                        total_size = cli.get_torrent(r.id, [u'id', u'totalSize']).totalSize
                dl_list = []
                skip_list = []
                main_list = []
                full_list = []
                ext_list = [u'*.srt', u'*.sub', u'*.idx', u'*.ssa', u'*.ass']
                main_ratio = config[u'main_file_ratio']
                if (u'main_file_ratio' in options[u'post']):
                    main_ratio = options[u'post'][u'main_file_ratio']
                if (u'include_files' in options[u'post']):
                    options[u'post'][u'include_files'] = _filter_list(options[u'post'][u'include_files'])
                for f in fl[r.id]:
                    full_list.append(f)
                    if (find_main_file and (fl[r.id][f][u'size'] > (total_size * main_ratio))):
                        main_id = f
                    if (u'include_files' in options[u'post']):
                        if _find_matches(fl[r.id][f][u'name'], options[u'post'][u'include_files']):
                            dl_list.append(f)
                        elif (options[u'post'].get(u'include_subs') and _find_matches(fl[r.id][f][u'name'], ext_list)):
                            dl_list.append(f)
                    if skip_files:
                        if _find_matches(fl[r.id][f][u'name'], options[u'post'][u'skip_files']):
                            skip_list.append(f)
                if (main_id is not None):
                    if options[u'post'].get(u'rename_like_files'):
                        for f in fl[r.id]:
                            fs = os.path.splitext(fl[r.id][f][u'name'])
                            if (fs[0] == os.path.splitext(fl[r.id][main_id][u'name'])[0]):
                                main_list.append(f)
                    else:
                        main_list = [main_id]
                    if (main_id not in dl_list):
                        dl_list.append(main_id)
                elif find_main_file:
                    log.warning(u'No    files    in    "%s"    are    >    %d%%    of    content    size,    no    files    renamed.', entry[u'title'], (main_ratio * 100))
                if ((u'content_filename' in options[u'post']) and (main_id is not None)):
                    if (u'download_dir' not in options[u'add']):
                        download_dir = cli.get_session().download_dir
                    else:
                        download_dir = options[u'add'][u'download_dir']
                    file_ext = os.path.splitext(fl[r.id][main_id][u'name'])[1]
                    file_path = os.path.dirname(os.path.join(download_dir, fl[r.id][main_id][u'name']))
                    filename = options[u'post'][u'content_filename']
                    if ((config[u'host'] == u'localhost') or (config[u'host'] == u'127.0.0.1')):
                        counter = 1
                        while os.path.exists(os.path.join(file_path, (filename + file_ext))):
                            filename = (u'%s(%s)' % (options[u'post'][u'content_filename'], counter))
                            counter += 1
                    else:
                        log.debug(u'Cannot    ensure    content_filename    is    unique    when    adding    to    a    remote    transmission    daemon.')
                    for index in main_list:
                        file_ext = os.path.splitext(fl[r.id][index][u'name'])[1]
                        log.debug((u'File    %s    renamed    to    %s' % (fl[r.id][index][u'name'], (filename + file_ext))))
                        try:
                            cli.rename_torrent_path(r.id, fl[r.id][index][u'name'], os.path.basename(str(pathscrub((filename + file_ext)))))
                        except TransmissionError:
                            log.error(u'content_filename    only    supported    with    transmission    2.8+')
                if (options[u'post'].get(u'main_file_only') and (main_id is not None)):
                    options[u'change'][u'files_unwanted'] = [x for x in full_list if (x not in dl_list)]
                    options[u'change'][u'files_wanted'] = dl_list
                    log.debug(u'Downloading    %s    of    %s    files    in    torrent.', len(options[u'change'][u'files_wanted']), len(full_list))
                elif (((not options[u'post'].get(u'main_file_only')) or (main_id is None)) and skip_files):
                    if (len(skip_list) >= len(full_list)):
                        log.debug(u'skip_files    filter    would    cause    no    files    to    be    downloaded;    including    all    files    in    torrent.')
                    else:
                        options[u'change'][u'files_unwanted'] = skip_list
                        options[u'change'][u'files_wanted'] = [x for x in full_list if (x not in skip_list)]
                        log.debug(u'Downloading    %s    of    %s    files    in    torrent.', len(options[u'change'][u'files_wanted']), len(full_list))
            if list(options[u'change'].keys()):
                cli.change_torrent(r.id, 30, **options[u'change'])
            if (((u'paused' in options[u'post']) and (not options[u'post'][u'paused'])) or ((u'paused' not in options[u'post']) and cli.get_session().start_added_torrents)):
                cli.start_torrent(r.id)
            elif options[u'post'].get(u'paused'):
                log.debug(u'sleeping    5s    to    stop    the    torrent...')
                time.sleep(5)
                cli.stop_torrent(r.id)
                log.info(u'Torrent    "%s"    stopped    because    of    addpaused=yes', entry[u'title'])
        except TransmissionError as e:
            log.debug(u'TransmissionError', exc_info=True)
            log.debug(u'Failed    options    dict:    %s', options)
            msg = ((u'TransmissionError:    %s' % e.message) or u'N/A')
            log.error(msg)
            entry.fail(msg)
