Ejemplo n.º 1
0
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)
Ejemplo n.º 2
0
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)
Ejemplo n.º 3
0
def process_to_db(media,
                  theme=None,
                  vid=None,
                  start=None,
                  end=None,
                  ffmpeg_end=None,
                  recap=None,
                  credits_start=None,
                  credits_end=None):
    """Process a plex media item to the db

       Args:
            media (Episode obj):
            theme: path to the theme.
            vid: path to the stripped wav of the media item.
            start (None, int): of theme.
            end (None, int): of theme.
            ffmpeg_end (None, int): What does ffmpeg think is the start of the ep.
            recap(None, bool): If this how has a recap or not
            credits_start(None, int): The offset (in sec) the credits text starts
            credits_end(None, int): The offset (in sec) the credits text ends

       Returns:
            None

    """
    global HT

    # Disable for now.
    # if media.TYPE == 'movie':
    #    return

    # This will download the theme and add it to
    # the hashtable if its missing
    if media.TYPE == 'episode' and theme is None:
        if HT.has_theme(media, add_if_missing=False) is False:
            LOG.debug('downloading theme from process_to_db')
            theme = download_theme(media, HT)

    name = media._prettyfilename()
    LOG.debug('Started to process %s', name)

    if vid is None and media.TYPE == 'episode':
        vid = convert_and_trim(check_file_access(media),
                               fs=11025,
                               trim=CONFIG['tv'].get('check_for_theme_sec',
                                                     600))

    # Find the start and the end of the theme in the episode file.
    if end is None and media.TYPE == 'episode':
        start, end = get_offset_end(vid, HT)

    # Guess when the intro ended using blackframes and audio silence.
    if ffmpeg_end is None:
        if media.TYPE == 'episode':
            trim = CONFIG['tv'].get('check_intro_ffmpeg_sec')
        else:
            trim = CONFIG['movie'].get('check_intro_ffmpeg_sec')
        ffmpeg_end = find_offset_ffmpeg(check_file_access(media), trim=trim)

    # Check for recap.
    if recap is None:
        recap = has_recap(media, CONFIG['tv'].get('words', []), audio=vid)

    if (media.TYPE == 'episode' and CONFIG['tv'].get('check_credits') is True
            and credits_start is None and credits_end is None):

        dur = media.duration / 1000 - CONFIG['tv'].get('check_credits_sec',
                                                       120)
        credits_start, credits_end = find_credits(check_file_access(media),
                                                  offset=dur,
                                                  check=-1)

    elif (media.TYPE == 'movie'
          and CONFIG['movie'].get('check_credits') is True
          and credits_start is None and credits_end is None):

        dur = media.duration / 1000 - CONFIG['movie'].get(
            'check_credits_sec', 600)
        credits_start, credits_end = find_credits(check_file_access(media),
                                                  offset=dur,
                                                  check=-1)
    else:
        # We dont want to find the credits.
        credits_start = -1
        credits_end = -1

    # We assume this is kinda right, # double check this # TODO
    location = list(i.file for i in media.iterParts() if i)[0]

    with session_scope() as se:
        try:
            se.query(Processed).filter_by(ratingKey=media.ratingKey).one()
        except NoResultFound:
            if media.TYPE == 'episode':
                p = Processed(show_name=media.grandparentTitle,
                              title=media.title,
                              type=media.TYPE,
                              theme_end=end,
                              theme_start=start,
                              theme_start_str=to_time(start),
                              theme_end_str=to_time(end),
                              ffmpeg_end=ffmpeg_end,
                              ffmpeg_end_str=to_time(ffmpeg_end),
                              credits_start=credits_start,
                              credits_start_str=to_time(credits_start),
                              credits_end=credits_end,
                              credits_end_str=to_time(credits_end),
                              duration=media.duration,
                              ratingKey=media.ratingKey,
                              grandparentRatingKey=media.grandparentRatingKey,
                              prettyname=media._prettyfilename(),
                              updatedAt=media.updatedAt,
                              has_recap=recap,
                              location=location)

            elif media.TYPE == 'movie':
                p = Processed(title=media.title,
                              type=media.TYPE,
                              ffmpeg_end=ffmpeg_end,
                              ffmpeg_end_str=to_time(ffmpeg_end),
                              credits_start=credits_start,
                              credits_start_str=to_time(credits_start),
                              credits_end=credits_end,
                              credits_end_str=to_time(credits_end),
                              duration=media.duration,
                              ratingKey=media.ratingKey,
                              prettyname=media._prettyfilename(),
                              updatedAt=media.updatedAt,
                              location=location)

            se.add(p)
            LOG.debug('Added %s to media.db', name)

            if media.TYPE == 'movie' and CONFIG['movie']['create_edl']:
                edl.write_edl(
                    location,
                    edl.db_to_edl(p, type=CONFIG['movie']['edl_action_type']))

            elif media.TYPE == 'episode' and CONFIG['tv']['create_edl']:
                edl.write_edl(
                    location,
                    edl.db_to_edl(p, type=CONFIG['tv']['edl_action_type']))
Ejemplo n.º 4
0
def process_to_db(media,
                  theme=None,
                  vid=None,
                  start=None,
                  end=None,
                  ffmpeg_end=None,
                  recap=None):
    """Process a plex media item to the db

       Args:
            media (Episode obj):
            theme: path to the theme.
            vid: path to the stripped wav of the media item.
            start (None, int): of theme.
            end (None, int): of theme.
            ffmpeg_end (None, int): What does ffmpeg think is the start of the ep.

       Returns:
            None

    """
    global HT

    # This will download the theme and add it to
    # the hashtable if its missing
    if theme is None:
        if HT.has_theme(media, add_if_missing=False) is False:
            LOG.debug('downloading theme from process_to_db')
            theme = download_theme(media, HT)

    ff = -1
    name = media._prettyfilename()
    LOG.debug('Started to process %s', name)

    if vid is None:
        vid = convert_and_trim(check_file_access(media), fs=11025, trim=600)

    # Find the start and the end of the theme in the video file.
    if end is None:
        start, end = get_offset_end(vid, HT)

    # Guess when the intro ended using blackframes and audio silence.
    if ffmpeg_end is None:
        ffmpeg_end = find_offset_ffmpeg(check_file_access(media))

    # Check for recap.
    if recap is None:
        recap = has_recap(media, CONFIG.get('words', []), audio=vid)

    with session_scope() as se:
        try:
            se.query(Preprocessed).filter_by(ratingKey=media.ratingKey).one()
        except NoResultFound:
            p = Preprocessed(show_name=media.grandparentTitle,
                             ep_title=media.title,
                             theme_end=end,
                             theme_start=start,
                             theme_start_str=to_time(start),
                             theme_end_str=to_time(end),
                             ffmpeg_end=ffmpeg_end,
                             ffmpeg_end_str=to_time(ffmpeg_end),
                             duration=media.duration,
                             ratingKey=media.ratingKey,
                             grandparentRatingKey=media.grandparentRatingKey,
                             prettyname=media._prettyfilename(),
                             updatedAt=media.updatedAt,
                             has_recap=recap)
            se.add(p)
            LOG.debug('Added %s to media.db', name)