bc4c3cf532d66baf4bb91496e75e1042c304aac5
[vpproxy.git] / xmlparse.py
1 #!/usr/bin/python
2
3 import xml.dom.minidom
4 import sys,os
5 from pprint import pprint
6 from plugins.modules.PlaylistGenerator import PlaylistGenerator
7
8 tvguide_url="http://www.teleguide.info/download/new3/jtv.zip"
9
10 def parseXML(filename):
11   datasource = open(filename)
12   return xml.dom.minidom.parse(datasource)   # parse an open file
13
14 def getText(element):
15   rc = []
16   nodelist = element.childNodes
17   for node in nodelist:
18     if node.nodeType == node.TEXT_NODE:
19       rc.append(node.data)
20       return ''.join(rc)
21
22 def main(argv):
23   if len(argv) < 2:
24     sys.stderr.write("Usage: %s <xml-file>\n" % (argv[0],))
25     return 1
26                     
27   if not os.path.exists(argv[1]):
28     sys.stderr.write("ERROR: Channel file %r was not found!\n" % (argv[1],))
29     return 1
30
31   dom = parseXML(argv[1])
32   tracklist = dom.getElementsByTagName('playlist')[0].getElementsByTagName('trackList')[0].getElementsByTagName('track')
33
34   channels = PlaylistGenerator()
35   
36   for track in tracklist:
37   
38     channel = {}
39   
40     channel['name'] = getText(track.getElementsByTagName('title')[0])
41     channel['url'] = getText(track.getElementsByTagName('location')[0])
42     try:
43       channel['logo'] = getText(track.getElementsByTagName('image')[0])
44     except:
45       None
46     try:    
47       channel['country'] = getText(track.getElementsByTagName('country')[0])
48     except:
49       None
50     try:        
51       channel['group'] = getText(track.getElementsByTagName('category')[0])
52     except:
53       None  
54
55     channels.addItem(channel)  
56
57   print channels.dumpm3u().encode('utf8')
58
59 if __name__ == "__main__":
60    sys.exit(main(sys.argv))