From 1231edabbc0e398c580cb59e5419d20c9b2735dd Mon Sep 17 00:00:00 2001 From: Matt Curtis Date: Fri, 6 Nov 2009 23:42:10 +1100 Subject: [PATCH] Ratings, subtitles, year in title Get rating from icetv Show subtitles/repeat Include year in title if known --- icetotgd.py | 61 +++++++++++++++++++++++++++++++++++++++++-------------- test_icetotgd.py | 62 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 108 insertions(+), 15 deletions(-) diff --git a/icetotgd.py b/icetotgd.py index 302142d..0aa5eee 100644 --- a/icetotgd.py +++ b/icetotgd.py @@ -23,7 +23,11 @@ class IceToTgd(object): 'desc': p.findtext('desc'), 'categories': [x.text for x in p.findall('category')], 'channel': p.get('channel'), - 'rating': None, + 'rating': p.findtext('rating/value'), + 'date': p.findtext('date'), + 'subtitles': p.findtext('subtitles'), + 'subtitles': p.findtext('subtitles'), + 'previously-shown': None if p.find('previously-shown') is None else p.find('previously-shown').get('start', ''), 'start': timestamp_from_xmltv_time(p.get('start')), 'stop': timestamp_from_xmltv_time(p.get('stop'))} for p in self.tree.findall('programme')] @@ -31,12 +35,13 @@ class IceToTgd(object): def tgd_channel(self, programme): return self.channels[programme['channel']]['lcn'] - def programme_to_tgd(self, programme): - tgd_channel = self.tgd_channel(programme) - tgd_start = tgd_time_from_timestamp(programme['start']) - duration = programme['stop'] - programme['start'] - tgd_duration = tgd_duration_from_timedelta(duration) - tgd_rating = 'X' + def tgd_title(self, programme): + title = programme['title'] + if 'date' in programme and programme['date'] is not None: + title += ' (%s)' % programme['date'] + return title + + def tgd_short_description(self, programme): short_desc = '' if programme['subtitle'] is not None: short_desc = programme['subtitle'] + ' ' @@ -45,14 +50,42 @@ class IceToTgd(object): and programme['categories'][0] is not None: short_desc += '[' + \ '/'.join(programme['categories']) + ']' + return short_desc + + def tgd_description(self, programme): + desc = programme['desc'] or '' + if 'subtitles' in programme: + desc += ' [Subtitles]' + if 'previously-shown' in programme and programme['previously-shown'] is not None: + datestr = programme['previously-shown'] + if datestr == '': + desc += ' [Repeat]' + else: + date = datetime.date(int(datestr[0:4]), int(datestr[4:6]), int(datestr[6:8])) + out = date.strftime('%x') + desc += ' [Repeat, last shown ' + out + ']' + return desc + + def tgd_rating(self, programme): + if 'rating' in programme and programme['rating'] is not None: + return programme['rating'] + return 'X' + + def programme_to_tgd(self, programme): + tgd_channel = self.tgd_channel(programme) + tgd_start = tgd_time_from_timestamp(programme['start']) + duration = programme['stop'] - programme['start'] + tgd_duration = tgd_duration_from_timedelta(duration) + tgd_rating = 'X' + line = '\t'.join([str_or_empty(x) for x in [tgd_channel, tgd_start, tgd_duration, - programme['title'], - short_desc, - programme['desc'], - 'X', + self.tgd_title(programme), + self.tgd_short_description(programme), + self.tgd_description(programme), + self.tgd_rating(programme), 'N']]) return line @@ -72,9 +105,7 @@ def tgd_duration_from_timedelta(duration): return str(duration.seconds / 60) def str_or_empty(s): - if s is not None: - return s - return '' + return s if s is not None else '' def tgd_filename_from_programme(programme): t = timestamp_as_localtime(programme['start']) @@ -92,6 +123,6 @@ if __name__=='__main__': if parser.tgd_channel(p) in sd_channels: new_tgd_filename = tgd_filename_from_programme(p) if new_tgd_filename != current_tgd_filename: - current_tgd_file = open('out/' + new_tgd_filename, 'w') + current_tgd_file = open('out/' + new_tgd_filename, 'a') line = parser.programme_to_tgd(p).encode('UTF-8') current_tgd_file.write(line + '\r\n') diff --git a/test_icetotgd.py b/test_icetotgd.py index 0fd3c15..98f3a83 100644 --- a/test_icetotgd.py +++ b/test_icetotgd.py @@ -31,6 +31,23 @@ class T(unittest.TestCase): 14328-72386 + + Suburb of the Moths + A suburb is terrorised by shrimp moths from hell. + + Dave Keenan + Marvin O'Gravel Ballon-Face + Oliver Boliver Butt + Zanzibar Buck-Buck McBean + + 1996 + Movie + + + M + + + '''} @@ -89,6 +106,51 @@ class T(unittest.TestCase): p = self.parser.programmes[1] self.assertEqual(p['desc'], None) + def test_programme_xml_with_year(self): + p = self.parser.programmes[2] + self.assertEqual(p['date'], '1996') + + def test_tgd_title_includes_year(self): + p = self.parser.programmes[2] + title = self.parser.tgd_title(p) + self.assertEqual(title, 'Suburb of the Moths (1996)') + + def test_unrated_programmes_are_rated_x(self): + p = self.parser.programmes[0] + rating = self.parser.tgd_rating(p) + self.assertEqual(rating, 'X') + + def test_can_get_programme_rating(self): + p = self.parser.programmes[2] + rating = self.parser.tgd_rating(p) + self.assertEqual(rating, 'M') + + def test_description_says_subtitles_if_they_exist(self): + p = self.parser.programmes[2] + description = self.parser.tgd_description(p) + self.assertTrue(description.index('[Subtitles]')) + + def test_description_doesnt_say_repeat_if_its_not_a_repeat(self): + p = self.parser.programmes[0] + description = self.parser.tgd_description(p) + self.assertTrue(description.find('[Repeat]') == -1) + + def test_description_says_repeat_if_its_a_repeat(self): + p = self.parser.programmes[2] + description = self.parser.tgd_description(p) + self.assertTrue(description.find('[Repeat]') != -1) + + def test_description_says_repeat_with_date_if_its_a_repeat_with_a_known_date(self): + p = self.parser.programmes[1] + description = self.parser.tgd_description(p) + date = datetime.date(2009, 9, 17).strftime('%x') + self.assertTrue(description.find('[Repeat, last shown ' + date + ']') != -1) + + def test_tgd_short_description_includes_category(self): + p = self.parser.programmes[0] + short_desc = self.parser.tgd_short_description(p) + self.assertEqual(short_desc, 'The One Where Spiderman Eats Salad [News/Sport]') + def test_programme_xml_without_desc(self): p = self.programme.copy() p['desc'] = None -- 2.11.4.GIT