Output DOS line endings
[xmltvtotgd.git] / icetotgd.py
blob302142df806d8a69d44ba895ae04272cf1a98739
1 #!/usr/bin/env python
2 from xml.etree import ElementTree as ET
3 import datetime
5 class IceToTgd(object):
7 def use_xml_file(self, filename):
8 self.tree = ET.parse(filename)
9 self.load_channels()
10 self.load_programmes()
12 def load_channels(self):
13 self.channels = dict(
14 [(ch.get('id'),
15 {'lcn': ch.findtext('lcn'),
16 'display-name': ch.findtext('display-name')})
17 for ch in self.tree.findall('channel')])
19 def load_programmes(self):
20 self.programmes = \
21 [{'title': p.findtext('title'),
22 'subtitle': p.findtext('sub-title'),
23 'desc': p.findtext('desc'),
24 'categories': [x.text for x in p.findall('category')],
25 'channel': p.get('channel'),
26 'rating': None,
27 'start': timestamp_from_xmltv_time(p.get('start')),
28 'stop': timestamp_from_xmltv_time(p.get('stop'))}
29 for p in self.tree.findall('programme')]
31 def tgd_channel(self, programme):
32 return self.channels[programme['channel']]['lcn']
34 def programme_to_tgd(self, programme):
35 tgd_channel = self.tgd_channel(programme)
36 tgd_start = tgd_time_from_timestamp(programme['start'])
37 duration = programme['stop'] - programme['start']
38 tgd_duration = tgd_duration_from_timedelta(duration)
39 tgd_rating = 'X'
40 short_desc = ''
41 if programme['subtitle'] is not None:
42 short_desc = programme['subtitle'] + ' '
43 if programme['categories'] is not None \
44 and len(programme['categories']) > 0 \
45 and programme['categories'][0] is not None:
46 short_desc += '[' + \
47 '/'.join(programme['categories']) + ']'
48 line = '\t'.join([str_or_empty(x)
49 for x in [tgd_channel,
50 tgd_start,
51 tgd_duration,
52 programme['title'],
53 short_desc,
54 programme['desc'],
55 'X',
56 'N']])
57 return line
60 def timestamp_from_xmltv_time(timestr):
61 return datetime.datetime.strptime(timestr, '%Y%m%d%H%M%S +0000')
63 def tgd_time_from_timestamp(timestamp):
64 t = timestamp_as_localtime(timestamp)
65 return t.strftime('%Y/%m/%d %H:%M')
67 def timestamp_as_localtime(timestamp):
68 # [FUCKO]
69 return timestamp + datetime.timedelta(0, 36000 + 3600, 0)
71 def tgd_duration_from_timedelta(duration):
72 return str(duration.seconds / 60)
74 def str_or_empty(s):
75 if s is not None:
76 return s
77 return ''
79 def tgd_filename_from_programme(programme):
80 t = timestamp_as_localtime(programme['start'])
81 return t.strftime('%Y%m%d.tgd')
83 sd_channels = ('2','22','3','7','9','10','23','32','72','99','12')
85 if __name__=='__main__':
86 filename = 'iceguide.xml'
87 parser = IceToTgd()
88 parser.use_xml_file(filename)
89 current_tgd_filename = None
90 current_tgd_file = None
91 for p in parser.programmes:
92 if parser.tgd_channel(p) in sd_channels:
93 new_tgd_filename = tgd_filename_from_programme(p)
94 if new_tgd_filename != current_tgd_filename:
95 current_tgd_file = open('out/' + new_tgd_filename, 'w')
96 line = parser.programme_to_tgd(p).encode('UTF-8')
97 current_tgd_file.write(line + '\r\n')