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
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')
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