Skip to content

Commit

Permalink
Merge pull request #68 from olof/topic/justin_archive_content
Browse files Browse the repository at this point in the history
Support archived content from Justin/Twitch
  • Loading branch information
spaam committed Apr 27, 2014
2 parents 45f9d81 + cb710e6 commit a175a74
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 13 deletions.
1 change: 1 addition & 0 deletions lib/svtplay_dl/fetcher/http.py
Expand Up @@ -11,6 +11,7 @@

def download_http(options, url):
""" Get the stream from HTTP """
log.debug("Fetching %s", url)
request = Request(url)
request.add_header('User-Agent', 'Mozilla/5.0 (Windows; U; Windows NT 5.1; en-GB; rv:1.9.0.3) Gecko/2008092417 Firefox/3.0.3')
try:
Expand Down
45 changes: 32 additions & 13 deletions lib/svtplay_dl/service/justin.py
Expand Up @@ -46,33 +46,52 @@ class Justin(Service):

def get(self, options):
urlp = urlparse(self.url)
success = False

try:
self._get_video(urlp, options)
except JustinUrlException as e:
log.debug(str(e))

for jtv_video_type in [self._get_chapter, self._get_archive,
self._get_channel]:
try:
self._get_channel(urlp, options)
jtv_video_type(urlp, options)
success = True
break
except JustinUrlException as e:
log.debug(str(e))
log.error("Can't find media for URL")
sys.exit(2)

if not success:
log.debug(str(e))
log.error("This twitch/justin video type is unsupported")
sys.exit(2)

def _get_video(self, urlp, options):
match = re.match(r'/\w+/c/(\d+)', urlp.path)
if not match:
raise JustinUrlException('video', urlp.geturl())

url = "http://api.justin.tv/api/broadcast/by_chapter/%s.xml?onsite=true" % match.group(1)
def _get_static_video(self, vid, options, vidtype):
url = "http://api.justin.tv/api/broadcast/by_%s/%s.xml?onsite=true" % (
vidtype, vid)
data = get_http_data(url)
if not data:
return False

xml = ET.XML(data)
url = xml.find("archive").find("video_file_url").text

download_http(options, url)


def _get_archive(self, urlp, options):
match = re.match(r'/\w+/b/(\d+)', urlp.path)
if not match:
raise JustinUrlException('video', urlp.geturl())

self._get_static_video(match.group(1), options, 'archive')


def _get_chapter(self, urlp, options):
match = re.match(r'/\w+/c/(\d+)', urlp.path)
if not match:
raise JustinUrlException('video', urlp.geturl())

self._get_static_video(match.group(1), options, 'chapter')


def _get_access_token(self, channel):
"""
Get a Twitch access token. It's a three element dict:
Expand Down

0 comments on commit a175a74

Please sign in to comment.