Ejemplo n.º 1
0
    def prepare_files(self, path, files, force=False):
        """
        Prepare files for post-processing.

        Separate the Rar and Video files. -> self.video_files
            Extract the rar files. -> self.rar_content
            Collect new video files. -> self.video_in_rar
            List unwanted files -> self.unwanted_files
        :param path: Path to start looking for rar/video files.
        :param files: Array of files.
        """
        video_files = []
        rar_files = []
        for each_file in files:
            if helpers.is_media_file(each_file):
                video_files.append(each_file)
            elif helpers.is_rar_file(each_file):
                rar_files.append(each_file)

        rar_content = []
        video_in_rar = []
        if rar_files:
            rar_content = self.unrar(path, rar_files, force)
            files.extend(rar_content)
            video_in_rar = [
                each_file for each_file in rar_content
                if helpers.is_media_file(each_file)
            ]
            video_files.extend(video_in_rar)

        self.log_and_output('Post-processing files: {files}',
                            level=logging.DEBUG,
                            **{'files': files})
        self.log_and_output('Post-processing video files: {video_files}',
                            level=logging.DEBUG,
                            **{'video_files': video_files})

        if rar_content:
            self.log_and_output('Post-processing rar content: {rar_content}',
                                level=logging.DEBUG,
                                **{'rar_content': rar_content})
            self.log_and_output('Post-processing video in rar: {video_in_rar}',
                                level=logging.DEBUG,
                                **{'video_in_rar': video_in_rar})

        unwanted_files = [
            filename for filename in files if filename not in video_files
            and helpers.get_extension(filename) not in self.allowed_extensions
        ]
        if unwanted_files:
            self.log_and_output('Found unwanted files: {unwanted_files}',
                                level=logging.DEBUG,
                                **{'unwanted_files': unwanted_files})

        self.video_files = video_files
        self.rar_content = rar_content
        self.video_in_rar = video_in_rar
        self.unwanted_files = unwanted_files
Ejemplo n.º 2
0
    def prepare_files(self, path, files, force=False):
        """Prepare files for post-processing."""
        video_files = []
        rar_files = []
        for each_file in files:
            if helpers.is_media_file(each_file):
                video_files.append(each_file)
            elif helpers.is_rar_file(each_file):
                rar_files.append(each_file)

        rar_content = []
        video_in_rar = []
        if rar_files:
            rar_content = self.unrar(path, rar_files, force)
            files.extend(rar_content)
            video_in_rar = [
                each_file for each_file in rar_content
                if helpers.is_media_file(each_file)
            ]
            video_files.extend(video_in_rar)

        self._log('Post-processing files: {0}'.format(files), logger.DEBUG)
        self._log('Post-processing video files: {0}'.format(video_files),
                  logger.DEBUG)

        if rar_content:
            self._log('Post-processing rar content: {0}'.format(rar_content),
                      logger.DEBUG)
            self._log('Post-processing video in rar: {0}'.format(video_in_rar),
                      logger.DEBUG)

        unwanted_files = [
            filename for filename in files if filename not in video_files
            and helpers.get_extension(filename) not in self.allowed_extensions
        ]
        if unwanted_files:
            self._log('Found unwanted files: {0}'.format(unwanted_files),
                      logger.DEBUG)

        self.video_files = video_files
        self.rar_content = rar_content
        self.video_in_rar = video_in_rar
        self.unwanted_files = unwanted_files
Ejemplo n.º 3
0
    def prepare_files(self, path, files, force=False):
        """Prepare files for post-processing."""
        video_files = []
        rar_files = []
        for each_file in files:
            if helpers.is_media_file(each_file):
                video_files.append(each_file)
            elif helpers.is_rar_file(each_file):
                rar_files.append(each_file)

        rar_content = []
        video_in_rar = []
        if rar_files:
            rar_content = self.unrar(path, rar_files, force)
            files.extend(rar_content)
            video_in_rar = [each_file for each_file in rar_content if helpers.is_media_file(each_file)]
            video_files.extend(video_in_rar)

        self.log('Post-processing files: {0}'.format(files), logger.DEBUG)
        self.log('Post-processing video files: {0}'.format(video_files), logger.DEBUG)

        if rar_content:
            self.log('Post-processing rar content: {0}'.format(rar_content), logger.DEBUG)
            self.log('Post-processing video in rar: {0}'.format(video_in_rar), logger.DEBUG)

        unwanted_files = [filename
                          for filename in files
                          if filename not in video_files and
                          helpers.get_extension(filename) not in
                          self.allowed_extensions]
        if unwanted_files:
            self.log('Found unwanted files: {0}'.format(unwanted_files), logger.DEBUG)

        self.video_files = video_files
        self.rar_content = rar_content
        self.video_in_rar = video_in_rar
        self.unwanted_files = unwanted_files
Ejemplo n.º 4
0
    def remove_ratio_reached(self):
        """Remove all Medusa torrents that ratio was reached.

        It loops in all hashes returned from client and check if it is in the snatch history
        if its then it checks if we already processed media from the torrent (episode status `Downloaded`)
        If is a RARed torrent then we don't have a media file so we check if that hash is from an
        episode that has a `Downloaded` status

        0 = Torrent is stopped
        1 = Queued to check files
        2 = Checking files
        3 = Queued to download
        4 = Downloading
        5 = Queued to seed
        6 = Seeding

        isFinished = whether seeding finished (based on seed ratio)
        IsStalled =  Based on Tranmission setting "Transfer is stalled when inactive for"
        """
        log.info('Checking Transmission torrent status.')

        return_params = {
            'fields': ['name', 'hashString', 'percentDone', 'status',
                       'isStalled', 'errorString', 'seedRatioLimit',
                       'isFinished', 'uploadRatio', 'seedIdleLimit', 'files', 'activityDate']
        }

        post_data = json.dumps({'arguments': return_params, 'method': 'torrent-get'})

        if not self._request(method='post', data=post_data):
            log.debug('Could not connect to Transmission. Check logs')
            return

        try:
            returned_data = json.loads(self.response.content)
        except ValueError:
            log.warning('Unexpected data received from Transmission: {resp}',
                        {'resp': self.response.content})
            return

        if not returned_data['result'] == 'success':
            log.debug('Nothing in queue or error')
            return

        found_torrents = False
        for torrent in returned_data['arguments']['torrents']:

            # Check if that hash was sent by Medusa
            if not is_info_hash_in_history(str(torrent['hashString'])):
                continue
            found_torrents = True

            to_remove = False
            for i in torrent['files']:
                # Need to check only the media file or the .rar file to avoid checking all .r0* files in history
                if not (is_media_file(i['name']) or get_extension(i['name']) == 'rar'):
                    continue
                # Check if media was processed
                # OR check hash in case of RARed torrents
                if is_already_processed_media(i['name']) or is_info_hash_processed(str(torrent['hashString'])):
                    to_remove = True

            # Don't need to check status if we are not going to remove it.
            if not to_remove:
                log.info('Torrent not yet post-processed. Skipping: {torrent}',
                         {'torrent': torrent['name']})
                continue

            status = 'busy'
            error_string = torrent.get('errorString')
            if torrent.get('isStalled') and not torrent['percentDone'] == 1:
                status = 'stalled'
            elif error_string and 'unregistered torrent' in error_string.lower():
                status = 'unregistered'
            elif torrent['status'] == 0:
                status = 'stopped'
                if torrent['percentDone'] == 1:
                    # Check if torrent is stopped because of idle timeout
                    seed_timed_out = False
                    if torrent['activityDate'] > 0 and torrent['seedIdleLimit'] > 0:
                        last_activity_date = datetime.fromtimestamp(torrent['activityDate'])
                        seed_timed_out = (datetime.now() - timedelta(
                            minutes=torrent['seedIdleLimit'])) > last_activity_date

                    if torrent.get('isFinished') or seed_timed_out:
                        status = 'completed'
            elif torrent['status'] == 6:
                status = 'seeding'

            if status == 'completed':
                log.info(
                    'Torrent completed and reached minimum'
                    ' ratio: [{ratio:.3f}/{ratio_limit:.3f}] or'
                    ' seed idle limit: [{seed_limit} min].'
                    ' Removing it: [{name}]',
                    ratio=torrent['uploadRatio'],
                    ratio_limit=torrent['seedRatioLimit'],
                    seed_limit=torrent['seedIdleLimit'],
                    name=torrent['name']
                )
                self.remove_torrent(torrent['hashString'])
            elif status == 'stalled':
                log.warning('Torrent is stalled. Check it: [{name}]',
                            name=torrent['name'])
            elif status == 'unregistered':
                log.warning('Torrent was unregistered from tracker.'
                            ' Check it: [{name}]', name=torrent['name'])
            elif status == 'seeding':
                if float(torrent['uploadRatio']) < float(torrent['seedRatioLimit']):
                    log.info(
                        'Torrent did not reach minimum'
                        ' ratio: [{ratio:.3f}/{ratio_limit:.3f}].'
                        ' Keeping it: [{name}]',
                        ratio=torrent['uploadRatio'],
                        ratio_limit=torrent['seedRatioLimit'],
                        name=torrent['name']
                    )
                else:
                    log.info(
                        'Torrent completed and reached minimum ratio but it'
                        ' was force started again. Current'
                        ' ratio: [{ratio:.3f}/{ratio_limit:.3f}].'
                        ' Keeping it: [{name}]',
                        ratio=torrent['uploadRatio'],
                        ratio_limit=torrent['seedRatioLimit'],
                        name=torrent['name']
                    )
            elif status in ('stopped', 'busy'):
                log.info('Torrent is {status}. Keeping it: [{name}]',
                         status=status, name=torrent['name'])
            else:
                log.warning(
                    'Torrent has an unmapped status. Keeping it: [{name}].'
                    ' Report torrent info: {info}',
                    name=torrent['name'],
                    info=torrent
                )
        if not found_torrents:
            log.info('No torrents found that were snatched by Medusa')
Ejemplo n.º 5
0
def read_torrent_status(torrent_data):
    """Read torrent status from Deluge and Deluged client."""
    found_torrents = False
    info_hash_to_remove = []
    for torrent in viewitems(torrent_data):
        info_hash = str(torrent[0])
        details = torrent[1]
        if not is_info_hash_in_history(info_hash):
            continue
        found_torrents = True

        to_remove = False
        for i in details['files']:
            # Need to check only the media file or the .rar file to avoid checking all .r0* files in history
            if not (is_media_file(i['path'])
                    or get_extension(i['path']) == 'rar'):
                continue
            # Check if media was processed
            # OR check hash in case of RARed torrents
            if is_already_processed_media(
                    i['path']) or is_info_hash_processed(info_hash):
                to_remove = True

        # Don't need to check status if we are not going to remove it.
        if not to_remove:
            log.info('Torrent not yet post-processed. Skipping: {torrent}',
                     {'torrent': details['name']})
            continue

        status = 'busy'
        if details['is_finished']:
            status = 'completed'
        elif details['is_seed']:
            status = 'seeding'
        elif details['paused']:
            status = 'paused'
        else:
            status = details['state']

        if status == 'completed':
            log.info(
                'Torrent completed and reached minimum'
                ' ratio: [{ratio:.3f}/{ratio_limit:.3f}] or'
                ' seed idle limit'
                ' Removing it: [{name}]',
                ratio=details['ratio'],
                ratio_limit=details['stop_ratio'],
                name=details['name'])
            info_hash_to_remove.append(info_hash)
        elif status == 'seeding':
            if float(details['ratio']) < float(details['stop_ratio']):
                log.info(
                    'Torrent did not reach minimum'
                    ' ratio: [{ratio:.3f}/{ratio_limit:.3f}].'
                    ' Keeping it: [{name}]',
                    ratio=details['ratio'],
                    ratio_limit=details['stop_ratio'],
                    name=details['name'])
            else:
                log.info(
                    'Torrent completed and reached minimum ratio but it'
                    ' was force started again. Current'
                    ' ratio: [{ratio:.3f}/{ratio_limit:.3f}].'
                    ' Keeping it: [{name}]',
                    ratio=details['ratio'],
                    ratio_limit=details['stop_ratio'],
                    name=details['name'])
        else:
            log.info('Torrent is {status}. Keeping it: [{name}]',
                     status=status,
                     name=details['name'])

    if not found_torrents:
        log.info('No torrents found that were snatched by Medusa')
    return info_hash_to_remove
Ejemplo n.º 6
0
def read_torrent_status(torrent_data):
    """Read torrent status from Deluge and Deluged client."""
    found_torrents = False
    info_hash_to_remove = []
    for torrent in viewitems(torrent_data):
        info_hash = str(torrent[0])
        details = torrent[1]
        if not is_info_hash_in_history(info_hash):
            continue
        found_torrents = True

        to_remove = False
        for i in details['files']:
            # Need to check only the media file or the .rar file to avoid checking all .r0* files in history
            if not (is_media_file(i['path']) or get_extension(i['path']) == 'rar'):
                continue
            # Check if media was processed
            # OR check hash in case of RARed torrents
            if is_already_processed_media(i['path']) or is_info_hash_processed(info_hash):
                to_remove = True

        # Don't need to check status if we are not going to remove it.
        if not to_remove:
            log.info('Torrent not yet post-processed. Skipping: {torrent}',
                     {'torrent': details['name']})
            continue

        if details['is_finished']:
            status = 'completed'
        elif details['is_seed']:
            status = 'seeding'
        elif details['paused']:
            status = 'paused'
        else:
            status = details['state']

        if status == 'completed':
            log.info(
                'Torrent completed and reached minimum'
                ' ratio: [{ratio:.3f}/{ratio_limit:.3f}] or'
                ' seed idle limit'
                ' Removing it: [{name}]',
                ratio=details['ratio'],
                ratio_limit=details['stop_ratio'],
                name=details['name']
            )
            info_hash_to_remove.append(info_hash)
        elif status == 'seeding':
            if float(details['ratio']) < float(details['stop_ratio']):
                log.info(
                    'Torrent did not reach minimum'
                    ' ratio: [{ratio:.3f}/{ratio_limit:.3f}].'
                    ' Keeping it: [{name}]',
                    ratio=details['ratio'],
                    ratio_limit=details['stop_ratio'],
                    name=details['name']
                )
            else:
                log.info(
                    'Torrent completed and reached minimum ratio but it'
                    ' was force started again. Current'
                    ' ratio: [{ratio:.3f}/{ratio_limit:.3f}].'
                    ' Keeping it: [{name}]',
                    ratio=details['ratio'],
                    ratio_limit=details['stop_ratio'],
                    name=details['name']
                )
        else:
            log.info('Torrent is {status}. Keeping it: [{name}]', status=status, name=details['name'])

    if not found_torrents:
        log.info('No torrents found that were snatched by Medusa')
    return info_hash_to_remove