def update_path(): paths = Settings.get_settings('paths') excl = paths['media_root_exclude'] re_excl = re.compile(r'^(%s)/' % '|'.join([re.escape(p.rstrip('/')) for p in excl])) for file in iter_files(str(paths['media_root'])): if not re_excl.search(file): Media.add_file(file) time.sleep(.05) for media in Media.find({'files': {'$exists': True}}, timeout=False): files_orig = media['files'][:] for file in files_orig: if not os.path.exists(file) or re_excl.search(file): media['files'].remove(file) if not media['files'] and not media.get('urls'): Media.remove({'_id': media['_id']}, safe=True) elif media['files'] != files_orig: Media.save(media, safe=True) Work.set_info(NAME, 'updated', datetime.utcnow())
def search_subtitles(media_id): media = Media.get(media_id) if not media: return search_langs = Settings.get_settings('subtitles_langs') temp_dir = Settings.get_settings('paths')['tmp'] if not search_langs: logger.error('missing subtitles search langs') return root_path = Settings.get_settings('paths')['media']['video'].rstrip('/') + '/' info = media['info'] if info['subtype'] == 'tv': name = clean(info.get('name'), 6) season = info.get('season') episode = info.get('episode') date = None else: name = info.get('full_name') season = None episode = None date = media.get('extra', {}).get('imdb', {}).get('date') subtitles_langs = [] plugins = { 'subscene': Subscene(), 'opensubtitles': Opensubtitles(**Settings.get_settings('opensubtitles')), } stat = [] for file in media['files']: if not validate_file(file, root_path): continue file_ = get_file(file) dst = file_.get_subtitles_path() processed = False for lang in search_langs: logger.debug('searching %s subtitles for "%s" (%s)', lang, media['name'], file) for obj_name, obj in plugins.items(): if not obj.accessible: continue processed = True lang_ = LANGS_DEF[obj_name].get(lang) if not lang_: continue for url in obj.results(name, season, episode, date, lang_): doc = {'url': url, 'file': file_.file} if Subtitles.find_one(doc): continue try: files_dst = obj.download(url, dst, temp_dir) except RateLimitReached: break if not files_dst: continue for file_dst in files_dst: logger.info('downloaded %s on %s', file_dst, obj_name) doc['created'] = datetime.utcnow() Subtitles.insert(doc, safe=True) for lang in search_langs: if file_.set_subtitles(lang): subtitles_langs.append(lang) stat.append(processed) if False not in stat: media['updated_subs'] = datetime.utcnow() media['subtitles'] = sorted(list(set(subtitles_langs))) Media.save(media, safe=True)