Browse Source

Merge pull request #2052 from nrgaway/tv_database_2

Added Seasons
pull/2057/head
Joel Kåberg 12 years ago
parent
commit
21030e7cb4
  1. 40
      couchpotato/core/media/show/_base/main.py
  2. 8
      couchpotato/core/media/show/library/episode/main.py
  3. 42
      couchpotato/core/media/show/library/season/main.py
  4. 2
      couchpotato/core/media/show/library/show/main.py
  5. 222
      couchpotato/core/providers/show/thetvdb/main.py
  6. 2
      couchpotato/core/settings/model.py

40
couchpotato/core/media/show/_base/main.py

@ -83,7 +83,7 @@ class ShowBase(MediaBase):
'movie': movie_dict,
}
def debug(self):
def debug(self, identifier):
"""
XXX: This is only a hook for a breakpoint so we can test database stuff easily
REMOVE when finished
@ -99,7 +99,7 @@ class ShowBase(MediaBase):
import traceback
db = get_session()
#parent = db.query(Library).filter_by(identifier = attrs.get('')).first()
parent = db.query(Library).filter_by(identifier = identifier).first()
return
def add(self, params = {}, force_readd = True, search_after = True, update_library = False, status_id = None):
@ -126,18 +126,36 @@ class ShowBase(MediaBase):
# Add show parent to db first
parent = self.addToDatabase(params = params, type = 'show')
skip = False # XXX: For debugging
identifier = params.get('id')
episodes = fireEvent('show.episodes', identifier = identifier)
# XXX: Fix so we dont have a nested list
if episodes is not None and skip is False:
for episode in episodes[0]:
episode['title'] = episode.get('titles', None)[0]
episode['identifier'] = episode.get('id', None)
episode['parent_identifier'] = identifier
# XXX: add seasons
# XXX: Fix so we dont have a nested list [0] (fireEvent)
try:
seasons = fireEvent('season.info', identifier = identifier)[0]
except: return None
if seasons is not None:
for season in seasons:
season['title'] = season.get('title', None)
season_id = season.get('id', None)
if season_id is None: continue
season['identifier'] = season_id
season['parent_identifier'] = identifier
self.addToDatabase(params=season, type = "season")
# XXX: Fix so we dont have a nested list [0] (fireEvent)
try:
episodes = fireEvent('episode.info', identifier = identifier, season_identifier = season_id)[0]
except: continue
if episodes is not None:
for episode in episodes:
episode['title'] = episode.get('titles', None)[0] # XXX. [0] will create exception. FIX!
episode_id = episode.get('id', None)
if episode_id is None: continue
episode['identifier'] = episode_id
episode['parent_identifier'] = season['identifier']
self.addToDatabase(params=episode, type = "episode")
self.debug(str(identifier)) # XXX: Remove TODO: Add Show(extend Library) convience options for db seasching
return parent
def addToDatabase(self, params = {}, type="show", force_readd = True, search_after = True, update_library = False, status_id = None):

8
couchpotato/core/media/show/library/episode/main.py

@ -88,8 +88,12 @@ class EpisodeLibraryPlugin(LibraryBase):
if library.status_id == done_status.get('id') and not force:
do_update = False
info = fireEvent('episode.info', merge = True, identifier = identifier, \
parent_identifier = parent_identifier)
#info = fireEvent('episode.info', merge = True, identifier = identifier, \
# parent_identifier = parent_identifier)
#info = fireEvent('episode.info', merge = True, identifier = parent_identifier, \
# episode_identifier = identifier)
info = fireEvent('episode.info', merge = True, season_identifier = parent_identifier, \
episode_identifier = identifier)
# Don't need those here
try: del info['in_wanted']

42
couchpotato/core/media/show/library/season/main.py

@ -88,8 +88,8 @@ class SeasonLibraryPlugin(LibraryBase):
if library.status_id == done_status.get('id') and not force:
do_update = False
info = fireEvent('season.info', merge = True, identifier = identifier, \
parent_identifier = parent_identifier)
info = fireEvent('season.info', merge = True, identifier = parent_identifier, \
season_identifier = identifier)
# Don't need those here
try: del info['in_wanted']
@ -124,7 +124,9 @@ class SeasonLibraryPlugin(LibraryBase):
t = LibraryTitle(
title = title,
simple_title = self.simplifyTitle(title),
default = (len(default_title) == 0 and counter == 0) or len(titles) == 1 or title.lower() == toUnicode(default_title.lower()) or (toUnicode(default_title) == u'' and toUnicode(titles[0]) == title)
# XXX: default was None; so added a quick hack since we don't really need titiles for seasons anyway
#default = (len(default_title) == 0 and counter == 0) or len(titles) == 1 or title.lower() == toUnicode(default_title.lower()) or (toUnicode(default_title) == u'' and toUnicode(titles[0]) == title)
default = True,
)
library.titles.append(t)
counter += 1
@ -132,23 +134,23 @@ class SeasonLibraryPlugin(LibraryBase):
db.commit()
# Files
images = info.get('images', [])
for image_type in ['poster']:
for image in images.get(image_type, []):
if not isinstance(image, (str, unicode)):
continue
file_path = fireEvent('file.download', url = image, single = True)
if file_path:
file_obj = fireEvent('file.add', path = file_path, type_tuple = ('image', image_type), single = True)
try:
file_obj = db.query(File).filter_by(id = file_obj.get('id')).one()
library.files.append(file_obj)
db.commit()
break
except:
log.debug('Failed to attach to library: %s', traceback.format_exc())
#images = info.get('images', [])
#for image_type in ['poster']:
#for image in images.get(image_type, []):
#if not isinstance(image, (str, unicode)):
#continue
#file_path = fireEvent('file.download', url = image, single = True)
#if file_path:
#file_obj = fireEvent('file.add', path = file_path, type_tuple = ('image', image_type), single = True)
#try:
#file_obj = db.query(File).filter_by(id = file_obj.get('id')).one()
#library.files.append(file_obj)
#db.commit()
#break
#except:
#log.debug('Failed to attach to library: %s', traceback.format_exc())
library_dict = library.to_dict(self.default_dict)

2
couchpotato/core/media/show/library/show/main.py

@ -75,7 +75,7 @@ class ShowLibraryPlugin(LibraryBase):
do_update = True
info = fireEvent('show.info' % library.type, merge = True, identifier = identifier)
info = fireEvent('show.info', merge = True, identifier = identifier)
# Don't need those here
try: del info['in_wanted']

222
couchpotato/core/providers/show/thetvdb/main.py

@ -12,13 +12,17 @@ log = CPLog(__name__)
# should be returning an empty list or dictionary
# XXX: Consider grabbing zips to put less strain on tvdb
# XXX: Consider a cache; not implenented everywhere yet or at all
# XXX: Search by language; now ists defualt of "en"
# XXX: alternate titles do exist for show and episodes; add them
# XXX: Unicode stuff
# XXX: we have a getShow function but it it being used?
class TheTVDb(ShowProvider):
def __init__(self):
#addEvent('show.by_hash', self.byHash)
addEvent('show.search', self.search, priority = 1)
addEvent('show.info', self.getShowInfo, priority = 1)
addEvent('show.episodes', self.getEpisodes, priority = 1)
addEvent('season.info', self.getSeasonInfo, priority = 1)
addEvent('episode.info', self.getEpisodeInfo, priority = 1)
#addEvent('show.info_by_thetvdb', self.getInfoByTheTVDBId)
@ -76,6 +80,7 @@ class TheTVDb(ShowProvider):
search_string = simplifyString(q)
cache_key = 'thetvdb.cache.%s.%s' % (search_string, limit)
results = self.getCache(cache_key)
# TODO: cache is not returned
if not results:
log.debug('Searching for show: %s', q)
@ -83,7 +88,6 @@ class TheTVDb(ShowProvider):
raw = None
try:
raw = self.tvdb.search(search_string)
except (tvdb_exceptions.tvdb_error, IOError), e:
log.error('Failed searching TheTVDB for "%s": %s', (search_string, traceback.format_exc()))
return None
@ -114,40 +118,96 @@ class TheTVDb(ShowProvider):
return results
def getEpisodes(self, identifier=None, episode_identifier=None):
"""Either return a list of all episodes or a single episode.
If episode_identifer contains an episode to search for it will be returned if found
def getShow(self, identifier = None):
show = None
try:
log.debug('Getting show: %s', identifier)
show = self.tvdb[int(identifier)]
except (tvdb_exceptions.tvdb_error, IOError), e:
log.error('Failed to getShowInfo for show id "%s": %s', (identifier, traceback.format_exc()))
return None
return show
def getSeasonInfo(self, identifier=None, season_identifier=None):
"""Either return a list of all seasons or a single season by number.
identifier is the show 'id'
"""
if not identifier:
return None
# season_identifier must contain the 'show id : season number' since there is no tvdb id
# for season and we need a reference to both the show id and season number
if season_identifier:
try: season_identifier = int(season_identifier.split(':')[1])
except: return None
cache_key = 'thetvdb.cache.%s.%s' % (identifier, season_identifier)
log.debug('Getting SeasonInfo: %s', cache_key)
result = self.getCache(cache_key) or {}
if result:
return result
try:
show = self.tvdb[int(identifier)]
except (tvdb_exceptions.tvdb_error, IOError), e:
log.error('Failed parsing TheTVDB for "%s" id "%s": %s', (show, identifier, traceback.format_exc()))
log.error('Failed parsing TheTVDB SeasonInfo for "%s" id "%s": %s', (show, identifier, traceback.format_exc()))
return None
result = []
for season in show.values():
for episode in season.values():
if episode_identifier:
if episode['id'] == toUnicode(episode_identifier):
return episode
for number, season in show.items():
if season_identifier is not None and number == season_identifier:
result = self.parseSeason(show, (number, season))
self.setCache(cache_key, result)
return result
else:
result.append(self.parseEpisode(show, episode))
result.append(self.parseSeason(show, (number, season)))
self.setCache(cache_key, result)
return result
def getEpisodeInfo(self, identifier=None, season_identifier=None, episode_identifier=None):
"""Either return a list of all episodes or a single episode.
If episode_identifer contains an episode number to search for
"""
if not identifier and season_identifier is None:
return None
# season_identifier must contain the 'show id : season number' since there is no tvdb id
# for season and we need a reference to both the show id and season number
if season_identifier:
try:
identifier, season_identifier = season_identifier.split(':')
season_identifier = int(season_identifier)
except: return None
cache_key = 'thetvdb.cache.%s.%s.%s' % (identifier, episode_identifier, season_identifier)
log.debug('Getting EpisodeInfo: %s', cache_key)
result = self.getCache(cache_key) or {}
if result:
return result
def getShow(self, identifier = None):
show = None
try:
log.debug('Getting show: %s', identifier)
show = self.tvdb[int(identifier)]
except (tvdb_exceptions.tvdb_error, IOError), e:
log.error('Failed to getShowInfo for show id "%s": %s', (identifier, traceback.format_exc()))
log.error('Failed parsing TheTVDB EpisodeInfo for "%s" id "%s": %s', (show, identifier, traceback.format_exc()))
return None
return show
result = []
for number, season in show.items():
if season_identifier is not None and number != season_identifier:
continue
for episode in season.values():
if episode_identifier is not None and episode['id'] == toUnicode(episode_identifier):
result = self.parseEpisode(show, episode)
self.setCache(cache_key, result)
return result
else:
result.append(self.parseEpisode(show, episode))
self.setCache(cache_key, result)
return result
def getShowInfo(self, identifier = None):
if not identifier:
@ -166,26 +226,6 @@ class TheTVDb(ShowProvider):
return result
def getEpisodeInfo(self, identifier = None, parent_identifier = None):
if not identifier or not parent_identifier:
return None
cache_key = 'thetvdb.cache.%s.%s' % (parent_identifier, identifier)
log.debug('Getting EpisodeInfo: %s', cache_key)
result = self.getCache(cache_key) or {}
if result:
return result
show = self.getShow(identifier = parent_identifier)
if show:
episode = self.getEpisodes(identifier=parent_identifier, episode_identifier=identifier)
if episode:
result = self.parseEpisode(show, episode)
self.setCache(cache_key, result)
return result
#def getInfoByTheTVDBId(self, id = None):
#cache_key = 'thetvdb.cache.%s' % id
@ -247,12 +287,10 @@ class TheTVDb(ShowProvider):
#poster_original = self.getImage(show, type = 'poster', size = 'original')
#backdrop_original = self.getImage(show, type = 'backdrop', size = 'original')
## Genres
genres = [] if show['genre'] is None else show['genre'].strip('|').split('|')
## Year
if show['firstaired']:
year = datetime.strptime(show['firstaired'], '%Y-%m-%d').year
if show['firstaired'] is not None:
try: year = datetime.strptime(show['firstaired'], '%Y-%m-%d').year
except: year = None
else:
year = None
@ -268,24 +306,69 @@ class TheTVDb(ShowProvider):
'poster_original': [],
'backdrop_original': [],
},
'imdb': show['imdb_id'],
'runtime': show['runtime'],
'released': show['firstaired'],
'year': year,
'plot': show['overview'],
'genres': genres,
'imdb': show['imdb_id'],
'zap2it_id': show['zap2it_id'],
'seriesid': show['seriesid'],
'network': show['network'],
'networkid': show['networkid'],
'airs_dayofweek': show['airs_dayofweek'],
'airs_time': show['airs_time'],
'firstaired': show['firstaired'],
'released': show['firstaired'],
'runtime': show['runtime'],
'contentrating': show['contentrating'],
'rating': show['rating'],
'ratingcount': show['ratingcount'],
'actors': show['actors'],
'lastupdated': show['lastupdated'],
'status': show['status'],
'language': show['language'],
}
show_data = dict((k, v) for k, v in show_data.iteritems() if v)
## Add alternative names
#for alt in ['original_name', 'alternative_name']:
#alt_name = toUnicode(show.get(alt))
#alt_name = toUnicode(show['alt))
#if alt_name and not alt_name in show_data['titles'] and alt_name.lower() != 'none' and alt_name != None:
#show_data['titles'].append(alt_name)
return show_data
def parseSeason(self, show, season_tuple):
"""
contains no data
"""
number, season = season_tuple
title = toUnicode('%s - Season %s' % (show['seriesname'], str(number)))
# XXX: work on title; added defualt_title to fix an error
season_data = {
'id': (show['id'] + ':' + str(number)),
'type': 'season',
'primary_provider': 'thetvdb',
'titles': [title, ],
'original_title': title,
'via_thetvdb': True,
'parent_identifier': show['id'],
'seasonnumber': str(number),
'images': {
'poster': [],
'backdrop': [],
'poster_original': [],
'backdrop_original': [],
},
'year': None,
'genres': None,
'imdb': None,
}
season_data = dict((k, v) for k, v in season_data.iteritems() if v)
return season_data
def parseEpisode(self, show, episode):
"""
('episodenumber', u'1'),
@ -327,17 +410,17 @@ class TheTVDb(ShowProvider):
#backdrop = self.getImage(episode, type = 'fanart', size = 'w1280')
##poster_original = self.getImage(episode, type = 'poster', size = 'original')
##backdrop_original = self.getImage(episode, type = 'backdrop', size = 'original')
poster = episode['filename'] or []
backdrop = []
## Genres
genres = []
plot = "%s - %sx%s - %s" % (show['seriesname'], episode['seasonnumber'], episode['episodenumber'], episode['overview'])
## Year
if episode['firstaired']:
year = datetime.strptime(episode['firstaired'], '%Y-%m-%d').year
plot = "%s - %sx%s - %s" % (show['seriesname'],
episode['seasonnumber'],
episode['episodenumber'],
episode['overview'])
if episode['firstaired'] is not None:
try: year = datetime.strptime(episode['firstaired'], '%Y-%m-%d').year
except: year = None
else:
year = None
@ -348,7 +431,7 @@ class TheTVDb(ShowProvider):
'via_thetvdb': True,
'thetvdb_id': int(episode['id']),
'titles': [episode['episodename'], ],
'original_title': episode['episodename'],
'original_title': episode['episodename'] ,
'images': {
'poster': [poster] if poster else [],
'backdrop': [backdrop] if backdrop else [],
@ -362,13 +445,38 @@ class TheTVDb(ShowProvider):
'plot': plot,
'genres': genres,
'parent_identifier': show['id'],
'seasonnumber': episode['seasonnumber'],
'episodenumber': episode['episodenumber'],
'combined_episodenumber': episode['combined_episodenumber'],
'absolute_number': episode['absolute_number'],
'combined_season': episode['combined_season'],
'productioncode': episode['productioncode'],
'seriesid': episode['seriesid'],
'seasonid': episode['seasonid'],
'firstaired': episode['firstaired'],
'thumb_added': episode['thumb_added'],
'thumb_height': episode['thumb_height'],
'thumb_width': episode['thumb_width'],
'rating': episode['rating'],
'ratingcount': episode['ratingcount'],
'epimgflag': episode['epimgflag'],
'dvd_episodenumber': episode['dvd_episodenumber'],
'dvd_discid': episode['dvd_discid'],
'dvd_chapter': episode['dvd_chapter'],
'dvd_season': episode['dvd_season'],
'tms_export': episode['tms_export'],
'writer': episode['writer'],
'director': episode['director'],
'gueststars': episode['gueststars'],
'lastupdated': episode['lastupdated'],
'language': episode['language'],
}
episode_data = dict((k, v) for k, v in episode_data.iteritems() if v)
## Add alternative names
#for alt in ['original_name', 'alternative_name']:
#alt_name = toUnicode(episode.get(alt))
#alt_name = toUnicode(episode['alt))
#if alt_name and not alt_name in episode_data['titles'] and alt_name.lower() != 'none' and alt_name != None:
#episode_data['titles'].append(alt_name)

2
couchpotato/core/settings/model.py

@ -94,7 +94,7 @@ class Library(Entity):
type = Field(String(10), default="movie", index=True)
primary_provider = Field(String(10), default="imdb", index=True)
year = Field(Integer)
identifier = Field(String(20), index = True)
identifier = Field(String(40), index = True)
plot = Field(UnicodeText)
tagline = Field(UnicodeText(255))

Loading…
Cancel
Save