-
Notifications
You must be signed in to change notification settings - Fork 3
/
parseList.py
167 lines (157 loc) · 5.21 KB
/
parseList.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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
import re
from network import getVideoUrl
from network import getPdfUrl
def char2int(n):
indexDict = {
'一':1,
'二':2,
'三':3,
'四':4,
'五':5,
'六':6,
'七':7,
'八':8,
'九':9,
'十':10
}
if len(n)==1:
return str(indexDict[n])
elif len(n)==2 and n[0]=='十':
return str(10+indexDict[n[1]])
elif len(n)==2 and n[1]=='十':
return str(indexDict[n[0]]*10)
elif len(n)==2:
return str(indexDict[n[0]]*10+indexDict[n[1]])
else:
return str(indexDict[n[0]]*10+indexDict[n[2]])
def reName(name):
if re.search('第[一二三四五六七八九十]{3}', name):
get = list(re.search('第[一二三四五六七八九十]{3}', name).span())
temp = ''
for i in range(len(name)):
if (i>get[0] and i<get[1]):
temp+=name[i]
temp = '第'+'0'+char2int(temp)
name = re.sub('第[一二三四五六七八九十]{3}', temp, name)
elif re.search('第[一二三四五六七八九十]{2}', name):
get = list(re.search('第[一二三四五六七八九十]{2}', name).span())
temp = ''
for i in range(len(name)):
if (i>get[0] and i<get[1]):
temp+=name[i]
temp = '第'+'0'+char2int(temp)
name = re.sub('第[一二三四五六七八九十]{2}', temp, name)
elif re.search('第[一二三四五六七八九十]', name):
get = list(re.search('第[一二三四五六七八九十]', name).span())
temp = ''
for i in range(len(name)):
if (i>get[0] and i<get[1]):
temp+=name[i]
temp = '第'+'00'+char2int(temp)
name = re.sub('第[一二三四五六七八九十]', temp, name)
return name
def namer(name, num='k'):
name = reName(name)
forbid = re.compile('[\\/:\*\?\"<>\|\s]')
legal_name = ''
for each in name:
if forbid.match(each):
pass
else:
legal_name+=each
if num=='k':
return legal_name
elif num<10:
return '00'+str(num)+legal_name
elif num<100:
return '0'+str(num)+legal_name
else:
return str(num)+legal_name
def getchId(line):
chPat = re.compile('chapterId=\d+')
chId = chPat.search(line)
if chId:
return chId.group()[10:]
else:
return None
def getcoId(line):
ciPat = re.compile('contentId=\d+')
coId = ciPat.search(line)
if coId:
return coId.group()[10:]
else:
return None
def getId(line):
idPat = re.compile('id=\d+')
iD = idPat.search(line)
if iD:
return iD.group()[3:]
else:
return None
def getcoType(line):
ctPat = re.compile('contentType=\d+')
coType = ctPat.search(line)
if coType:
return coType.group()[12:]
else:
return None
def getName(line):
nPat = re.compile('name=\".+\"')
name = nPat.search(line)
if name:
return name.group()[6:-1]
else:
return None
def notTest(line):
testPat = re.compile('s\d+\.test=s\d+')
if testPat.search(line):
return False
else:
return True
def parser(courseList:str, courseUrl)->dict:
courseDict = {}
'''
一级目录 无chapterId 无contentId contentType=1
二级目录 有chapterId 无contentId contentType=1
视频文件 有chapterId 有contentId contentType=1
pdf文件 有chapterId 有contentId contentType=3
'''
courseList = courseList.split('\n')
for line in courseList:
# ~ print('--*---------*--')
# ~ print(getchId(line))
# ~ print(getcoId(line))
# ~ print(getcoType(line))
# ~ print(getName(line))
if (getchId(line)==None and getcoId(line)==None and getcoType(line)=='1'):
# ~ print('一级目录')
#print('\n\n{0}\n{1}\n\n'.format(getName(line), namer(getName(line))))
temp = courseDict
courseDict[namer(getName(line))]={}
temp = courseDict[namer(getName(line))]
elif (getchId(line) and getcoId(line)==None and getcoType(line)=='1'):
# ~ print('二级目录')
temp2 = temp
temp2[namer(getName(line))]={}
temp2 = temp2[namer(getName(line))]
cc=0
elif (getchId(line) and getcoId(line) and getcoType(line)=='1'):
# ~ print('视频文件')
yes = getVideoUrl(getName(line), getcoId(line), getId(line), courseUrl)
if yes[1]=='hls':
yes[1]='m3u8'
temp2[namer(getName(line), cc)]={
# ~ 'contentId':getcoId(line),
# ~ 'id':getId(line),
'url':yes[0],
'format':yes[1]
}
cc+=1
elif (getchId(line) and getcoId(line) and getcoType(line)=='3' and notTest(line)):
# ~ print('pdf课件')
ok = getPdfUrl(getName(line), getcoId(line), getId(line))
temp2[namer(getName(line))]={
'url':ok,
'format':'pdf'
}
return courseDict