Skip to content

Commit e541e76

Browse files
authored
Merge pull request #16 from anxdpanic/dev
error passthrough in parser
2 parents 342d66b + 30f7a6e commit e541e76

File tree

3 files changed

+38
-16
lines changed

3 files changed

+38
-16
lines changed

addon.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
2-
<addon id="script.module.python.twitch" name="python-twitch for Kodi" version="1.0.0~alpha8" provider-name="A Talented Community">
2+
<addon id="script.module.python.twitch" name="python-twitch for Kodi" version="1.0.0~alpha9" provider-name="A Talented Community">
33
<requires>
44
<import addon="xbmc.python" version="2.1.0"/>
55
<import addon="script.module.six" version="1.9.0"/>

resources/lib/twitch/api/usher.py

Lines changed: 25 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -34,11 +34,8 @@ def _legacy_video(video_id):
3434
return q
3535

3636

37-
@m3u8
3837
@query
39-
def live(channel):
40-
token = channel_token(channel)
41-
38+
def _live(channel, token):
4239
q = UsherQuery('api/channel/hls/{channel}.m3u8')
4340
q.add_urlkw(keys.CHANNEL, channel)
4441
q.add_param(keys.SIG, token[keys.SIG])
@@ -49,12 +46,17 @@ def live(channel):
4946

5047

5148
@m3u8
52-
@query
53-
def _vod(video_id):
54-
video_id = video_id[1:]
49+
def live(channel):
50+
token = channel_token(channel)
51+
if keys.ERROR in token:
52+
return token
53+
else:
54+
return _live(channel, token)
5555

56-
token = vod_token(video_id)
5756

57+
@m3u8
58+
@query
59+
def _vod(video_id, token):
5860
q = UsherQuery('vod/{id}')
5961
q.add_urlkw(keys.ID, video_id)
6062
q.add_param(keys.NAUTHSIG, token[keys.SIG])
@@ -64,13 +66,22 @@ def _vod(video_id):
6466

6567

6668
def video(video_id):
67-
if video_id.startswith('videos'):
68-
video_id = 'v' + video_id[6:]
69-
return _vod(video_id)
70-
elif video_id.startswith('v'):
71-
return _vod(video_id)
69+
if video_id.startswith('videos') or video_id.startswith('v'):
70+
if video_id.startswith('videos'):
71+
video_id = 'v' + video_id[6:]
72+
video_id = video_id[1:]
73+
token = vod_token(video_id)
74+
if keys.ERROR in token:
75+
return token
76+
else:
77+
return _vod(video_id, token)
7278
elif video_id.startswith(('a', 'c')):
73-
return _legacy_video(video_id)
79+
video_id = video_id[1:]
80+
token = vod_token(video_id)
81+
if keys.ERROR in token:
82+
return token
83+
else:
84+
return _vod(video_id, token)
7485
else:
7586
raise NotImplementedError('Unknown Video Type')
7687

resources/lib/twitch/parser.py

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
# -*- encoding: utf-8 -*-
22
import re
3+
from twitch import keys
34
from twitch.logging import log
45

56
_m3u_pattern = re.compile(
@@ -12,10 +13,20 @@
1213

1314
_clip_embed_pattern = re.compile(r'quality_options:\s*(?P<qualities>\[[^\]]+?\])')
1415

16+
_error_pattern = re.compile(r'.*<tr><td><b>error</b></td><td>(?P<message>.+?)</td></tr>.*', re.IGNORECASE)
17+
1518

1619
def m3u8(f):
1720
def m3u8_wrapper(*args, **kwargs):
18-
return m3u8_to_list(f(*args, **kwargs))
21+
results = f(*args, **kwargs)
22+
if keys.ERROR in results:
23+
if isinstance(results, dict):
24+
return results
25+
else:
26+
error = re.search(_error_pattern, results)
27+
if error:
28+
return {'error': 'Error', 'message': error.group('message'), 'status': 0}
29+
return m3u8_to_list(results)
1930

2031
return m3u8_wrapper
2132

0 commit comments

Comments
 (0)