def download_subtitles(self): if self.location == '': return if not os.path.isfile(self.location): sickrage.app.log.debug("%s: Episode file doesn't exist, can't download subtitles for S%02dE%02d" % (self.show.indexer_id, self.season or 0, self.episode or 0)) return sickrage.app.log.debug("%s: Downloading subtitles for S%02dE%02d" % (self.show.indexer_id, self.season or 0, self.episode or 0)) subtitles, newSubtitles = Subtitles().download_subtitles(self.showid, self.season, self.episode) self.subtitles = ','.join(subtitles) self.subtitles_searchcount += 1 if self.subtitles_searchcount else 1 self.subtitles_lastsearch = datetime.datetime.now().toordinal() if newSubtitles: subtitle_list = ", ".join([Subtitles().name_from_code(newSub) for newSub in newSubtitles]) sickrage.app.log.debug("%s: Downloaded %s subtitles for S%02dE%02d" % (self.show.indexer_id, subtitle_list, self.season or 0, self.episode or 0)) Notifiers.mass_notify_subtitle_download(self.pretty_name(), subtitle_list) else: sickrage.app.log.debug("%s: No subtitles downloaded for S%02dE%02d" % (self.show.indexer_id, self.season or 0, self.episode or 0)) return newSubtitles
def handle_get(self): q = self.get_argument('q') codes = [{"id": code, "name": Subtitles().name_from_code(code)} for code in Subtitles().subtitle_code_filter()] codes = list(filter(lambda code: q.lower() in code['name'].lower(), codes)) return self.write(json_encode(codes))
def get(self, *args, **kwargs): codes = [{ "id": code, "name": Subtitles().name_from_code(code) } for code in Subtitles().subtitle_code_filter()] codes = list( filter(lambda code: code['id'] in Subtitles().wanted_languages(), codes)) return self.write(json_encode(codes))
def get(self, *args, **kwargs): indexer_id = self.get_argument('indexer_id') which_subs = self.get_argument('whichSubs') result = {} for dbData in self.db_session.query(TVEpisode).filter_by( showid=int(indexer_id)).filter(TVEpisode.status.endswith(4), TVEpisode.season != 0): if which_subs == 'all': if not frozenset(Subtitles().wanted_languages()).difference( dbData.subtitles.split(',')): continue elif which_subs in dbData.subtitles: continue cur_season = dbData.season cur_episode = dbData.episode if cur_season not in result: result[cur_season] = {} if cur_episode not in result[cur_season]: result[cur_season][cur_episode] = {} result[cur_season][cur_episode]["name"] = dbData.name result[cur_season][cur_episode]["subtitles"] = dbData.subtitles return self.write(json_encode(result))
def rename_ep_file(self, cur_path, new_path, old_path_length=0): """ Creates all folders needed to move a file to its new location, renames it, then cleans up any folders left that are now empty. :param cur_path: The absolute path to the file you want to move/rename :param new_path: The absolute path to the destination for the file WITHOUT THE EXTENSION :param old_path_length: The length of media file path (old name) WITHOUT THE EXTENSION """ # new_dest_dir, new_dest_name = os.path.split(new_path) if old_path_length == 0 or old_path_length > len(cur_path): # approach from the right cur_file_name, cur_file_ext = os.path.splitext(cur_path) else: # approach from the left cur_file_ext = cur_path[old_path_length:] cur_file_name = cur_path[:old_path_length] if cur_file_ext[1:] in Subtitles().subtitle_extensions: # Extract subtitle language from filename sublang = os.path.splitext(cur_file_name)[1][1:] # Check if the language extracted from filename is a valid language if sublang in Subtitles().subtitle_code_filter(): cur_file_ext = '.' + sublang + cur_file_ext # put the extension on the incoming file new_path += cur_file_ext make_dirs(os.path.dirname(new_path)) # move the file try: sickrage.app.log.info("Renaming file from %s to %s" % (cur_path, new_path)) move_file(cur_path, new_path) except (OSError, IOError) as e: sickrage.app.log.warning("Failed renaming %s to %s : %r" % (cur_path, new_path, e)) return False # clean up any old folders that are empty delete_empty_folders(os.path.dirname(cur_path)) return True
def get(self, *args, **kwargs): which_subs = self.get_argument('whichSubs', None) ep_counts = {} show_names = {} sorted_show_ids = [] status_results = [] if which_subs: for s in get_show_list(session=self.db_session): if not s.subtitles == 1: continue for e in s.episodes: if e.season != 0 and (str(e.status).endswith('4') or str(e.status).endswith('6')): status_results += [{ 'show_name': s.name, 'indexer_id': s.indexer_id, 'subtitles': e.subtitles }] for cur_status_result in sorted(status_results, key=lambda k: k['show_name']): if which_subs == 'all': if not frozenset( Subtitles().wanted_languages()).difference( cur_status_result["subtitles"].split(',')): continue elif which_subs in cur_status_result["subtitles"]: continue cur_indexer_id = int(cur_status_result["indexer_id"]) if cur_indexer_id not in ep_counts: ep_counts[cur_indexer_id] = 1 else: ep_counts[cur_indexer_id] += 1 show_names[cur_indexer_id] = cur_status_result["show_name"] if cur_indexer_id not in sorted_show_ids: sorted_show_ids.append(cur_indexer_id) return self.render("/manage/subtitles_missed.mako", whichSubs=which_subs, show_names=show_names, ep_counts=ep_counts, sorted_show_ids=sorted_show_ids, title=_('Missing Subtitles'), header=_('Missing Subtitles'), topmenu='manage', controller='manage', action='subtitles_missed')
def refresh_subtitles(self): """Look for subtitles files and refresh the subtitles property""" subtitles, save_subtitles = Subtitles().refresh_subtitles(self.showid, self.season, self.episode) if save_subtitles: self.subtitles = ','.join(subtitles)
def task(self, force=False): if self.running or not sickrage.app.config.subtitles.enable and not force: return try: self.running = True # set thread name threading.currentThread().setName(self.name) if len(Subtitles().getEnabledServiceList()) < 1: sickrage.app.log.warning('Not enough services selected. At least 1 service is required to search subtitles in the background') return session = sickrage.app.main_db.session() sickrage.app.log.info('Checking for subtitles') # get episodes on which we want subtitles # criteria is: # - show subtitles = 1 # - episode subtitles != config wanted languages or 'und' (depends on config multi) # - search count < 2 and diff(airdate, now) > 1 week : now -> 1d # - search count < 7 and diff(airdate, now) <= 1 week : now -> 4h -> 8h -> 16h -> 1d -> 1d -> 1d rules = self._get_rules() now = datetime.datetime.now() results = [] for s in get_show_list(): if s.subtitles != 1: continue for e in session.query(MainDB.TVEpisode).filter_by(series_id=s.series_id).filter(MainDB.TVEpisode.location != '', ~MainDB.TVEpisode.subtitles.in_( Subtitles().wanted_languages()), or_(MainDB.TVEpisode.subtitles_searchcount <= 2, and_(MainDB.TVEpisode.subtitles_searchcount <= 7, datetime.date.today() - MainDB.TVEpisode.airdate))): results += [{ 'show_name': s.name, 'series_id': s.series_id, 'series_provider_id': s.series_provider_id, 'season': e.season, 'episode': e.episode, 'status': e.status, 'subtitles': e.subtitles, 'searchcount': e.subtitles_searchcount, 'lastsearch': e.subtitles_lastsearch, 'location': e.location, 'airdate_daydiff': (datetime.date.today() - e.airdate).days }] if len(results) == 0: sickrage.app.log.info('No subtitles to download') return for epToSub in results: show_object = find_show(epToSub['series_id'], epToSub['series_provider_id']) episode_object = show_object.get_episode(epToSub['season'], epToSub['episode']) if not os.path.isfile(epToSub['location']): sickrage.app.log.debug('Episode file does not exist, cannot download ' 'subtitles for episode %dx%d of show %s' % (episode_object.season, episode_object.episode, epToSub['show_name'])) continue if ((epToSub['airdate_daydiff'] > 7 and epToSub['searchcount'] < 2 and now - epToSub['lastsearch'] > datetime.timedelta(hours=rules['old'][epToSub['searchcount']])) or (epToSub['airdate_daydiff'] <= 7 and epToSub['searchcount'] < 7 and now - epToSub['lastsearch'] > datetime.timedelta(hours=rules['new'][epToSub['searchcount']]))): sickrage.app.log.debug('Downloading subtitles for ' 'episode %dx%d of show %s' % (episode_object.season, episode_object.episode, epToSub['show_name'])) existing_subtitles = episode_object.subtitles try: episode_object.download_subtitles() new_subtitles = frozenset(episode_object.subtitles).difference(existing_subtitles) if new_subtitles: sickrage.app.log.info('Downloaded subtitles ' 'for S%02dE%02d in %s' % (episode_object.season, episode_object.episode, ', '.join(new_subtitles))) except Exception as e: sickrage.app.log.debug('Unable to find subtitles') sickrage.app.log.debug(str(e)) finally: self.running = False