def manually_correct_theme(name, url, type, rk, just_theme, remove_old_theme): # pragma: no cover """Set the correct fingerprint of the show in the hashes.db and process the eps of that show in the db against the new theme fingerprint. Args: name (str): name of the show url (str): the youtube/tvtunes url or filepath to the correct theme. type (str): What source to use for themes. rk (str): ratingkey of that show. Pass auto if your lazy. just_theme (bool): just add the theme song not reprocess stuff. remove_old_theme (bool): Removes all the old themes of this show Returns: None """ global HT HT = get_hashtable() # Assist for the lazy bastards.. if rk == 'auto': items = PMS.search(name) items = [i for i in items if i and i.TYPE == 'show'] items = choose('Select correct show', items, lambda x: '%s %s' % (x.title, x.TYPE)) if items: rk = items[0].ratingKey if remove_old_theme: themes = HT.get_theme(items[0]) for th in themes: LOG.debug('Removing %s from the hashtable', th) HT.remove(th) # Download the themes depending on the manual option or config file. download_theme(items[0], HT, theme_source=type, url=url) to_pp = [] if just_theme: return if rk: with session_scope() as se: # Find all episodes of this show. item = se.query(Processed).filter_by(grandparentRatingKey=rk) for i in item: to_pp.append(PMS.fetchItem(i.ratingKey)) # Prob should have used edit, but we do this so we can use process_to_db. se.delete(i) for media in to_pp: process_to_db(media)
def manually_correct_theme(name, url, type, rk, just_theme): """Set the correct fingerprint of the show in the hashes.db and process the eps of that show in the db against the new theme fingerprint. Args: name (str): name of the show url (str): the youtube url to the correct theme. rk (str): ratingkey of that show. Pass auto if your lazy. just_theme (bool): just add the theme song. Returns: None """ global HT HT = get_hashtable() # Assist for the lazy bastards.. if rk == 'auto': items = PMS.search(name) items = [i for i in items if i and i.TYPE == 'show'] items = choose('Select correct show', items, lambda x: '%s %s' % (x.title, x.TYPE)) if items: rk = items[0].ratingKey # I dont think think themes be removed anymore as one show can now have several themes # also it dont remove the files in THEMES, this is intended!. themes = HT.get_theme(items[0]) for th in themes: LOG.debug('Removing %s from the hashtable', th) HT.remove(th) # Download the themes depending on the manual option or config file. theme_path = download_theme(items[0], HT, theme_source=type, url=url) to_pp = [] # THis should be removed, if you just want to download the theme. use find theme. if just_theme: return if rk: with session_scope() as se: # Find all episodes of this show. item = se.query(Preprocessed).filter_by(grandparentRatingKey=rk) for i in item: to_pp.append(PMS.fetchItem(i.ratingKey)) # Prob should have edit, but we do this so we can use process_to_db. se.delete(i) for media in to_pp: process_to_db(media)
def set_manual_theme_time(showname, season, episode, type, start, end): # pragma: no cover """Set a manual start and end time for a theme. Args: showname(str): name of the show you want to find season(int): season number fx 1 episode(int): episode number 1 type(str): theme, credit # Still TODO Stuff for credits start(int, str): This can be in seconds or MM:SS format start(int, str): This can be in seconds or MM:SS format Returns: None """ LOG.debug('Trying to set manual time') result = PMS.search(showname) if result: items = choose('Select show', result, 'title') show = items[0] ep = show.episode(season=season, episode=episode) if ep: with session_scope() as se: item = se.query(Processed).filter_by( ratingKey=ep.ratingKey).one() start = to_sec(start) end = to_sec(end) if type == 'ffmpeg': item.correct_ffmpeg = end elif type == 'theme': if start: item.correct_time_start = start if end: item.correct_time_end = end elif type == 'credits': if start: item.correct_credits_start = start if end: item.correct_credits_end = end LOG.debug('Set correct_time %s for %s to start %s end %s', type, ep._prettyfilename(), start, end)
def manually_correct_theme(name, url, type, rk, just_theme): """Set the correct fingerprint of the show in the hashes.db and process the eps of that show in the db against the new theme fingerprint. Args: name (str): name of the show url (str): the youtube url to the correct theme. rk (str): ratingkey of that show. Pass auto if your lazy. just_theme (bool): just add the theme song. Returns: None """ global HT HT = get_hashtable() # Assist for the lazy bastards.. if rk == 'auto': item = PMS.search(name) item = choose('Select correct show', item, lambda x: '%s %s' % (x.title, x.TYPE)) if item: rk = item[0].ratingKey theme_path = search_for_theme_youtube(name, rk=rk, url=url, save_path=THEMES) for fp in HT.names: if os.path.basename(fp).lower() == name.lower() and os.path.exists(fp): LOG.debug('Removing %s from the hashtable', fp) HT.remove(fp) analyzer().ingest(HT, theme_path) # HT.save(FP_HASHES) to_pp = [] if just_theme: return if rk: with session_scope() as se: item = se.query(Preprocessed).filter_by(grandparentRatingKey=rk) for i in item: to_pp.append(PMS.fetchItem(i.ratingKey)) # Prob should have edit, but we do this so we can use process_to_db. se.delete(i) for media in to_pp: process_to_db(media)
def process(name, sample, threads, skip_done): """Manual process some/all eps. You will asked for what you want to process Args: name (None): Pass a name of a show you want to process sample (int): process x eps for all shows. threads (int): How many thread to use skip_done(bool): Should we skip stuff that is processed. Return: None """ global HT global SHOWS all_eps = [] if name: load_themes() shows = find_all_shows() shows = [s for s in shows if s.title.lower().startswith(name.lower())] shows = choose('Select what show to process', shows, 'title') for show in shows: eps = show.episodes() eps = choose('Select episodes', eps, lambda x: '%s %s' % (x._prettyfilename(), x.title)) all_eps += eps if sample: def lol(i): x = i.episodes()[:sample] return all_eps.extend(x) find_all_shows(lol) if skip_done: # Now there must be a better way.. with session_scope() as se: items = se.query(Preprocessed).all() for item in items: for ep in all_eps: if ep.ratingKey == item.ratingKey: click.secho('Removing as %s already is processed' % item.prettyname, fg='red') all_eps.remove(ep) HT = get_hashtable() def prot(item): try: process_to_db(item) except Exception as e: logging.error(e, exc_info=True) if all_eps: p = Pool(threads) # process_to_db craps out because off a race condition in get_theme(media) # if the user is selecting n eps > 1 for the same theme. # Lets just download the the themes first so the shit is actually processed. gr = set([i.grandparentRatingKey for i in all_eps]) - SHOWS.keys() LOG.debug('Downloading theme for %s shows this might take a while..', len(gr)) if len(gr): sh = p.map(PMS.fetchItem, gr) try: p.map(search_for_theme_youtube, [(s.title, s. ratingKey, THEMES) for s in sh]) except KeyboardInterrupt: pass try: load_themes() p.map(prot, all_eps) except KeyboardInterrupt: p.terminate()
def check_db(client_name, skip_done): """Do a manual check of the db. This will start playback on a client and seek the video file where we have found theme start/end and ffmpeg_end. You will be asked if its a correct match, press y or set the correct time in mm:ss format. Args: client_name (None, str): Name of the client you want to use (watch) skip_done (bool): Skip shit that is verified before. Returns: None """ if client_name is None: client = choose('Select what client to use', PMS.clients(), 'title')[0] else: client = PMS.client(client_name).connect() with session_scope() as se: items = se.query(Preprocessed).all() click.echo('') for item in sorted(items, key=lambda k: k.ratingKey): click.echo('%s %s' % (click.style('Checking', fg='white'), click.style(item.prettyname, bold=True, fg='green'))) click.echo('theme_start %s theme_end %s ffmpeg_end %s' % (item.theme_start, item.theme_end_str, item.ffmpeg_end)) click.echo('*%s*' % ('-' * 80)) click.echo('') if item.theme_start == -1 or item.theme_end == -1: click.echo('Exists in the db but the start of the theme was not found.' ' Check the audio file and run it again.') if item.theme_end != -1: if (not skip_done and item.correct_theme_start) or not item.correct_theme_start: click.echo('Found theme_start at %s %s theme_end %s %s' % (item.theme_start, item.theme_start_str, item.theme_end, item.theme_end_str)) client.playMedia(PMS.fetchItem(item.ratingKey)) time.sleep(1) client.seekTo(item.theme_start * 1000) start_match = click.prompt('Was theme_start at %s correct?' % item.theme_start_str) if start_match: if start_match == 'y': item.correct_theme_start = item.theme_start else: item.correct_theme_start = to_sec(start_match) if (not skip_done and item.correct_theme_end) or not item.correct_theme_end: client.seekTo(item.theme_end * 1000) end_match = click.prompt('Was theme_end at %s correct?' % item.theme_end_str) if end_match: if end_match == 'y': item.correct_theme_end = item.theme_end else: item.correct_theme_end = to_sec(end_match) if item.ffmpeg_end: if (not skip_done and item.correct_ffmpeg) or not item.correct_ffmpeg: click.echo('Found ffmpeg_end at sec %s time %s' % (item.ffmpeg_end, item.ffmpeg_end_str)) if item.ffmpeg_end > 30: j = item.ffmpeg_end - 20 else: j = item.ffmpeg_end client.playMedia(PMS.fetchItem(item.ratingKey)) time.sleep(1) client.seekTo(j * 1000) match = click.prompt('Was ffmpeg_end at %s correct?' % item.ffmpeg_end_str) if match: if match.lower() in ['y', 'yes']: item.correct_ffmpeg = item.ffmpeg_end else: item.correct_ffmpeg = to_sec(match) click.clear() # Commit thit shit after each loop. if se.dirty: se.commit() click.echo('Done')
def process(name, sample, threads, skip_done): """Manual process some/all eps. You will asked for what you want to process Args: name (None): Pass a name of a show you want to process sample (int): process x eps for all shows. threads (int): How many thread to use skip_done(bool): Should we skip stuff that is processed. Return: None """ global HT all_items = [] if name: medias = find_all_movies_shows() medias = [ s for s in medias if s.title.lower().startswith(name.lower()) ] medias = choose('Select what item to process', medias, 'title') for media in medias: if media.TYPE == 'show': eps = media.episodes() eps = choose( 'Select episodes', eps, lambda x: '%s %s' % (x._prettyfilename(), x.title)) all_items += eps else: all_items.append(media) if sample: def lol(i): if i.TYPE == 'show': x = i.episodes()[:sample] return all_items.extend(x) else: return all_items.append(i) find_all_movies_shows(lol) if skip_done: # Now there must be a better way.. with session_scope() as se: items = se.query(Processed).all() for item in items: for ep in all_items: if ep.ratingKey == item.ratingKey: click.secho( "Removing %s at it's already is processed" % item.prettyname, fg='red') all_items.remove(ep) HT = get_hashtable() def prot(item): try: process_to_db(item) except Exception as e: logging.error(e, exc_info=True) if all_items: p = Pool(threads) # Download all the themes first, skip the ones that we already have.. gr = set([ i.grandparentRatingKey for i in all_items if i.TYPE == 'episode' ]) - set(HT.get_themes().keys()) LOG.debug('Downloading theme for %s shows this might take a while..', len(gr)) if len(gr): sh = p.map(PMS.fetchItem, gr) try: p.map(HT.has_theme, sh) except KeyboardInterrupt: pass try: p.map(prot, all_items) except KeyboardInterrupt: p.terminate()
def check_db(client_name, skip_done): # pragma: no cover """Do a manual check of the db. This will start playback on a client and seek the video file where we have found theme start/end and ffmpeg_end. You will be asked if its a correct match, press y or set the correct time in mm:ss format. Args: client_name (None, str): Name of the client you want to use (watch) skip_done (bool): Skip episodes that already exist in the db. Returns: None """ if client_name is None: client = choose('Select what client to use', PMS.clients(), 'title') if len(client): client = client[0] else: click.echo('No client to check with.. Aborting') return else: client = PMS.client(client_name).connect() client.proxyThroughServer() with session_scope() as se: items = se.query(Processed).all() click.echo('') for item in sorted(items, key=lambda k: k.ratingKey): click.echo('%s %s' % (click.style('Checking', fg='white'), click.style(item.prettyname, bold=True, fg='green'))) click.echo('theme_start %s theme_end %s ffmpeg_end %s' % (item.theme_start, item.theme_end_str, item.ffmpeg_end)) click.echo('*%s*' % ('-' * 80)) click.echo('') media = PMS.fetchItem(item.ratingKey) if item.theme_start == -1 or item.theme_end == -1: click.echo( 'Exists in the db but the start of the theme was not found.' ' Check the audio file and run it again. Use cmd manually_correct_theme' ) if item.theme_end != -1: if (not skip_done and item.correct_theme_start ) or not item.correct_theme_start: click.echo('Found theme_start at %s %s theme_end %s %s' % (item.theme_start, item.theme_start_str, item.theme_end, item.theme_end_str)) client.playMedia(media, offset=item.theme_start * 1000) time.sleep(1) start_match = click.prompt( 'Was theme_start at %s correct? [y or MM:SS]' % item.theme_start_str) if start_match: if start_match in ['y', 'yes']: item.correct_theme_start = item.theme_start else: item.correct_theme_start = to_sec(start_match) if (not skip_done and item.correct_theme_end) or not item.correct_theme_end: client.playMedia(media, offset=item.theme_end * 1000) end_match = click.prompt( 'Was theme_end at %s correct? [y or MM:SS]' % item.theme_end_str) if end_match: if end_match in ['y', 'yes']: item.correct_theme_end = item.theme_end else: item.correct_theme_end = to_sec(end_match) if item.ffmpeg_end: if (not skip_done and item.correct_ffmpeg) or not item.correct_ffmpeg: click.echo('Found ffmpeg_end at sec %s time %s' % (item.ffmpeg_end, item.ffmpeg_end_str)) if item.ffmpeg_end > 30: j = item.ffmpeg_end - 20 else: j = item.ffmpeg_end client.playMedia(media, offset=j * 1000) time.sleep(1) match = click.prompt( 'Was ffmpeg_end at %s correct? [y or MM:SS]' % item.ffmpeg_end_str) if match: if match.lower() in ['y', 'yes']: item.correct_ffmpeg = item.ffmpeg_end else: item.correct_ffmpeg = to_sec(match) # This needs to be tested manually. if item.credits_start and item.credits_start != 1: if (not skip_done and item.correct_credits_start ) or not item.correct_credits_start: click.echo('Found credits start as sec %s time %s' % (item.credits_start, item.credits_start_str)) client.playMedia(media, offset=item.credits_start - 10) time.sleep(1) match = click.prompt( 'Did the credits start at %s correct? [y or MM:SS]' % item.credits_start_str) if match: if match.lower() in ['y', 'yes']: item.correct_credits_start = item.credits_start else: item.correct_credits_start = to_sec(match) click.clear() # Commit this shit after each loop. if se.dirty: se.commit() click.echo('Done')