def search_episode_torrent(self, episode): '''Find a torrent for the provided episode, returns the Torrent object''' season = episode.season series = season.series # Run search engine query search_string = "s%02de%02d" % (season.number, episode.number) torrent_list = self.search_torrent_by_string(wall.helpers.normalize_text(series.name), search_string) # Isolate the right torrent torrent = None for torrent_result in torrent_list: if torrent_result.hash is None or torrent_result.seeds is None or torrent_result.seeds <= 0: log.info("Discarded result for lack of seeds or hash: %s", torrent_result) else: torrent = torrent_result break log.info("Episode lookup for '%s' gave torrent %s", search_string, torrent) if torrent is None: torrent = Torrent() torrent.status = 'Error' try: # Check if this torrent is already in the database existing_torrent = Torrent.objects.get(hash=torrent.hash) torrent = existing_torrent except Torrent.DoesNotExist: torrent = self.update_torrent_with_tracker_list(torrent) torrent.save() return torrent