def pull_show(watched, rated, directory, tvdb_id): # Sync watched if Prefs['sync_watched'] is True: for show in [x for x in watched if x['tvdb_id'] == tvdb_id]: Log.Debug('We have a match for %s' % show['title']) episodes = PMS.get_metadata_leaves(directory.get('ratingKey')) if not episodes: Log.Warn('Unable to fetch episodes for show with id %s' % directory.get('ratingKey')) continue for episode in episodes.xpath('//Video'): season_num = try_convert(episode.get('parentIndex'), int) episode_num = try_convert(episode.get('index'), int) # Skip episodes with missing season or episode numbers if season_num is None or episode_num is None: continue for season in matches(season_num, show['seasons'], lambda x: int(x['season'])): if episode_num in season['episodes']: Log.Debug('Marking %s episode %s with key: %s as seen.' % ( episode.get('grandparentTitle'), episode.get('title'), episode.get('ratingKey') )) if not PMS.scrobble(episode): Log.Debug('The episode %s is already marked as seen in the library.' % episode.get('title')) # Sync ratings if Prefs['sync_ratings'] is True: for show in [x for x in rated if x['show']['tvdb_id'] == tvdb_id]: show_season = try_convert(show['episode']['season'], int) show_episode = try_convert(show['episode']['number'], int) # Skip episodes with missing season or episode numbers if show_season is None or show_episode is None: continue episodes = PMS.get_metadata_leaves(directory.get('ratingKey')) if not episodes: Log.Warn('Unable to fetch episodes for show with id %s' % directory.get('ratingKey')) continue for episode in episodes.xpath('//Video'): if show_season == int(episode.get('parentIndex')) and show_episode == int(episode.get('index')): PMS.rate(episode, show['rating_advanced'])
def push_show(all_episodes, collected, rated, directory): tvdb_id = match_tvdb_id(directory.get('ratingKey')) if not tvdb_id: return tv_show = { 'title': directory.get('title') } year = directory.get('year', None) if year is not None: tv_show['year'] = int(year) if tvdb_id is not None: tv_show['tvdb_id'] = tvdb_id seen_episodes = [] collected_episodes = [] episodes = PMS.get_metadata_leaves(directory.get('ratingKey')) if not episodes: Log.Warn('Unable to fetch episodes for show with id %s' % directory.get('ratingKey')) return for episode, parentIndex, index in iterget(episodes.xpath('//Video'), ['parentIndex', 'index']): # Ensure we have valid data if parentIndex is None or index is None: Log.Warn('Episode missing required data, skipping (key: %s)' % episode.get('ratingKey')) continue season_num = int(parentIndex) episode_num = int(index) base_episode = { 'season': season_num, 'episode': episode_num } collected_episodes.append(base_episode) if episode.get('viewCount') > 0: seen_episodes.append(extend(base_episode)) if episode.get('userRating') is not None: rating_episode = extend(base_episode, { 'title': directory.get('title'), 'rating': int(episode.get('userRating')) }) if year is not None: rating_episode['year'] = year if tvdb_id is not None: rating_episode['tvdb_id'] = tvdb_id rated.append(rating_episode) if len(seen_episodes) > 0: seen_tv_show = { 'title': directory.get('title') } if year is not None: seen_tv_show['year'] = year if tvdb_id is not None: seen_tv_show['tvdb_id'] = tvdb_id seen_tv_show['episodes'] = seen_episodes all_episodes.append(seen_tv_show) tv_show['episodes'] = collected_episodes collected.append(tv_show)