def on_get_session_state(torrent_ids): """Gets called with a list of torrent_ids loaded in the deluge session. Adds new torrents and modifies the settings for ones already in the session.""" dlist = [] # add the torrents for entry in feed.accepted: def add_entry(entry, opts): """Adds an entry to the deluge session""" magnet, filedump = None, None if entry.get('url', '').startswith('magnet:'): magnet = entry['url'] else: if not os.path.exists(entry['file']): feed.fail(entry, 'Downloaded temp file \'%s\' doesn\'t exist!' % entry['file']) del(entry['file']) return try: f = open(entry['file'], 'rb') filedump = base64.encodestring(f.read()) finally: f.close() log.verbose('Adding %s to deluge.' % entry['title']) if magnet: return client.core.add_torrent_magnet(magnet, opts) else: return client.core.add_torrent_file(entry['title'], filedump, opts) # Generate deluge options dict for torrent add add_opts = {} try: path = entry.render(entry.get('path', config['path'])) if path: add_opts['download_location'] = make_valid_path(os.path.expanduser(path)) except RenderError, e: log.error('Could not set path for %s: %s' % (entry['title'], e)) for fopt, dopt in self.options.iteritems(): value = entry.get(fopt, config.get(fopt)) if value is not None: add_opts[dopt] = value if fopt == 'ratio': add_opts['stop_at_ratio'] = True # Make another set of options, that get set after the torrent has been added modify_opts = {'label': format_label(entry.get('label', config['label'])), 'queuetotop': entry.get('queuetotop', config.get('queuetotop')), 'main_file_only': entry.get('main_file_only', config.get('main_file_only', False))} try: movedone = entry.render(entry.get('movedone', config['movedone'])) modify_opts['movedone'] = make_valid_path(os.path.expanduser(movedone)) except RenderError, e: log.error('Error setting movedone for %s: %s' % (entry['title'], e))
def on_get_session_state(torrent_ids): """Gets called with a list of torrent_ids loaded in the deluge session. Adds new torrents and modifies the settings for ones already in the session.""" dlist = [] # add the torrents for entry in feed.accepted: def add_entry(entry, opts): """Adds an entry to the deluge session""" magnet, filedump = None, None if entry.get('url', '').startswith('magnet:'): magnet = entry['url'] else: if not os.path.exists(entry['file']): feed.fail(entry, 'Downloaded temp file \'%s\' doesn\'t exist!' % entry['file']) del(entry['file']) return try: f = open(entry['file'], 'rb') filedump = base64.encodestring(f.read()) finally: f.close() log.debug('Adding %s to deluge.' % entry['title']) if magnet: return client.core.add_torrent_magnet(magnet, opts) else: return client.core.add_torrent_file(entry['title'], filedump, opts) # Generate deluge options dict for torrent add path = replace_from_entry(entry.get('path', config['path']), entry, 'path', log.error) add_opts = {} if path: add_opts['download_location'] = make_valid_path(os.path.expanduser(path)) for fopt, dopt in self.options.iteritems(): value = entry.get(fopt, config.get(fopt)) if value is not None: add_opts[dopt] = value if fopt == 'ratio': add_opts['stop_at_ratio'] = True # Make another set of options, that get set after the torrent has been added content_filename = entry.get('content_filename', config.get('content_filename', '')) movedone = replace_from_entry(entry.get('movedone', config['movedone']), entry, 'movedone', log.error) modify_opts = {'movedone': make_valid_path(os.path.expanduser(movedone)), 'label': format_label(entry.get('label', config['label'])), 'queuetotop': entry.get('queuetotop', config.get('queuetotop', None)), 'content_filename': replace_from_entry(content_filename, entry, 'content_filename', log.error), 'main_file_only': entry.get('main_file_only', config.get('main_file_only', False))} torrent_id = entry.get('deluge_id') or entry.get('torrent_info_hash') torrent_id = torrent_id and torrent_id.lower() if torrent_id in torrent_ids: log.info('%s is already loaded in deluge, setting options' % entry['title']) # Entry has a deluge id, verify the torrent is still in the deluge session and apply options # Since this is already loaded in deluge, we may also need to change the path modify_opts['path'] = add_opts.pop('download_location', None) dlist.extend([set_torrent_options(torrent_id, entry, modify_opts), client.core.set_torrent_options([torrent_id], add_opts)]) else: dlist.append(add_entry(entry, add_opts).addCallbacks(set_torrent_options, on_fail, callbackArgs=(entry, modify_opts), errbackArgs=(feed, entry))) return defer.DeferredList(dlist)