Skip to content

Commit 845230c

Browse files
committed
return uniform dicts from parser
1 parent 270101b commit 845230c

File tree

1 file changed

+39
-10
lines changed

1 file changed

+39
-10
lines changed

resources/lib/twitch/parser.py

Lines changed: 39 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -43,10 +43,20 @@ def m3u8_to_dict(string):
4343
d = dict()
4444
matches = re.finditer(_m3u_pattern, string)
4545
for m in matches:
46-
d[m.group('group_name')] = m.group('url')
4746
if m.group('group_id') == 'chunked':
48-
d.update({'Source': m.group('url')}) # ensure Source stream identified for consistency
49-
47+
d[m.group('group_id')] = {
48+
'id': m.group('group_id'),
49+
'name': 'Source',
50+
'url': m.group('url'),
51+
'bandwidth': int(m.group('bandwidth'))
52+
}
53+
else:
54+
d[m.group('group_id')] = {
55+
'id': m.group('group_id'),
56+
'name': m.group('group_name'),
57+
'url': m.group('url'),
58+
'bandwidth': int(m.group('bandwidth'))
59+
}
5060
log.debug('m3u8_to_dict result:\n{}'.format(d))
5161
return d
5262

@@ -55,13 +65,21 @@ def m3u8_to_list(string):
5565
log.debug('m3u8_to_list called for:\n{}'.format(string))
5666
l = list()
5767
matches = re.finditer(_m3u_pattern, string)
58-
chunked = None
5968
for m in matches:
60-
l.append((m.group('group_name'), m.group('url'), m.group('bandwidth')))
6169
if m.group('group_id') == 'chunked':
62-
chunked = ('Source', m.group('url'), int(m.group('bandwidth')))
63-
if (chunked) and (not any(re.match('[Ss]ource', name) for name, url, bandwidth in l)):
64-
l.insert(0, chunked)
70+
l.insert(0, {
71+
'id': m.group('group_id'),
72+
'name': 'Source',
73+
'url': m.group('url'),
74+
'bandwidth': int(m.group('bandwidth'))
75+
})
76+
else:
77+
l.append({
78+
'id': m.group('group_id'),
79+
'name': m.group('group_name'),
80+
'url': m.group('url'),
81+
'bandwidth': int(m.group('bandwidth'))
82+
})
6583

6684
log.debug('m3u8_to_list result:\n{}'.format(l))
6785
return l
@@ -73,8 +91,19 @@ def clip_embed_to_list(string):
7391
l = list()
7492
if match:
7593
match = eval(match.group('qualities'))
76-
l = [(item['quality'], item['source'], -1) for item in match]
77-
l.insert(0, ('Source', l[0][1], -1))
94+
l = [{
95+
'id': item['quality'],
96+
'name': item['quality'],
97+
'url': item['source'],
98+
'bandwidth': -1
99+
} for item in match]
100+
if l:
101+
l.insert(0, {
102+
'id': 'Source',
103+
'name': 'Source',
104+
'url': l[0]['url'],
105+
'bandwidth': -1
106+
})
78107

79108
log.debug('clip_embed_to_list result:\n{}'.format(l))
80109
return l

0 commit comments

Comments
 (0)