Clean-up and dont mark status twice

This commit is contained in:
mano3m committed 2013-12-16 22:43:05 +01:00
1 parent e9a457e263
commit 55777531d5
1 file changed
+95 -88
+95 -88
View File
@@ -211,120 +211,127 @@ class Release(Plugin):
db = get_session() db = get_session()
rel = db.query(Relea).filter_by(id = id).first() rel = db.query(Relea).filter_by(id = id).first()
if rel: if not rel:
item = {}
for info in rel.info:
item[info.identifier] = info.value
fireEvent('notify.frontend', type = 'release.manual_download', data = True, message = 'Snatching "%s"' % item['name'])
# Get matching provider
provider = fireEvent('provider.belongs_to', item['url'], provider = item.get('provider'), single = True)
if not item.get('protocol'):
item['protocol'] = item['type']
item['type'] = 'movie'
if item.get('protocol') != 'torrent_magnet':
item['download'] = provider.loginDownload if provider.urls.get('login') else provider.download
success = self.download(data = item, media = rel.movie.to_dict({
'profile': {'types': {'quality': {}}},
'releases': {'status': {}, 'quality': {}},
'library': {'titles': {}, 'files':{}},
'files': {}
}), manual = True)
if success:
db.expunge_all()
rel = db.query(Relea).filter_by(id = id).first() # Get release again @RuudBurger why do we need to get it again??
fireEvent('notify.frontend', type = 'release.manual_download', data = True, message = 'Successfully snatched "%s"' % item['name'])
return {
'success': success
}
else:
log.error('Couldn\'t find release with id: %s', id) log.error('Couldn\'t find release with id: %s', id)
return {
'success': False
}
item = {}
for info in rel.info:
item[info.identifier] = info.value
fireEvent('notify.frontend', type = 'release.manual_download', data = True, message = 'Snatching "%s"' % item['name'])
# Get matching provider
provider = fireEvent('provider.belongs_to', item['url'], provider = item.get('provider'), single = True)
# Backwards compatibility code
if not item.get('protocol'):
item['protocol'] = item['type']
item['type'] = 'movie'
if item.get('protocol') != 'torrent_magnet':
item['download'] = provider.loginDownload if provider.urls.get('login') else provider.download
success = self.download(data = item, media = rel.movie.to_dict({
'profile': {'types': {'quality': {}}},
'releases': {'status': {}, 'quality': {}},
'library': {'titles': {}, 'files':{}},
'files': {}
}), manual = True)
if success:
db.expunge_all()
rel = db.query(Relea).filter_by(id = id).first() # Get release again @RuudBurger why do we need to get it again??
fireEvent('notify.frontend', type = 'release.manual_download', data = True, message = 'Successfully snatched "%s"' % item['name'])
return { return {
'success': False 'success': success
} }
def download(self, data, media, manual = False): def download(self, data, media, manual = False):
# Backwards compatibility code
if not data.get('protocol'): if not data.get('protocol'):
data['protocol'] = data['type'] data['protocol'] = data['type']
data['type'] = 'movie' data['type'] = 'movie'
# Test to see if any downloaders are enabled for this type # Test to see if any downloaders are enabled for this type
downloader_enabled = fireEvent('download.enabled', manual, data, single = True) downloader_enabled = fireEvent('download.enabled', manual, data, single = True)
if not downloader_enabled:
log.info('Tried to download, but none of the "%s" downloaders are enabled or gave an error', (data.get('protocol')))
return False
if downloader_enabled: # Download NZB or torrent file
snatched_status, done_status, downloaded_status, active_status = fireEvent('status.get', ['snatched', 'done', 'downloaded', 'active'], single = True) filedata = None
if data.get('download') and (ismethod(data.get('download')) or isfunction(data.get('download'))):
filedata = data.get('download')(url = data.get('url'), nzb_id = data.get('id'))
if filedata == 'try_next':
return filedata
# Download release to temp # Send NZB or torrent file to downloader
filedata = None download_result = fireEvent('download', data = data, media = media, manual = manual, filedata = filedata, single = True)
if data.get('download') and (ismethod(data.get('download')) or isfunction(data.get('download'))): if not download_result:
filedata = data.get('download')(url = data.get('url'), nzb_id = data.get('id')) log.info('Tried to download, but the "%s" downloader gave an error', (data.get('protocol')))
if filedata == 'try_next': return False
return filedata log.debug('Downloader result: %s', download_result)
download_result = fireEvent('download', data = data, media = media, manual = manual, filedata = filedata, single = True) snatched_status, done_status, downloaded_status, active_status = fireEvent('status.get', ['snatched', 'done', 'downloaded', 'active'], single = True)
log.debug('Downloader result: %s', download_result)
if download_result: try:
try: db = get_session()
# Mark release as snatched rls = db.query(Relea).filter_by(identifier = md5(data['url'])).first()
db = get_session() if not rls:
rls = db.query(Relea).filter_by(identifier = md5(data['url'])).first() log.error('No release found to store download information in')
if rls: return False
renamer_enabled = Env.setting('enabled', 'renamer')
# Save download-id info if returned renamer_enabled = Env.setting('enabled', 'renamer')
if isinstance(download_result, dict):
for key in download_result:
rls_info = ReleaseInfo(
identifier = 'download_%s' % key,
value = toUnicode(download_result.get(key))
)
rls.info.append(rls_info)
db.commit()
log_movie = '%s (%s) in %s' % (getTitle(media['library']), media['library']['year'], rls.quality.label) # Save download-id info if returned
snatch_message = 'Snatched "%s": %s' % (data.get('name'), log_movie) if isinstance(download_result, dict):
log.info(snatch_message) for key in download_result:
fireEvent('%s.snatched' % data['type'], message = snatch_message, data = rls.to_dict()) rls_info = ReleaseInfo(
identifier = 'download_%s' % key,
value = toUnicode(download_result.get(key))
)
rls.info.append(rls_info)
db.commit()
# If renamer isn't used, mark media done log_movie = '%s (%s) in %s' % (getTitle(media['library']), media['library']['year'], rls.quality.label)
if not renamer_enabled: snatch_message = 'Snatched "%s": %s' % (data.get('name'), log_movie)
# Mark release downloaded log.info(snatch_message)
self.updateStatus(rls.id, status = downloaded_status) fireEvent('%s.snatched' % data['type'], message = snatch_message, data = rls.to_dict())
try: # Mark release as snatched
if media['status_id'] == active_status.get('id'): if renamer_enabled:
for profile_type in media['profile']['types']: self.updateStatus(rls.id, status = snatched_status)
if profile_type['quality_id'] == rls.quality.id and profile_type['finish']:
log.info('Renamer disabled, marking media as finished: %s', log_movie)
# Mark release done # If renamer isn't used, mark media done if finished or release downloaded
self.updateStatus(rls.id, status = done_status) else:
if media['status_id'] == active_status.get('id'):
finished = next((True for profile_type in media['profile']['types'] if \
profile_type['quality_id'] == rls.quality.id and profile_type['finish']), False)
if finished:
log.info('Renamer disabled, marking media as finished: %s', log_movie)
# Mark media done # Mark release done
mdia = db.query(Media).filter_by(id = media['id']).first() self.updateStatus(rls.id, status = done_status)
mdia.status_id = done_status.get('id')
mdia.last_edit = int(time.time())
db.commit()
except:
log.error('Failed marking media finished, renamer disabled: %s', traceback.format_exc())
else:
self.updateStatus(rls.id, status = snatched_status)
except: # Mark media done
log.error('Failed marking media finished: %s', traceback.format_exc()) mdia = db.query(Media).filter_by(id = media['id']).first()
mdia.status_id = done_status.get('id')
mdia.last_edit = int(time.time())
db.commit()
return True return True
log.info('Tried to download, but none of the "%s" downloaders are enabled or gave an error', (data.get('protocol'))) # Assume release downloaded
self.updateStatus(rls.id, status = downloaded_status)
except:
log.error('Failed storing download status: %s', traceback.format_exc())
return False
return False return False