-
Notifications
You must be signed in to change notification settings - Fork 35
/
biliDanmuXmlParser.py
88 lines (79 loc) · 2.62 KB
/
biliDanmuXmlParser.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
# (C) 2019-2020 lifegpc
# This file is part of bili.
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as published
# by the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
import xml.sax
def loadXML(filen):
"加载弹幕"
p = xml.sax.make_parser()
p.setFeature(xml.sax.handler.feature_namespaces, 0)
h = Hander()
p.setContentHandler(h)
p.parse(filen)
return p.getContentHandler().sa
class Hander(xml.sax.ContentHandler):
istag = 0
sa = {}
sb = []
sc = {}
def startDocument(self):
self.sa = {}
self.istag = 0
self.sb = []
self.sc = {}
def startElement(self, tag, attributes):
if tag == 'd':
self.istag = 1
at = attributes['p'].split(',')
self.sc = {'ti': at[0], 'mod': at[1], 'fs': at[2], 'fc': at[3], 'ut': at[4], 'dp': at[5], 'si': at[6], 'ri': at[7]}
elif tag == 'chatserver':
self.istag = 2
elif tag == 'chatid':
self.istag = 3
elif tag == 'mission':
self.istag = 4
elif tag == 'maxlimit':
self.istag = 5
elif tag == 'state':
self.istag = 6
elif tag == 'real_name':
self.istag = 7
elif tag == 'source':
self.istag = 8
else:
self.istag = 0
def endElement(self, tag):
if self.istag > 0:
self.istag = 0
if tag == 'd':
self.sb.append(self.sc)
def characters(self, context):
if self.istag == 1:
self.sc['t'] = context
elif self.istag == 2:
self.sa['chatserver'] = context
elif self.istag == 3:
self.sa['chatid'] = context
elif self.istag == 4:
self.sa['mission'] = context
elif self.istag == 5:
self.sa['maxlimit'] = context
elif self.istag == 6:
self.sa['state'] = context
elif self.istag == 7:
self.sa['real_name'] = context
elif self.istag == 8:
self.sa['source'] = context
def endDocument(self):
self.sa['list'] = self.sb