Segments too close together, only skips one + better start timing

Fixes #15
This commit is contained in:
dmunozv04
2022-04-02 00:25:01 +02:00
parent eabede1db5
commit 3fe89082f6

38
main.py
View File

@@ -4,7 +4,7 @@ import pyatv
import aiohttp import aiohttp
from cache import AsyncTTL from cache import AsyncTTL
import json import json
import time
def listToTuple(function): def listToTuple(function):
def wrapper(*args): def wrapper(*args):
@@ -22,10 +22,10 @@ class MyPushListener(pyatv.interface.PushListener):
web_session = None web_session = None
categories = ["sponsor"] categories = ["sponsor"]
def __init__(self, apikey, atv, categories): def __init__(self, apikey, atv, categories, web_session):
self.apikey = apikey self.apikey = apikey
self.rc = atv.remote_control self.rc = atv.remote_control
self.web_session = aiohttp.ClientSession() self.web_session = web_session
self.categories = categories self.categories = categories
self.atv = atv self.atv = atv
@@ -35,20 +35,21 @@ class MyPushListener(pyatv.interface.PushListener):
self.task.cancel() self.task.cancel()
except: except:
pass pass
self.task = asyncio.create_task(process_playstatus(playstatus, self.apikey, self.rc, self.web_session, self.categories, self.atv)) time_start = time.time()
self.task = asyncio.create_task(process_playstatus(playstatus, self.apikey, self.rc, self.web_session, self.categories, self.atv, time_start))
def playstatus_error(self, updater, exception): def playstatus_error(self, updater, exception):
print(exception) print(exception)
print("stopped") print("stopped")
async def process_playstatus(playstatus, apikey, rc, web_session, categories, atv): async def process_playstatus(playstatus, apikey, rc, web_session, categories, atv, time_start):
if playstatus.device_state == playstatus.device_state.Playing and atv.metadata.app.identifier == "com.google.ios.youtube": if playstatus.device_state == playstatus.device_state.Playing and atv.metadata.app.identifier == "com.google.ios.youtube":
vid_id = await get_vid_id(playstatus.title, playstatus.artist, apikey, web_session) vid_id = await get_vid_id(playstatus.title, playstatus.artist, apikey, web_session)
print(vid_id) print(vid_id)
segments, duration = await get_segments(vid_id, web_session, categories) segments, duration = await get_segments(vid_id, web_session, categories)
print(segments) print(segments)
await time_to_segment(segments, playstatus.position, rc) await time_to_segment(segments, playstatus.position, rc, time_start)
@AsyncTTL(time_to_live=300, maxsize=5) @AsyncTTL(time_to_live=300, maxsize=5)
@@ -74,13 +75,25 @@ async def get_segments(vid_id, web_session, categories = ["sponsor"]):
try: try:
duration = response[0]["videoDuration"] duration = response[0]["videoDuration"]
for i in response: for i in response:
segments.append(i["segment"]) segment = i["segment"]
try:
#Get segment before to check if they are too close to each other
segment_before_end = segments[-1][1]
segment_before_start = segments[-1][0]
except:
segment_before_end = -10
if segment[0] - segment_before_end < 1: #Less than 1 second appart, combine them and skip them together
segment = [segment_before_start, segment[1]]
segments.pop()
segments.append(segment)
except: except:
duration = 0 duration = 0
return segments, duration return segments, duration
async def time_to_segment(segments, position, rc): async def time_to_segment(segments, position, rc, time_start):
position = position + (time.time() - time_start)
for segment in segments: for segment in segments:
if position < 2 and (position >= segment[0] and position < segment[1]): if position < 2 and (position >= segment[0] and position < segment[1]):
next_segment = [position, segment[1]] next_segment = [position, segment[1]]
@@ -112,12 +125,12 @@ async def connect_atv(loop, identifier, airplay_credentials):
return await pyatv.connect(config, loop) return await pyatv.connect(config, loop)
async def loop_atv(event_loop, atv_config, apikey, categories): async def loop_atv(event_loop, atv_config, apikey, categories, web_session):
identifier = atv_config["identifier"] identifier = atv_config["identifier"]
airplay_credentials = atv_config["airplay_credentials"] airplay_credentials = atv_config["airplay_credentials"]
atv = await connect_atv(event_loop, identifier, airplay_credentials) atv = await connect_atv(event_loop, identifier, airplay_credentials)
if atv: if atv:
listener = MyPushListener(apikey, atv, categories) listener = MyPushListener(apikey, atv, categories, web_session)
atv.push_updater.listener = listener atv.push_updater.listener = listener
atv.push_updater.start() atv.push_updater.start()
@@ -131,7 +144,7 @@ async def loop_atv(event_loop, atv_config, apikey, categories):
#reconnect to apple tv #reconnect to apple tv
atv = await connect_atv(event_loop, identifier, airplay_credentials) atv = await connect_atv(event_loop, identifier, airplay_credentials)
if atv: if atv:
listener = MyPushListener(apikey, atv, categories) listener = MyPushListener(apikey, atv, categories, web_session)
atv.push_updater.listener = listener atv.push_updater.listener = listener
atv.push_updater.start() atv.push_updater.start()
@@ -150,8 +163,9 @@ def start_async():
loop = asyncio.get_event_loop_policy().get_event_loop() loop = asyncio.get_event_loop_policy().get_event_loop()
asyncio.set_event_loop(loop) asyncio.set_event_loop(loop)
atv_configs, apikey, categories = load_config() atv_configs, apikey, categories = load_config()
web_session = aiohttp.ClientSession()
for i in atv_configs: for i in atv_configs:
loop.create_task(loop_atv(loop, i, apikey, categories)) loop.create_task(loop_atv(loop, i, apikey, categories, web_session))
loop.run_forever() loop.run_forever()
if __name__ == "__main__": if __name__ == "__main__":