def find_torrents_task_to_download(client, links): tasks = client.read_all_tasks() hashes = set(t['bt_hash'].lower() for t in tasks if t['type'] == 'bt') link_hashes = [] for link in links: if re.match(r'^(?:bt://)?([a-fA-F0-9]{40})$', link): info_hash = link[-40:].lower() if info_hash not in hashes: print 'Adding bt task', link client.add_torrent_task_by_info_hash(info_hash) link_hashes.append(info_hash) elif re.match(r'http://', link): print 'Downloading torrent file from', link torrent = urllib2.urlopen(link, timeout=60).read() assert torrent.startswith( 'd8:announce' ), 'Probably not a valid torrent file [%s...]' % repr(torrent[:11]) info_hash = lixian_hash_bt.info_hash_from_content(torrent) if info_hash not in hashes: print 'Adding bt task', link client.add_torrent_task_by_content(torrent, os.path.basename(link)) link_hashes.append(info_hash) elif os.path.exists(link): with open(link, 'rb') as stream: torrent = stream.read() assert torrent.startswith( 'd8:announce' ), 'Probably not a valid torrent file [%s...]' % repr(torrent[:11]) info_hash = lixian_hash_bt.info_hash_from_content(torrent) if info_hash not in hashes: print 'Adding bt task', link client.add_torrent_task_by_content(torrent, os.path.basename(link)) link_hashes.append(info_hash) else: raise NotImplementedError('Unknown torrent ' + link) all_tasks = client.read_all_tasks() tasks = [] for h in link_hashes: for t in all_tasks: if t['bt_hash'].lower() == h.lower(): tasks.append(t) break else: raise NotImplementedError('not task found') return tasks
def find_torrents_task_to_download(client, links): tasks = client.read_all_tasks() hashes = set(t["bt_hash"].lower() for t in tasks if t["type"] == "bt") link_hashes = [] for link in links: if re.match(r"^(?:bt://)?([a-fA-F0-9]{40})$", link): info_hash = link[-40:].lower() if info_hash not in hashes: print "Adding bt task", link client.add_torrent_task_by_info_hash(info_hash) link_hashes.append(info_hash) elif re.match(r"http://", link): print "Downloading torrent file from", link torrent = urllib2.urlopen(link, timeout=60).read() assert torrent.startswith("d8:announce") or torrent.startswith( "d13:announce-list" ), "Probably not a valid torrent file [%s...]" % repr(torrent[:17]) info_hash = lixian_hash_bt.info_hash_from_content(torrent) if info_hash not in hashes: print "Adding bt task", link client.add_torrent_task_by_content(torrent, os.path.basename(link)) link_hashes.append(info_hash) elif os.path.exists(link): with open(link, "rb") as stream: torrent = stream.read() assert torrent.startswith("d8:announce") or torrent.startswith( "d13:announce-list" ), "Probably not a valid torrent file [%s...]" % repr(torrent[:17]) info_hash = lixian_hash_bt.info_hash_from_content(torrent) if info_hash not in hashes: print "Adding bt task", link client.add_torrent_task_by_content(torrent, os.path.basename(link)) link_hashes.append(info_hash) else: raise NotImplementedError("Unknown torrent " + link) all_tasks = client.read_all_tasks() tasks = [] for h in link_hashes: for t in all_tasks: if t["bt_hash"].lower() == h.lower(): tasks.append(t) break else: raise NotImplementedError("not task found") return tasks
def find_torrents_task_to_download(client, links): tasks = client.read_all_tasks() hashes = set(t['bt_hash'].lower() for t in tasks if t['type'] == 'bt') link_hashes = [] for link in links: if re.match(r'^(?:bt://)?([a-fA-F0-9]{40})$', link): info_hash = link[-40:].lower() if info_hash not in hashes: print 'Adding bt task', link client.add_torrent_task_by_info_hash(info_hash) link_hashes.append(info_hash) elif re.match(r'http://', link): print 'Downloading torrent file from', link torrent = urllib2.urlopen(link, timeout=60).read() assert torrent.startswith('d8:announce'), 'Probably not a valid torrent file [%s...]' % repr(torrent[:11]) info_hash = lixian_hash_bt.info_hash_from_content(torrent) if info_hash not in hashes: print 'Adding bt task', link client.add_torrent_task_by_content(torrent, os.path.basename(link)) link_hashes.append(info_hash) elif os.path.exists(link): with open(link, 'rb') as stream: torrent = stream.read() assert torrent.startswith('d8:announce'), 'Probably not a valid torrent file [%s...]' % repr(torrent[:11]) info_hash = lixian_hash_bt.info_hash_from_content(torrent) if info_hash not in hashes: print 'Adding bt task', link client.add_torrent_task_by_content(torrent, os.path.basename(link)) link_hashes.append(info_hash) else: raise NotImplementedError('Unknown torrent '+link) all_tasks = client.read_all_tasks() tasks = [] for h in link_hashes: for t in all_tasks: if t['bt_hash'].lower() == h.lower(): tasks.append(t) break else: raise NotImplementedError('not task found') return tasks
def __init__(self, base, url, torrent): super(BtUrlQuery, self).__init__(base) self.url = url self.torrent = torrent self.hash = lixian_hash_bt.info_hash_from_content(self.torrent) self.task = self.base.find_task_by_hash(self.hash)