From 3c7cea68a3aa979691f5f727262c5e03d72103a4 Mon Sep 17 00:00:00 2001 From: Hellowlol Date: Sun, 20 May 2018 19:35:29 +0200 Subject: [PATCH 1/3] inital timeline entry. --- bw_plex/plex.py | 34 ++++++++++++++++++++++++++++++++++ 1 file changed, 34 insertions(+) diff --git a/bw_plex/plex.py b/bw_plex/plex.py index d4b4b94..e0d713d 100644 --- a/bw_plex/plex.py +++ b/bw_plex/plex.py @@ -722,6 +722,37 @@ def task(item, sessionkey): process_to_db(nxt) +def timeline(data): + """ + {"type":"timeline","size":1, + "TimelineEntry":[{"identifier":"com.plexapp.plugins.library", + "sectionID":2, + "itemID":40532, + "type":4, + "title":"Call the Midwife S06 E04", + "state":5, + "queueSize":8, + "updatedAt":1526744644}]} + """ + 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') + + # Ideas/code is stolen from tautulli! Thanks Jonney! + + 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 +842,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)) From 49ece18ee6d4f13c9cffeccbcff4e4a14c08dfbd Mon Sep 17 00:00:00 2001 From: Hellowlol Date: Sun, 20 May 2018 19:55:23 +0200 Subject: [PATCH 2/3] inital test --- tests/test_cli.py | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) diff --git a/tests/test_cli.py b/tests/test_cli.py index 5387437..32b0dc6 100644 --- a/tests/test_cli.py +++ b/tests/test_cli.py @@ -145,3 +145,32 @@ 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, + "queueSize": 8, + "updatedAt": 1526744644}] + } + + plex.timeline(data) + assert len(HT.get_theme(1337)) From 21661d370a17136b29759a8b78cead78d52e3b8e Mon Sep 17 00:00:00 2001 From: Hellowlol Date: Sun, 20 May 2018 21:29:04 +0200 Subject: [PATCH 3/3] more stuff --- bw_plex/credits.py | 6 +++--- bw_plex/plex.py | 16 +++------------- tests/conftest.py | 17 ++++++++++++----- tests/test_cli.py | 18 ++++++------------ 4 files changed, 24 insertions(+), 33 deletions(-) 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 e0d713d..e57a56f 100644 --- a/bw_plex/plex.py +++ b/bw_plex/plex.py @@ -723,17 +723,8 @@ def task(item, sessionkey): def timeline(data): - """ - {"type":"timeline","size":1, - "TimelineEntry":[{"identifier":"com.plexapp.plugins.library", - "sectionID":2, - "itemID":40532, - "type":4, - "title":"Call the Midwife S06 E04", - "state":5, - "queueSize":8, - "updatedAt":1526744644}]} - """ + """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') @@ -742,8 +733,7 @@ def timeline(data): metadata_type = timeline.get('type') identifier = timeline.get('identifier') metadata_state = timeline.get('mediaState') - - # Ideas/code is stolen from tautulli! Thanks Jonney! + LOG.debug('TIMELINE %s', title) if (metadata_type == 4 and state == 0 and metadata_state == 'created' and 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 32b0dc6..7268527 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]) @@ -168,9 +159,12 @@ def test_timeline(intro_file, HT, monkeypatch, mocker, episode): "type": 4, "title": "Dexter S01 E01", "state": 0, + "mediaState": "created", "queueSize": 8, "updatedAt": 1526744644}] } plex.timeline(data) - assert len(HT.get_theme(1337)) + plex.POOL.close() + plex.POOL.join() + assert len(HT.get_theme(episode))