diff --git a/bw_plex/credits.py b/bw_plex/credits.py index 63cdf3b..e231e30 100644 --- a/bw_plex/credits.py +++ b/bw_plex/credits.py @@ -82,7 +82,7 @@ def video_frame_by_frame(path, offset=0, frame_range=None, step=1): duration = cap.get(cv2.CAP_PROP_FRAME_COUNT) / fps duration = int(duration) end = duration - start = offset + start = int(offset) # Just yield very step frame and currect time. frame_range = (i * fps for i in range(start, end, step)) @@ -273,7 +273,7 @@ def find_credits(path, offset=0, fps=None, duration=None, check=7, step=1, frame """ - LOG.debug('%r %r %r %r %r %r %r', path, offset, fps, duration, check, step, frame_range) + # LOG.debug('%r %r %r %r %r %r %r', path, offset, fps, duration, check, step, frame_range) import cv2 frames = [] start = -1 @@ -288,7 +288,7 @@ def find_credits(path, offset=0, fps=None, duration=None, check=7, step=1, frame for i, (frame, millisec) in enumerate(video_frame_by_frame(path, offset=offset, step=step, frame_range=frame_range)): - LOG.debug('progress %s', millisec / 1000) + # LOG.debug('progress %s', millisec / 1000) if frame is not None: recs = locate_text(frame, debug=False) diff --git a/bw_plex/plex.py b/bw_plex/plex.py index d4b4b94..e57a56f 100644 --- a/bw_plex/plex.py +++ b/bw_plex/plex.py @@ -722,6 +722,27 @@ def task(item, sessionkey): process_to_db(nxt) +def timeline(data): + """Process recently added episodes.""" + # Ideas/code is stolen from tautulli! Thanks Jonney! + timeline = data.get('TimelineEntry')[0] + state = timeline.get('state') + ratingkey = timeline.get('itemID') + title = timeline.get('title') + # section_id = timeline.get('sectionID') + metadata_type = timeline.get('type') + identifier = timeline.get('identifier') + metadata_state = timeline.get('mediaState') + LOG.debug('TIMELINE %s', title) + + if (metadata_type == 4 and state == 0 and + metadata_state == 'created' and + identifier == 'com.plexapp.plugins.library'): + LOG.debug('%s was added to %s', title, PMS.friendlyName) + ep = PMS.fetchItem(int(ratingkey)) + POOL.apply_async(process_to_db, args=(ep,)) + + def check(data): global JUMP_LIST @@ -811,6 +832,9 @@ def check(data): LOG.debug('Failed to find %s in the db', ratingkey) POOL.apply_async(task, args=(ratingkey, sessionkey)) + elif data.get('type') == 'timeline': + timeline(data) + @cli.command() @click.argument('-f', type=click.Path(exists=True)) diff --git a/tests/conftest.py b/tests/conftest.py index d16e7d7..6973cf6 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -4,8 +4,10 @@ import sys import tempfile from datetime import datetime as DT + from plexapi.video import Episode, Show from plexapi.compat import makedirs +from sqlalchemy.orm.exc import NoResultFound import pytest fp = os.path.join(os.path.dirname(os.path.dirname(__file__)), 'bw_plex') @@ -37,13 +39,10 @@ bw_plex.FP_HASHES = os.path.join(bw_plex.DEFAULT_FOLDER, 'hashes.pklz') bw_plex.LOG_FILE = os.path.join(bw_plex.DEFAULT_FOLDER, 'log.txt') bw_plex.INI_FILE = os.path.join(bw_plex.DEFAULT_FOLDER, 'config.ini') - - - import bw_plex.plex as plex import bw_plex.misc as misc import bw_plex.credits as credits - +from bw_plex.db import session_scope, Preprocessed TEST_DATA = os.path.join(os.path.dirname(__file__), 'test_data') @@ -65,7 +64,15 @@ def HT(): return misc.get_hashtable() - +@pytest.fixture() +def in_db(ratingkey): + rk = int(ratingkey) + with session_scope() as se: + try: + item = se.query(Preprocessed).filter_by(ratingKey=rk).one() + return item + except NoResultFound: + return @pytest.fixture() diff --git a/tests/test_cli.py b/tests/test_cli.py index 2754de7..7323ef6 100644 --- a/tests/test_cli.py +++ b/tests/test_cli.py @@ -86,9 +86,8 @@ def _test_process_to_db(episode, intro_file, cli_runner, tmpdir, monkeypatch, HT assert se.query(plex.Preprocessed).filter_by(ratingKey=episode.ratingKey).one() # lets check that we can export db shit too. - res = cli_runner.invoke(plex.export_db, ['-f', 'json', '-fp', str(tmpdir), '-wf']) - print(res.output) + # print(res.output) fp = os.path.join(str(tmpdir), 'Preprocessed.json') assert os.path.exists(fp) @@ -99,11 +98,6 @@ def _test_process_to_db(episode, intro_file, cli_runner, tmpdir, monkeypatch, HT def test_process(cli_runner, monkeypatch, episode, media, HT, intro_file, mocker): # Let the mock begin.. - #monkeypatch(plex, 'find_all_shows', lambda k: list(show)) - def j(a=None): - return [media] - def t(a=None): - return [episode] mocker.patch.object(plex, 'find_all_shows', side_effect=[[media], [episode]]) mocker.patch('click.prompt', side_effect=['0', '0']) @@ -123,15 +117,12 @@ def test_process(cli_runner, monkeypatch, episode, media, HT, intro_file, mocker monkeypatch.setattr(plex, 'find_next', lambda k: None) res = cli_runner.invoke(plex.process, ['-n', 'dexter', '-s', '1', '-t', '2', '-sd']) - print(res.output) - print('ass') - + #print(res.output) def test_add_theme_to_hashtable(cli_runner, monkeypatch, HT): # We just want to check that this doesnt blow up.. monkeypatch.setattr(plex, 'get_hashtable', HT) - cli_runner.invoke(plex.add_theme_to_hashtable, [2, None]) @@ -145,3 +136,35 @@ def test_ffmpeg_process(cli_runner, intro_file): def test_manually_correct_theme(): pass + + +def test_timeline(intro_file, HT, monkeypatch, mocker, episode): + + def fetchItem(i): + return episode + m = mocker.Mock() + m.fetchItem = fetchItem + + monkeypatch.setitem(plex.CONFIG, 'theme_source', 'tvtunes') + monkeypatch.setattr(plex, 'check_file_access', lambda k: intro_file) + monkeypatch.setattr(plex, 'HT', HT) + monkeypatch.setattr(plex, 'PMS', m) + monkeypatch.setattr(plex, 'find_next', lambda k: None) + + data = {"type": "timeline", + "size": 1, + "TimelineEntry": [{"identifier": "com.plexapp.plugins.library", + "sectionID": 2, + "itemID": 1337, + "type": 4, + "title": "Dexter S01 E01", + "state": 0, + "mediaState": "created", + "queueSize": 8, + "updatedAt": 1526744644}] + } + + plex.timeline(data) + plex.POOL.close() + plex.POOL.join() + assert len(HT.get_theme(episode))