Skip to content

Commit

Permalink
Use onAVStarted which requires a min version bump (#16)
Browse files Browse the repository at this point in the history
* only show skip dialog if we didn't already go to the next video

* more tolerant cp listener when `getTime` raises

* use onAVStarted

* remove previous hack

* prepare for 0.2.3 release

* update comment

* init should_start to False
  • Loading branch information
siku2 authored Nov 21, 2020
1 parent 964066d commit 63ac38d
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 18 deletions.
8 changes: 6 additions & 2 deletions addon.xml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<addon id="script.service.sponsorblock" version="0.2.2" name="SponsorBlock" provider-name="siku2">
<addon id="script.service.sponsorblock" version="0.2.3" name="SponsorBlock" provider-name="siku2">
<requires>
<import addon="xbmc.python" version="2.23.0"/>
<import addon="xbmc.python" version="2.26.0"/>
<import addon="script.module.requests" version="2.15.1"/>
<import addon="script.module.six" version="1.13.0"/>
<import addon="plugin.video.youtube" version="6.7.0"/>
Expand All @@ -28,6 +28,10 @@ Users submit when a sponsor happens and the add-on automatically skips sponsors
</description>
<disclaimer lang="en_GB">This is an unoffical port of the SponsorBlock browser extension</disclaimer>
<news>
[0.2.3]
- Bumped Kodi version to 18 (Leia) to use `onAVStarted` which should hopefully fix issues
with segments starting right at the beginning of a video.

[0.2.2]
- Fixed "Playback fails with sponsor section at video start" again.
Let's hope it worked this time :) (@bclindner, #14)
Expand Down
39 changes: 28 additions & 11 deletions resources/lib/player_listener.py
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,10 @@ def __init__(self, *args, **kwargs):
self._segments = [] # List[SponsorSegment]
self._next_segment = None # type: Optional[SponsorSegment]

# set by `onPlaybackStarted` and then read (/ reset) by `onAVStarted`
self._should_start = False
self._should_start_lock = threading.Lock()

def preload_segments(self, video_id):
assert not self._thread_running

Expand Down Expand Up @@ -107,18 +111,29 @@ def _prepare_segments(self, video_id):
return bool(self._segments)

def onPlayBackStarted(self): # type: () -> None
video_id = youtube_api.get_video_id()
if not video_id:
return
with self._should_start_lock:
video_id = youtube_api.get_video_id()
if not video_id:
return

if video_id == self._take_ignore_next_video_id():
logger.debug("ignoring video %s because it's ignored", video_id)
return
if video_id == self._take_ignore_next_video_id():
logger.debug("ignoring video %s because it's ignored", video_id)
return

if not self._prepare_segments(video_id):
return
if not self._prepare_segments(video_id):
return

self._next_segment = self._segments[0]
self._should_start = True

def onAVStarted(self): # type: () -> None
with self._should_start_lock:
if self._should_start:
self._should_start = False
else:
# `onPlayBackStarted` determined that we don't need to start
return

self._next_segment = self._segments[0]
self.start()

def _select_next_checkpoint(self):
Expand Down Expand Up @@ -164,8 +179,10 @@ def _reached_checkpoint(self):
else:
self.seekTime(seg.end)

if addon.get_config(CONF_SHOW_SKIPPED_DIALOG, bool):
self.__show_skipped_dialog(seg)
# with `playnext` there's no way for the user to "unskip" right now,
# so we only show the dialog if we're still in the same video.
if addon.get_config(CONF_SHOW_SKIPPED_DIALOG, bool):
self.__show_skipped_dialog(seg)

if addon.get_config(CONF_SKIP_COUNT_TRACKING, bool):
logger.debug("reporting sponsor skipped")
Expand Down
3 changes: 0 additions & 3 deletions resources/lib/sponsorblock/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -90,9 +90,6 @@ def get_skip_segments(
segments = []
for raw in data:
start, end = raw["segment"]
# segments starting at 0.0 auto-skip the entire video due to a
# quirk with xbmc.Player so we adjust start times accordingly here
start = max(start, 0.5)
seg = SponsorSegment(raw["UUID"], raw["category"], start, end)
segments.append(seg)

Expand Down
14 changes: 12 additions & 2 deletions resources/lib/utils/checkpoint_listener.py
Original file line number Diff line number Diff line change
Expand Up @@ -78,12 +78,18 @@ def _get_current_time(self): # type:() -> float
This is problematic for us because we rely on the current time being correct.
This function solves this by returning the seek time instead, until the seek time is cleared again.
If `Player.getTime` raises an exception, this function returns 0.0.
Returns:
Current time in seconds.
"""
seek_time = self._seek_time
if seek_time is None:
return self.getTime()
try:
return self.getTime()
except RuntimeError:
logger.exception("failed to get playback time, assuming 0.0")
return 0.0

return seek_time

Expand Down Expand Up @@ -181,7 +187,11 @@ def start(self): # type: () -> None
logger.warning("checkpoint listener already running, stopping")
self.stop()

logger.info("starting checkpoint listener")
if self.isPlaying():
logger.info("starting checkpoint listener")
else:
logger.warning("starting checkpoint listener but player isn't playing anything")

self._thread = threading.Thread(
target=self.__t_event_loop, name="Checkpoint Listener"
)
Expand Down

0 comments on commit 63ac38d

Please sign in to comment.