Merge branch 'pete'
[xmltvtotgd.git] / icetotgd.py
blob0aa5eee7f035d4dd85413cddf7fd94a48f2e4a0e
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': p.findtext('rating/value'),
27 'date': p.findtext('date'),
28 'subtitles': p.findtext('subtitles'),
29 'subtitles': p.findtext('subtitles'),
30 'previously-shown': None if p.find('previously-shown') is None else p.find('previously-shown').get('start', ''),
31 'start': timestamp_from_xmltv_time(p.get('start')),
32 'stop': timestamp_from_xmltv_time(p.get('stop'))}
33 for p in self.tree.findall('programme')]
35 def tgd_channel(self, programme):
36 return self.channels[programme['channel']]['lcn']
38 def tgd_title(self, programme):
39 title = programme['title']
40 if 'date' in programme and programme['date'] is not None:
41 title += ' (%s)' % programme['date']
42 return title
44 def tgd_short_description(self, programme):
45 short_desc = ''
46 if programme['subtitle'] is not None:
47 short_desc = programme['subtitle'] + ' '
48 if programme['categories'] is not None \
49 and len(programme['categories']) > 0 \
50 and programme['categories'][0] is not None:
51 short_desc += '[' + \
52 '/'.join(programme['categories']) + ']'
53 return short_desc
55 def tgd_description(self, programme):
56 desc = programme['desc'] or ''
57 if 'subtitles' in programme:
58 desc += ' [Subtitles]'
59 if 'previously-shown' in programme and programme['previously-shown'] is not None:
60 datestr = programme['previously-shown']
61 if datestr == '':
62 desc += ' [Repeat]'
63 else:
64 date = datetime.date(int(datestr[0:4]), int(datestr[4:6]), int(datestr[6:8]))
65 out = date.strftime('%x')
66 desc += ' [Repeat, last shown ' + out + ']'
67 return desc
69 def tgd_rating(self, programme):
70 if 'rating' in programme and programme['rating'] is not None:
71 return programme['rating']
72 return 'X'
74 def programme_to_tgd(self, programme):
75 tgd_channel = self.tgd_channel(programme)
76 tgd_start = tgd_time_from_timestamp(programme['start'])
77 duration = programme['stop'] - programme['start']
78 tgd_duration = tgd_duration_from_timedelta(duration)
79 tgd_rating = 'X'
81 line = '\t'.join([str_or_empty(x)
82 for x in [tgd_channel,
83 tgd_start,
84 tgd_duration,
85 self.tgd_title(programme),
86 self.tgd_short_description(programme),
87 self.tgd_description(programme),
88 self.tgd_rating(programme),
89 'N']])
90 return line
93 def timestamp_from_xmltv_time(timestr):
94 return datetime.datetime.strptime(timestr, '%Y%m%d%H%M%S +0000')
96 def tgd_time_from_timestamp(timestamp):
97 t = timestamp_as_localtime(timestamp)
98 return t.strftime('%Y/%m/%d %H:%M')
100 def timestamp_as_localtime(timestamp):
101 # [FUCKO]
102 return timestamp + datetime.timedelta(0, 36000 + 3600, 0)
104 def tgd_duration_from_timedelta(duration):
105 return str(duration.seconds / 60)
107 def str_or_empty(s):
108 return s if s is not None else ''
110 def tgd_filename_from_programme(programme):
111 t = timestamp_as_localtime(programme['start'])
112 return t.strftime('%Y%m%d.tgd')
114 sd_channels = ('2','22','3','7','9','10','23','32','72','99','12')
116 if __name__=='__main__':
117 filename = 'iceguide.xml'
118 parser = IceToTgd()
119 parser.use_xml_file(filename)
120 current_tgd_filename = None
121 current_tgd_file = None
122 for p in parser.programmes:
123 if parser.tgd_channel(p) in sd_channels:
124 new_tgd_filename = tgd_filename_from_programme(p)
125 if new_tgd_filename != current_tgd_filename:
126 current_tgd_file = open('out/' + new_tgd_filename, 'a')
127 line = parser.programme_to_tgd(p).encode('UTF-8')
128 current_tgd_file.write(line + '\r\n')