-
-
Notifications
You must be signed in to change notification settings - Fork 6
/
simplem3u8.py
42 lines (33 loc) · 1.37 KB
/
simplem3u8.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
#!python
import re
ATTRIBUTELISTPATTERN = re.compile(r'''((?:[^,"']|"[^"]*"|'[^']*')+)''')
class parseHandler:
def parse(self, m3u8Data):
listCnt = 0
retList = {}
retList[listCnt] = {}
for line in m3u8Data.splitlines():
line=line.decode()
if (line.startswith("#EXT-X-MEDIA:")):
params = self.parseExt(line, "#EXT-X-MEDIA:")
for attribute in params:
attr, val = attribute.split("=", 1)
retList[listCnt][attr.lower()] = val.lower()
if (line.startswith("#EXT-X-STREAM-INF:")):
params = self.parseExt(line, "#EXT-X-STREAM-INF:")
for attribute in params:
try:
attr, val = attribute.split("=", 1)
val = val.replace('"', "")
retList[listCnt][attr.lower()] = val.lower()
except ValueError:
pass
if (line.startswith(('https://', 'http://', '../', 'index_'))):
retList[listCnt]['uri'] = line
listCnt += 1
retList[listCnt] = {}
return retList
def parseExt(self, line, tag):
extMedia = line.replace(tag, "")
extMedia = extMedia.replace("\n", "")
return ATTRIBUTELISTPATTERN.split(extMedia)[1::2]