예제 #1
0
    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
예제 #2
0
    def get_torrent_from_result(self, result):
        '''Converts a result from the current engine to a Torrent object'''

        torrent = Torrent()

        torrent.name = wall.helpers.sane_text(result.title)
        torrent.hash = self.get_result_description_item('hash', result.description)
        torrent.seeds = self.get_result_description_item('seeds', result.description)
        torrent.peers = self.get_result_description_item('peers', result.description)
        
        return torrent
예제 #3
0
    def get_torrent_info(self, torrent_db):
        '''Returns a Torrent() object containing miscanealous info about the torrent
        State is either: 'Downloading', 'Completed' or 'Error'.'''

        import json
        from datetime import timedelta

        torrent_bt = Torrent()
        handle = self.get_handle_for_hash(torrent_db.hash)
        status = handle.status()

        # Status
        log.debug('Built torrent info for BT hash %s (status = %s, error = %s)', torrent_db.hash, status.state, status.error)
        if 'seeding' in str(status.state):
            torrent_bt.status = 'Completed'
        elif status.error:
            torrent_bt.status = 'Error'
        else:
            torrent_bt.status = 'Downloading'

        # Metadata
        torrent_bt.has_metadata = handle.has_metadata()
        if not torrent_bt.has_metadata:
            return torrent_bt

        info = handle.get_torrent_info()
        torrent_bt.name = info.name()
        torrent_bt.progress = status.progress
        torrent_bt.download_speed = "%.3f MB/s" % (status.download_rate/(1024*1024))
        torrent_bt.upload_speed = "%.3f MB/s" % (status.upload_rate/(1024*1024))
        torrent_bt.active_time = status.active_time
        torrent_bt.seeds = status.list_seeds
        torrent_bt.peers = status.list_peers

        # ETA
        size_left = info.total_size() - status.total_done
        if status.download_rate > 0:
            torrent_bt.eta = size_left / status.download_rate
        else:
            torrent_bt.eta = None

        # Files
        file_list = list()
        for res_file in info.files():
            file_list.append({'path': unicode(res_file.path, 'utf-8'), 'size': res_file.size})
        torrent_bt.file_list = json.dumps(file_list)

        return torrent_bt