def map_show_keys(data): keep_data = {} del_keys = [] new_data = {} for k, v in iteritems(data): k_org = k k = k.lower() if None is not v: if k in ['banner', 'fanart', 'poster'] and v: v = self.config['url_artworks'] % v elif 'genre' == k: keep_data['genre_list'] = v v = '|%s|' % '|'.join([clean_data(c) for c in v if isinstance(c, string_types)]) elif 'gueststars' == k: keep_data['gueststars_list'] = v v = '|%s|' % '|'.join([clean_data(c) for c in v if isinstance(c, string_types)]) elif 'writers' == k: keep_data[k] = v v = '|%s|' % '|'.join([clean_data(c) for c in v if isinstance(c, string_types)]) elif 'rating' == k: new_data['contentrating'] = v elif 'firstaired' == k: if v: try: v = parse(v, fuzzy=True).strftime('%Y-%m-%d') except (BaseException, Exception): v = None else: v = None elif 'imdbid' == k: if v: if re.search(r'^(tt)?\d{1,9}$', v, flags=re.I): v = clean_data(v) else: v = '' else: v = clean_data(v) else: if 'seriesname' == k: if isinstance(data.get('aliases'), list) and 0 < len(data.get('aliases')): v = data['aliases'].pop(0) # this is a invalid show, it has no Name if None is v: return None if k in map_show: k = map_show[k] if k_org is not k: del_keys.append(k_org) new_data[k] = v else: data[k] = v for d in del_keys: del (data[d]) if isinstance(data, dict): data.update(new_data) data.update(keep_data) return data
def get_episode_data(self, epid): # Parse episode information data = None log.debug('Getting all episode data for %s' % epid) url = self.config['url_episodes_info'] % epid episode_data = self._getetsrc(url, language=self.config['language']) if episode_data and 'data' in episode_data: data = episode_data['data'] if isinstance(data, dict): for k, v in iteritems(data): k = k.lower() if None is not v: if 'filename' == k and v: v = self.config['url_artworks'] % v else: v = clean_data(v) data[k] = v return data
def _get_show_data(self, sid, language, get_ep_info=False): """Takes a series ID, gets the epInfo URL and parses the TVDB XML file into the shows dict in layout: shows[series_id][season_number][episode_number] """ # Parse show information log.debug('Getting all series data for %s' % sid) url = self.config['url_series_info'] % sid show_data = self._getetsrc(url, language=language) # check and make sure we have data to process and that it contains a series name if not (show_data and 'seriesname' in show_data.get('data', {}) or {}): return False for k, v in iteritems(show_data['data']): self._set_show_data(sid, k, v) if sid in self.shows: self.shows[sid].ep_loaded = get_ep_info for img_type, en_type in [(u'poster', 'posters_enabled'), (u'banner', 'banners_enabled'), (u'fanart', 'fanart_enabled'), (u'season', 'seasons_enabled'), (u'seasonwide', 'seasonwides_enabled')]: self._parse_images(sid, language, show_data, img_type, en_type) if self.config['actors_enabled']: actor_data = self._getetsrc(self.config['url_actors_info'] % sid, language=language) if actor_data and 0 < len(actor_data.get('data', '') or ''): self._parse_actors(sid, actor_data['data']) if get_ep_info: # Parse episode data log.debug('Getting all episodes of %s' % sid) page = 1 episodes = [] while page <= 400: episode_data = self._getetsrc( self.config['url_series_episodes_info'] % (sid, page), language=language) if None is episode_data: raise TvdbError('Exception retrieving episodes for show') if isinstance(episode_data, dict) and not episode_data.get('data', []): if 1 != page: self.not_found = False break if not getattr(self, 'not_found', False) and None is not episode_data.get('data'): episodes.extend(episode_data['data']) next_link = episode_data.get('links', {}).get('next', None) # check if page is a valid following page if not isinstance(next_link, integer_types) or next_link <= page: next_link = None if not next_link and isinstance(episode_data, dict) \ and isinstance(episode_data.get('data', []), list) and 100 > len(episode_data.get('data', [])): break if next_link: page = next_link else: page += 1 ep_map_keys = { 'absolutenumber': u'absolute_number', 'airedepisodenumber': u'episodenumber', 'airedseason': u'seasonnumber', 'airedseasonid': u'seasonid', 'dvdepisodenumber': u'dvd_episodenumber', 'dvdseason': u'dvd_season' } for cur_ep in episodes: if self.config['dvdorder']: log.debug('Using DVD ordering.') use_dvd = None is not cur_ep.get( 'dvdseason') and None is not cur_ep.get( 'dvdepisodenumber') else: use_dvd = False if use_dvd: elem_seasnum, elem_epno = cur_ep.get( 'dvdseason'), cur_ep.get('dvdepisodenumber') else: elem_seasnum, elem_epno = cur_ep.get( 'airedseason'), cur_ep.get('airedepisodenumber') if None is elem_seasnum or None is elem_epno: log.warning( 'An episode has incomplete season/episode number (season: %r, episode: %r)' % (elem_seasnum, elem_epno)) continue # Skip to next episode # float() is because https://github.com/dbr/tvnamer/issues/95 - should probably be fixed in TVDB data seas_no = int(float(elem_seasnum)) ep_no = int(float(elem_epno)) for k, v in iteritems(cur_ep): k = k.lower() if None is not v: if 'filename' == k and v: v = self.config['url_artworks'] % v else: v = clean_data(v) if k in ep_map_keys: k = ep_map_keys[k] self._set_item(sid, seas_no, ep_no, k, v) return True