Skip to content

Commit

Permalink
plugins.twitch: Support VOD URLs with a time parameter.
Browse files Browse the repository at this point in the history
  • Loading branch information
chrippa committed Dec 16, 2013
1 parent fcee5bd commit 2194206
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 3 deletions.
10 changes: 7 additions & 3 deletions src/livestreamer/plugins/justintv_common.py
Expand Up @@ -9,14 +9,14 @@
except ImportError:
izip = zip

from livestreamer.compat import urljoin
from livestreamer.compat import urlparse, urljoin
from livestreamer.exceptions import NoStreamsError, PluginError, StreamError
from livestreamer.options import Options
from livestreamer.plugin import Plugin
from livestreamer.stream import (HTTPStream, HLSStream, FLVPlaylist,
extract_flv_header_tags)
from livestreamer.utils import (parse_json, res_json, res_xml, verifyjson,
urlget)
from livestreamer.utils import (parse_json, parse_qsd, res_json, res_xml,
verifyjson, urlget)

__all__ = ["PluginBase", "APIBase"]

Expand Down Expand Up @@ -118,8 +118,12 @@ def __init__(self, url):
self.video_type = match.get("video_type")
self.video_id = match.get("video_id")
self.usher = UsherService(match.get("domain"))

parsed = urlparse(url)
self.params = parse_qsd(parsed.query)
except AttributeError:
self.channel = None
self.params = None
self.video_id = None
self.video_type = None
self.usher = None
Expand Down
19 changes: 19 additions & 0 deletions src/livestreamer/plugins/twitch.py
@@ -1,3 +1,5 @@
import re

from livestreamer.exceptions import PluginError, NoStreamsError
from livestreamer.options import Options

Expand All @@ -9,6 +11,17 @@
JustinTVAPIBase = justintv_common.APIBase


def time_to_offset(t):
match = re.match(r"((?P<minutes>\d+)m)?((?P<seconds>\d+)s)?", t)
if match:
offset = int(match.group("minutes") or "0") * 60
offset += int(match.group("seconds") or "0")
else:
offset = 0

return offset


class TwitchAPI(JustinTVAPIBase):
def __init__(self):
JustinTVAPIBase.__init__(self, host="twitch.tv")
Expand Down Expand Up @@ -75,6 +88,12 @@ def _get_video_streams(self):
else:
raise

# Parse the "t" query parameter on broadcasts and adjust
# start offset if needed.
time_offset = self.params.get("t")
if time_offset and self.video_type == "a":
videos["start_offset"] = time_to_offset(self.params.get("t"))

return self._create_playlist_streams(videos)


Expand Down

1 comment on commit 2194206

@ckorn
Copy link

@ckorn ckorn commented on 2194206 Dec 17, 2013

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This commit is just awesome !

Thanks both to twitch and chrippa !
Now I only need to visit twitch to find out the correct time to start livestreamer :D

Please sign in to comment.