def handle_tmdb_tvshows(results, local_first=True, sortkey='year'): tvshows = [] response = get_tmdb_data( 'genre/tv/list?language=%s&' % xbmcaddon.Addon().getSetting('LanguageID'), 30) id_list = [item['id'] for item in response['genres']] label_list = [item['name'] for item in response['genres']] for tv in results: tmdb_id = Utils.fetch(tv, 'id') artwork = get_image_urls(poster=tv.get('poster_path'), fanart=tv.get('backdrop_path')) if 'genre_ids' in tv: genre_list = [ label_list[id_list.index(genre_id)] for genre_id in tv['genre_ids'] if genre_id in id_list ] genres = ' / '.join(genre_list) else: genres = '' duration = '' if 'episode_run_time' in tv: if len(tv['episode_run_time']) > 1: duration = '%i - %i' % (min( tv['episode_run_time']), max(tv['episode_run_time'])) elif len(tv['episode_run_time']) == 1: duration = '%i' % tv['episode_run_time'][0] newtv = { 'title': Utils.fetch(tv, 'name'), 'TVShowTitle': Utils.fetch(tv, 'name'), 'OriginalTitle': Utils.fetch(tv, 'original_name'), 'duration': duration, 'id': tmdb_id, 'genre': genres, 'country': Utils.fetch(tv, 'original_language'), 'Popularity': Utils.fetch(tv, 'popularity'), 'credit_id': Utils.fetch(tv, 'credit_id'), 'Plot': Utils.fetch(tv, 'overview'), 'Trailer': 'plugin://script.extendedinfo?info=tvtrailer&&id=%s' % tmdb_id, 'year': Utils.get_year(Utils.fetch(tv, 'first_air_date')), 'media_type': 'tv', 'mediatype': 'tvshow', 'character': Utils.fetch(tv, 'character'), 'path': 'plugin://script.extendedinfo?info=extendedtvinfo&&id=%s' % tmdb_id, 'Rating': Utils.fetch(tv, 'vote_average'), 'User_Rating': str(Utils.fetch(tv, 'rating')), 'Votes': Utils.fetch(tv, 'vote_count'), 'TotalEpisodes': Utils.fetch(tv, 'number_of_episodes'), 'TotalSeasons': Utils.fetch(tv, 'number_of_seasons'), 'Release_Date': Utils.fetch(tv, 'first_air_date'), 'Premiered': Utils.fetch(tv, 'first_air_date') } newtv.update(artwork) date = Utils.fetch(tv, 'first_air_date') tvshows.append(newtv) tvshows = local_db.merge_with_local_tvshow_info(tvshows, local_first, sortkey) return tvshows
def extended_tvshow_info(tvshow_id=None, cache_time=7, dbid=None): if not tvshow_id: return None session_str = '' response = get_tmdb_data( 'tv/%s?append_to_response=alternative_titles,content_ratings,credits,external_ids,images,keywords,rating,similar,translations,videos&language=%s&include_image_language=en,null,%s&%s' % (tvshow_id, xbmcaddon.Addon().getSetting('LanguageID'), xbmcaddon.Addon().getSetting('LanguageID'), session_str), cache_time) if not response: return False videos = handle_tmdb_videos( response['videos']['results']) if 'videos' in response else [] tmdb_id = Utils.fetch(response, 'id') external_ids = Utils.fetch(response, 'external_ids') if external_ids: imdb_id = Utils.fetch(external_ids, 'imdb_id') freebase_id = Utils.fetch(external_ids, 'freebase_id') tvdb_id = Utils.fetch(external_ids, 'tvdb_id') tvrage_id = Utils.fetch(external_ids, 'tvrage_id') artwork = get_image_urls(poster=response.get('poster_path'), fanart=response.get('backdrop_path')) if len(response.get('episode_run_time', -1)) > 1: duration = '%i - %i' % (min( response['episode_run_time']), max(response['episode_run_time'])) elif len(response.get('episode_run_time', -1)) == 1: duration = '%i' % response['episode_run_time'][0] else: duration = '' us_cert = Utils.dictfind(response['content_ratings']['results'], 'iso_3166_1', 'US') if us_cert: mpaa = us_cert['rating'] elif response['content_ratings']['results']: mpaa = response['content_ratings']['results'][0]['rating'] else: mpaa = '' genres = [item['name'] for item in response['genres']] tvshow = { 'title': Utils.fetch(response, 'name'), 'TVShowTitle': Utils.fetch(response, 'name'), 'OriginalTitle': Utils.fetch(response, 'original_name'), 'duration': duration, 'duration(h)': Utils.format_time(duration, 'h'), 'duration(m)': Utils.format_time(duration, 'm'), 'id': tmdb_id, 'tmdb_id': tmdb_id, 'imdb_id': imdb_id, 'freebase_id': freebase_id, 'tvdb_id': tvdb_id, 'tvrage_id': tvrage_id, 'mpaa': mpaa, 'genre': ' / '.join(genres), 'credit_id': Utils.fetch(response, 'credit_id'), 'Plot': Utils.clean_text(Utils.fetch(response, 'overview')), 'year': Utils.get_year(Utils.fetch(response, 'first_air_date')), 'media_type': 'tv', 'Popularity': Utils.fetch(response, 'popularity'), 'Rating': Utils.fetch(response, 'vote_average'), 'country': Utils.fetch(response, 'original_language'), 'User_Rating': str(Utils.fetch(response, 'rating')), 'Votes': Utils.fetch(response, 'vote_count'), 'Status': translate_status(Utils.fetch(response, 'status')), 'path': 'plugin://script.extendedinfo?info=extendedtvinfo&&id=%s' % tvshow_id, 'trailer': 'plugin://script.extendedinfo?info=playtvtrailer&&id=%s' % tvshow_id, 'ShowType': Utils.fetch(response, 'type'), 'homepage': Utils.fetch(response, 'homepage'), 'last_air_date': Utils.fetch(response, 'last_air_date'), 'first_air_date': Utils.fetch(response, 'first_air_date'), 'TotalEpisodes': Utils.fetch(response, 'number_of_episodes'), 'TotalSeasons': Utils.fetch(response, 'number_of_seasons'), 'in_production': Utils.fetch(response, 'in_production'), 'Release_Date': Utils.fetch(response, 'first_air_date'), 'Premiered': Utils.fetch(response, 'first_air_date') } tvshow.update(artwork) if dbid: local_item = local_db.get_tvshow_from_db(dbid) tvshow.update(local_item) else: tvshow = local_db.merge_with_local_tvshow_info([tvshow])[0] tvshow['Rating'] = Utils.fetch(response, 'vote_average') listitems = { 'actors': handle_tmdb_people(response['credits']['cast']), 'similar': handle_tmdb_tvshows(response['similar']['results']), 'studios': handle_tmdb_misc(response['production_companies']), 'networks': handle_tmdb_misc(response['networks']), 'certifications': handle_tmdb_misc(response['content_ratings']['results']), 'crew': handle_tmdb_people(response['credits']['crew']), 'genres': handle_tmdb_misc(response['genres']), 'keywords': handle_tmdb_misc(response['keywords']['results']), 'videos': videos, 'seasons': handle_tmdb_seasons(response['seasons']), 'images': handle_tmdb_images(response['images']['posters']), 'backdrops': handle_tmdb_images(response['images']['backdrops']) } return (tvshow, listitems)