-
Notifications
You must be signed in to change notification settings - Fork 0
/
mediathekdirekt.py
156 lines (122 loc) · 4.87 KB
/
mediathekdirekt.py
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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
#
# gPodder: Media and podcast aggregator
# Copyright (c) 2005-2014 Thomas Perl and the gPodder Team
#
# gPodder is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 3 of the License, or
# (at your option) any later version.
#
# gPodder is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
#
# MediathekDirect plugin for gPodder
# Thomas Perl <[email protected]>; 2014-10-29
import gpodder
from gpodder import registry
from gpodder import util
from gpodder import directory
import os
import re
import datetime
class MediathekDirektFeed(object):
KEYS = [
'station',
'show',
'episode',
'date',
'duration',
'description',
'enclosure',
'link',
'_unknown',
]
def __init__(self, station=None, show=None):
self.station = station
self.show = show
self.json_sources = [
'http://www.mediathekdirekt.de/good.json',
]
def was_updated(self):
return True
def get_etag(self, default):
return default
def get_modified(self, default):
return default
def get_title(self):
if self.station is None:
return 'Mediathekdirekt.de'
elif self.show is None:
return '{} (MediathekDirekt.de)'.format(self.station)
else:
return '{} auf {} (MediathekDirekt.de)'.format(self.show, self.station)
def get_image(self):
return 'http://www.mediathekdirekt.de/images/mediathekdirekt.png'
def get_link(self):
return 'http://www.mediathekdirekt.de/'
def get_description(self):
return 'MediathekDirekt ist eine Art "Suchmaschine" für die Inhalte der öffentlich-rechtlichen Fernsehmediatheken und ein einfaches Frontend für die mit MediathekView erstellte Filmliste.'
def get_payment_url(self):
return None
def _to_episode(self, track):
if track['time']:
dt = datetime.datetime.strptime(' '.join((track['date'], track['time'])), '%d.%m.%Y %H:%M:%S')
else:
dt = datetime.datetime.strptime(track['date'], '%d.%m.%Y')
return {
'title': '{} - {}'.format(track['station'], track['show']),
'subtitle': track['episode'],
'description': track['description'],
'published': int(dt.strftime('%s')),
#'duration': parse_duration(track['duration']),
'url': track['enclosure'],
'link': track['link'],
}
def _get_tracks(self):
for url in self.json_sources:
for track in util.read_json(url):
yield dict(list(zip(self.KEYS, track)))
def get_new_episodes(self, channel):
tracks = list(self._get_tracks())
existing_guids = [episode.guid for episode in channel.episodes]
seen_guids = []
new_episodes = []
for track in tracks:
if self.station is not None and track['station'] != self.station:
continue
if self.show is not None and track['show'] != self.show:
continue
seen_guids.append(track['enclosure'])
if track['enclosure'] not in existing_guids:
episode = channel.episode_factory(self._to_episode(track).items())
episode.save()
new_episodes.append(episode)
return new_episodes, seen_guids
@registry.feed_handler.register
def mediathekdirekt_feed_handler(channel, max_episodes):
m = re.match(r'http://www.mediathekdirekt.de/\?([^/]+)/(.*)', channel.url)
if channel.url == 'http://www.mediathekdirekt.de/':
return MediathekDirektFeed()
elif m:
return MediathekDirektFeed(m.group(1), m.group(2))
@registry.directory.register_instance
class MediathekDirektProvider(directory.Provider):
def __init__(self):
self.name = 'Mediathek Direkt'
self.kind = directory.Provider.PROVIDER_SEARCH
self.priority = 0
def on_search(self, query):
query = query.lower()
f = MediathekDirektFeed()
station_show_set = set()
for track in f._get_tracks():
if query in track['station'].lower() or query in track['show'].lower() or query in track['episode'].lower():
station_show_set.add((track['station'], track['show']))
for station, show in sorted(station_show_set):
yield directory.DirectoryEntry('{} auf {}'.format(show, station),
'http://www.mediathekdirekt.de/?{}/{}'.format(show, station))