def best_candidate(self): "Return the best torrent for the current episode" torrents = self.hits.torrents(self.hits.current) # Discard failed download torrents = [t for t in torrents if t.get('failed_download', 0) < 3 and t.satisfy(self.condition)] if torrents: for rule in self.rules: filtered_torrents = rule.filter_sort(self, torrents) if filtered_torrents: log.info("'%s' (%s): %d torrents" % (self.name, self.hits.current, len(filtered_torrents))) log.info(" - Best: %(name)s %(seeds)s/%(leechs)s from %(source)s\n - %(url)s" % filtered_torrents[0].__dict__) return filtered_torrents[0] log.verbose("'%s' (%s): No torrent" % (self.name, self.hits.current)) return None
def search_by_episode(self, episode): "Search all torrents corresponding to this episode" torrents = set() for source in self.sources: try: log.verbose("Searching for '%s' (%s) on %s" % (self.name, episode, source.name)) for t in source.search(self): if not t.satisfy(self.condition): continue t = self.is_hit(t, episode) if t: torrents.add(t) except Exception as e: print "Error while searching for (%s, %s, %s):" % (self.name, episode, source.name), type(e), e return torrents
def search(self): "Search torrents and update all hits corresponding to the current episode" self.update_airdates() airdate = self.get_airdate(self.hits.current) if not airdate: log.verbose("%s (%s) doesn't have an air date" % (self.name, self.hits.current)) # Try to determine if there is still an episode which airs after episode, airdate = self.get_episodes_after(self.hits.current) if episode: self.hits.current = episode if airdate and airdate >= date.today(): log.verbose("%s (%s) will air on %s" % (self.name, self.hits.current, airdate)) else: torrents = self.search_by_episode(self.hits.current) log.log(0 if len(torrents) else 1, "%d torrent(s) found for '%s' (%s)" % (len(torrents), self.name, self.hits.current)) if torrents: torrents = [t.merge(self.hits.torrent(self.hits.current, t)) for t in torrents] self.hits.torrents(self.hits.current, torrents) self.hits.save()
def update_airdates(self): "Update airdates" airdates = dict(self.airdates) airdates.update(self.hits.airdates) if self.hits.current not in airdates: log.verbose("No airdate for '%s' (%s)" % (self.name, self.hits.current)) # TODO too crowded if isinstance(self.hits.current, SeasonEpisodeNumbering): log.verbose("Updating airdates for '%s'" % self.name) self.hits.airdates = dict(get_next_episodes(self.name, self.hits.current)) elif isinstance(self.hits.current, DateNumbering): log.verbose("Updating airdates for '%s'" % self.name) self.hits.airdates = dict(get_all_episodes_after_airdate(self.name, self.hits.current))