Skip to content

Commit 1728dd6

Browse files
authored
Merge pull request #35 from anxdpanic/dev
add usher.live_request and usher.video_request
2 parents 68ccfb7 + e6067a7 commit 1728dd6

File tree

1 file changed

+48
-12
lines changed

1 file changed

+48
-12
lines changed

resources/lib/twitch/api/usher.py

Lines changed: 48 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,16 @@
1010
from twitch.queries import ClipsQuery, HiddenApiQuery, UsherQuery
1111
from twitch.queries import query
1212

13+
from six.moves.urllib.parse import urlencode
14+
15+
16+
def valid_video_id(video_id):
17+
if video_id.startswith('videos'):
18+
video_id = 'v' + video_id[6:]
19+
if video_id.startswith(('a', 'c', 'v')):
20+
return video_id[1:]
21+
return ''
22+
1323

1424
@query
1525
def channel_token(channel):
@@ -34,6 +44,22 @@ def _legacy_video(video_id):
3444
return q
3545

3646

47+
def live_request(channel):
48+
token = channel_token(channel)
49+
if keys.ERROR in token:
50+
return token
51+
else:
52+
q = UsherQuery('api/channel/hls/{channel}.m3u8')
53+
q.add_urlkw(keys.CHANNEL, channel)
54+
q.add_param(keys.SIG, token[keys.SIG])
55+
q.add_param(keys.TOKEN, token[keys.TOKEN])
56+
q.add_param(keys.ALLOW_SOURCE, Boolean.TRUE)
57+
q.add_param(keys.ALLOW_SPECTRE, Boolean.TRUE)
58+
q.add_param(keys.ALLOW_AUDIO_ONLY, Boolean.TRUE)
59+
url = '?'.join([q.url, urlencode(q.params)])
60+
return {'url': url, 'headers': q.headers}
61+
62+
3763
@query
3864
def _live(channel, token):
3965
q = UsherQuery('api/channel/hls/{channel}.m3u8')
@@ -55,7 +81,25 @@ def live(channel):
5581
return _live(channel, token)
5682

5783

58-
@m3u8
84+
def video_request(video_id):
85+
video_id = valid_video_id(video_id)
86+
if video_id:
87+
token = vod_token(video_id)
88+
if keys.ERROR in token:
89+
return token
90+
else:
91+
q = UsherQuery('vod/{id}')
92+
q.add_urlkw(keys.ID, video_id)
93+
q.add_param(keys.NAUTHSIG, token[keys.SIG])
94+
q.add_param(keys.NAUTH, token[keys.TOKEN])
95+
q.add_param(keys.ALLOW_SOURCE, Boolean.TRUE)
96+
q.add_param(keys.ALLOW_AUDIO_ONLY, Boolean.TRUE)
97+
url = '?'.join([q.url, urlencode(q.params)])
98+
return {'url': url, 'headers': q.headers}
99+
else:
100+
raise NotImplementedError('Unknown Video Type')
101+
102+
59103
@query
60104
def _vod(video_id, token):
61105
q = UsherQuery('vod/{id}')
@@ -67,18 +111,10 @@ def _vod(video_id, token):
67111
return q
68112

69113

114+
@m3u8
70115
def video(video_id):
71-
if video_id.startswith('videos') or video_id.startswith('v'):
72-
if video_id.startswith('videos'):
73-
video_id = 'v' + video_id[6:]
74-
video_id = video_id[1:]
75-
token = vod_token(video_id)
76-
if keys.ERROR in token:
77-
return token
78-
else:
79-
return _vod(video_id, token)
80-
elif video_id.startswith(('a', 'c')):
81-
video_id = video_id[1:]
116+
video_id = valid_video_id(video_id)
117+
if video_id:
82118
token = vod_token(video_id)
83119
if keys.ERROR in token:
84120
return token

0 commit comments

Comments
 (0)