forked from mueslo/pythonBits
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtvdb.py
More file actions
117 lines (99 loc) · 4.56 KB
/
Copy pathtvdb.py
File metadata and controls
117 lines (99 loc) · 4.56 KB
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
# -*- coding: utf-8 -*-
from __future__ import (absolute_import, division,
print_function, unicode_literals)
from builtins import * # noqa: F401, F403
import tvdb_api
class TvdbResult(object):
def __init__(self, show, season, episode=None):
self.show = show
self.season = season
self.episode = episode
def banner(self, season_number=-1):
# todo offer choice of cover if multiple?
# todo cache banner upload per season?
# get best banner, preferably for season
def best_banner(banners): return sorted(
banners, key=lambda b: float(b.get('rating', 0)))[-1]
try:
season_banners = self.show['_banners']['season']['season']
season_banners = [
banner for banner in season_banners.values()
if banner['season'] == str(season_number)]
return best_banner(season_banners)['_bannerpath']
except (IndexError, KeyError):
# failing that, use show banner. if there's no banner at all we
# just error out for now
series_banners = [v for r in self.show['_banners']
['poster'].values() for k, v in r.items()]
return best_banner(series_banners)['_bannerpath']
def summary(self):
return {
'title': self.show['seriesname'],
'network': self.show['network'],
'genres': self.show['genre'].strip('|').split('|'),
'seriessummary': self.show['overview'],
'cast': self.show['_actors'],
# 'seasons': len(self.show),
# 'status': self.show['status'],
'contentrating': self.show['contentrating']
}
class TvdbSeason(TvdbResult):
def summary(self):
s = super(TvdbSeason, self).summary()
some_episode = next(iter(self.season.values()))
season_number = some_episode['seasonnumber']
series_id = some_episode['seriesid']
season_id = some_episode['seasonid']
s.update(**{'num_episodes': len(self.season),
'episodes': []})
for episode_number in self.season:
episode = self.season[episode_number]
s["episodes"].append({
'title': episode['episodename'],
'url': "http://thetvdb.com/?tab=episode&seriesid=" + series_id
+ "&seasonid=" + season_id +
"&id=" + episode['id'],
'imdb_id': episode['imdb_id'],
'rating': (episode['rating'] and
float(episode['rating']), 10)})
s['url'] = ("http://thetvdb.com/?tab=episode&seriesid=" + series_id +
"&seasonid=" + season_id)
s['cover'] = self.banner(season_number)
s['season'] = season_number
s['imdb_id'] = self.show['imdb_id']
return s
class TvdbEpisode(TvdbResult):
def summary(self):
summary = super(TvdbEpisode, self).summary()
summary.update(**{
'season': self.episode['seasonnumber'],
'episode': self.episode['episodenumber'],
'episode_title': self.episode['episodename'],
'imdb_id': self.episode['imdb_id'],
'director': self.episode['director'],
'air_date': self.episode['firstaired'],
# 'air_dow': self.show['airs_dayofweek'],
# 'air_time': self.show['airs_time'],
'writers': (self.episode['writer'] or
"").strip('|').split('|'),
'rating': (self.episode['rating'] and
float(self.episode['rating']), 10),
'votes': self.episode['ratingcount'],
'episodesummary': self.episode['overview'],
'language': self.episode['language'],
'url': "http://thetvdb.com/?tab=episode&seriesid=" +
self.episode['seriesid'] + "&seasonid=" +
self.episode['seasonid'] + "&id=" + self.episode['id'],
'cover': self.banner(self.episode['seasonnumber'])})
return summary
class TVDB(object):
def __init__(self):
# todo: selectfirst=False
self.tvdb = tvdb_api.Tvdb(banners=True, actors=True)
def search(self, tv_specifier):
show = self.tvdb[tv_specifier.title]
season = show[tv_specifier.season]
if tv_specifier.episode is not None:
episode = season[tv_specifier.episode]
return TvdbEpisode(show, season, episode)
return TvdbSeason(show, season)