Skip to content

Commit

Permalink
Improve subtitles sql and logging
Browse files Browse the repository at this point in the history
  • Loading branch information
medariox committed Mar 4, 2016
1 parent 4787e63 commit a130e74
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 38 deletions.
32 changes: 15 additions & 17 deletions sickbeard/subtitles.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@
from sickbeard import history
from sickbeard import db
from sickbeard import processTV
from sickbeard.common import Quality
from sickbeard.helpers import remove_non_release_groups, isMediaFile, isRarFile
from sickrage.helper.common import episode_num, dateTimeFormat, subtitle_extensions
from sickrage.helper.exceptions import ex
Expand Down Expand Up @@ -141,6 +140,11 @@ def download_subtitles(subtitles_info): # pylint: disable=too-many-locals, too-
episode_num(subtitles_info['season'], subtitles_info['episode'], numbering='absolute')), logger.DEBUG)
return existing_subtitles, None

logger.log(u'Checking subtitle candidates for {} {} ({})'.format
(subtitles_info['show_name'], episode_num(subtitles_info['season'], subtitles_info['episode']) or
episode_num(subtitles_info['season'], subtitles_info['episode'], numbering='absolute'),
os.path.basename(subtitles_info['location'])), logger.DEBUG)

subtitles_path = get_subtitles_path(subtitles_info['location'])
video_path = subtitles_info['location']

Expand Down Expand Up @@ -180,7 +184,7 @@ def download_subtitles(subtitles_info): # pylint: disable=too-many-locals, too-
if not subtitles_list:
logger.log(u'No subtitles found for {} {}'.format
(subtitles_info['show_name'], episode_num(subtitles_info['season'], subtitles_info['episode']) or
episode_num(subtitles_info['season'], subtitles_info['episode'], numbering='absolute')), logger.DEBUG)
episode_num(subtitles_info['season'], subtitles_info['episode'], numbering='absolute')))
return existing_subtitles, None

for subtitle in subtitles_list:
Expand All @@ -204,6 +208,12 @@ def download_subtitles(subtitles_info): # pylint: disable=too-many-locals, too-
logger.log(traceback.format_exc(), logger.ERROR)
return existing_subtitles, None

if not found_subtitles:
logger.log(u'No subtitles downloaded for {} {}'.format
(subtitles_info['show_name'], episode_num(subtitles_info['season'], subtitles_info['episode']) or
episode_num(subtitles_info['season'], subtitles_info['episode'], numbering='absolute')))
return existing_subtitles, None

for subtitle in found_subtitles:
subtitle_path = subliminal.subtitle.get_subtitle_path(video.name,
None if not sickbeard.SUBTITLES_MULTI else
Expand Down Expand Up @@ -480,8 +490,6 @@ def dhm(td):

logger.log(u'Checking for missed subtitles', logger.INFO)

statuses = list({status for status in Quality.DOWNLOADED + Quality.ARCHIVED})

database = db.DBConnection()
# Shows with air date <= 30 days, have a limit of 100 results
# Shows with air date > 30 days, have a limit of 200 results
Expand All @@ -495,9 +503,8 @@ def dhm(td):
"FROM tv_episodes AS e INNER JOIN tv_shows AS s "
"ON (e.showid = s.indexer_id) "
"WHERE s.subtitles = 1 AND age {} 30 AND e.subtitles NOT LIKE ? "
"AND e.location != '' AND e.status IN ({}) ORDER BY lastsearch ASC LIMIT {}".format
(args[0], ','.join(['?'] * len(statuses)), args[1]),
[datetime.datetime.now().toordinal(), wanted_languages(True)] + statuses
"AND e.status LIKE '%4' ORDER BY lastsearch ASC LIMIT {}".format
(args[0], args[1]), [datetime.datetime.now().toordinal(), wanted_languages(True)]
)

if not sql_results:
Expand Down Expand Up @@ -545,10 +552,6 @@ def dhm(td):
dhm(lastsearched + delay_time - now)), logger.DEBUG)
continue

logger.log(u'Searching for missing subtitles of {} {}'.format
(ep_to_sub['show_name'], episode_num(ep_to_sub['season'], ep_to_sub['episode']) or
episode_num(ep_to_sub['season'], ep_to_sub['episode'], numbering='absolute')), logger.INFO)

show_object = Show.find(sickbeard.showList, int(ep_to_sub['showid']))
if not show_object:
logger.log(u'Show with ID {} not found in the database'.format(ep_to_sub['showid']), logger.DEBUG)
Expand All @@ -562,18 +565,13 @@ def dhm(td):
continue

try:
new_subtitles = episode_object.download_subtitles()
episode_object.download_subtitles()
except Exception as error:
logger.log(u'Unable to find subtitles for {} {}. Error: {}'.format
(ep_to_sub['show_name'], episode_num(ep_to_sub['season'], ep_to_sub['episode']) or
episode_num(ep_to_sub['season'], ep_to_sub['episode'], numbering='absolute'), ex(error)), logger.ERROR)
continue

if new_subtitles:
logger.log(u'Downloaded {} subtitles for {} {}'.format
(', '.join(new_subtitles), ep_to_sub['show_name'], episode_num(ep_to_sub['season'], ep_to_sub['episode']) or
episode_num(ep_to_sub['season'], ep_to_sub['episode'], numbering='absolute')))

except Exception as error:
logger.log(u'Error while searching subtitles for {} {}. Error: {}'.format
(ep_to_sub['show_name'], episode_num(ep_to_sub['season'], ep_to_sub['episode']) or
Expand Down
29 changes: 8 additions & 21 deletions sickbeard/tv.py
Original file line number Diff line number Diff line change
Expand Up @@ -1399,7 +1399,7 @@ def _set_location(self, new_location):
location = property(lambda self: self._location, _set_location)

def refreshSubtitles(self):
"""Look for subtitles files and refresh the subtitles property"""
"""Look for subtitles files and refresh the subtitles property."""
episode_info = {'show_name': self.show.name, 'location': self.location,
'season': self.season, 'episode': self.episode}
self.subtitles, save_subtitles = subtitles.refresh_subtitles(episode_info, self.subtitles)
Expand All @@ -1408,20 +1408,11 @@ def refreshSubtitles(self):

def download_subtitles(self, force=False):
if not ek(os.path.isfile, self.location):
logger.log(u"{id}: Episode file doesn't exist, can't download subtitles for {ep}".format
(id=self.show.indexerid, ep=episode_num(self.season, self.episode)),
logger.DEBUG)
return

if not subtitles.needs_subtitles(self.subtitles):
logger.log(u'Episode already has all needed subtitles, skipping episode {ep} of show {show}'.format
(ep=episode_num(self.season, self.episode), show=self.show.name), logger.DEBUG)
logger.log(u"Episode file doesn't exist, can't download subtitles for {} {}".format
(self.show.name, episode_num(self.season, self.episode) or
episode_num(self.season, self.episode, numbering='absolute')), logger.DEBUG)
return

logger.log(u"Checking subtitle candidates for {show} {ep} ({location})".format
(show=self.show.name, ep=episode_num(self.season, self.episode),
location=os.path.basename(self.location)), logger.DEBUG)

subtitles_info = {'location': self.location, 'subtitles': self.subtitles, 'season': self.season,
'episode': self.episode, 'name': self.name, 'show_name': self.show.name,
'show_indexerid': self.show.indexerid, 'status': self.status}
Expand All @@ -1433,16 +1424,12 @@ def download_subtitles(self, force=False):
self.saveToDB()

if new_subtitles:
subtitle_list = ", ".join([subtitles.name_from_code(code) for code in new_subtitles])
logger.log(u"{id}: Downloaded {subtitles} subtitles for {show} {ep}".format
(id=self.show.indexerid, subtitles=subtitle_list, show=self.show.name,
ep=episode_num(self.season, self.episode)), logger.DEBUG)
subtitle_list = ', '.join([subtitles.name_from_code(code) for code in new_subtitles])
logger.log(u'Downloaded {} subtitles for {} {}'.format
(subtitle_list, self.show.name, episode_num(self.season, self.episode) or
episode_num(self.season, self.episode, numbering='absolute')))

notifiers.notify_subtitle_download(self.prettyName(), subtitle_list)
else:
logger.log(u"{id}: No subtitles downloaded for {show} {ep}".format
(id=self.show.indexerid, show=self.show.name,
ep=episode_num(self.season, self.episode)), logger.DEBUG)

return new_subtitles

Expand Down

0 comments on commit a130e74

Please sign in to comment.