forked from jackuess/pirateplay
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathxml2srt.py
More file actions
37 lines (34 loc) · 1.11 KB
/
xml2srt.py
File metadata and controls
37 lines (34 loc) · 1.11 KB
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
import sys, urllib2, xml.sax
class DCHandler(xml.sax.ContentHandler):
def __init__(self):
self.capture = False
self.data = ''
self.count = 1
def startElement(self, name, attrs):
if name == 'Subtitle':
self.data += '%s\n%s,%s --> %s,%s\n' % (attrs.get('SpotNumber'), attrs.get('TimeIn')[:8], attrs.get('TimeIn')[9:12], attrs.get('TimeOut')[:8], attrs.get('TimeOut')[9:12])
elif name == 'p':
self.data += '%s\n%s,%s --> %s,%s\n' % (self.count, attrs.get('begin')[:8], attrs.get('begin')[9:], attrs.get('end')[:8], attrs.get('end')[9:])
self.capture = True
self.count += 1
elif name == 'br':
self.data += '\n'
elif name == 'Text':
self.capture = True
def endElement(self, name):
if name == 'Subtitle':
self.data += '\n'
elif name == 'Text':
self.capture = False
self.data += '\n'
elif name == 'p':
self.capture = False
self.data += '\n\n'
def characters(self, ch):
if self.capture: self.data += ch
def xml2srt(url):
handler = DCHandler()
xml.sax.parseString(urllib2.urlopen(url).read(), handler)
return unicode(handler.data)
if __name__ == "__main__":
print xml2srt(sys.argv[1])