def get_episodes_watchcount(
         self, unique_id, id_type, season=None, exclude_specials=True,
         tvshow=None, count_progress=False):
     """
     Get the number of episodes watched in a show or season
     Pass tvshow dict directly for speed otherwise will look-up ID from watched sync list
     Use count_progress to check progress against reset_at value rather than just count watched
     """
     season = try_int(season) if season is not None else None
     if not tvshow and id_type and unique_id:
         tvshow = self.get_sync('watched', 'show', id_type).get(unique_id)
     if not tvshow:
         return
     reset_at = None
     if count_progress and tvshow.get('reset_at'):
         reset_at = convert_timestamp(tvshow['reset_at'])
     count = 0
     for i in tvshow.get('seasons', []):
         if season is not None and i.get('number', -1) != season:
             continue
         if exclude_specials and i.get('number') == 0:
             continue
         # Reset_at is None so just count length of watched episode list
         if not reset_at:
             count += len(i.get('episodes', []))
             continue
         # Reset_at has a value so check progress rather than just watched count
         for j in i.get('episodes', []):
             if convert_timestamp(j.get('last_watched_at')) >= reset_at:
                 continue
             count += 1
     return count
 def get_upnext_episodes(self, slug, show, get_single_episode=False):
     """
     Get the next episode(s) to watch for a show
     Even though show dict is passed, slug is needed for cache naming purposes
     Set get_single_episode to only retrieve the next_episode value
     Otherwise returns a list of episodes to watch
     """
     # Get show progress
     response = self.get_show_progress(slug)
     if not response:
         return
     # For single episodes just grab next episode and add in show details
     if get_single_episode:
         if not response.get('next_episode'):
             return
         return {'show': show, 'episode': response['next_episode']}
     # For list of episodes we need to build them
     # Get show reset_at value
     reset_at = None
     if response.get('reset_at'):
         reset_at = convert_timestamp(response['reset_at'])
     # Get next episode items
     return [
         {'show': show, 'episode': {'number': episode.get('number'), 'season': season.get('number')}}
         for season in response.get('seasons', []) for episode in season.get('episodes', [])
         if not episode.get('completed')
         or (reset_at and convert_timestamp(episode.get('last_watched_at')) < reset_at)]
 def _get_calendar_episode_item(self, i):
     air_date = convert_timestamp(i.get('first_aired'), utc_convert=True)
     item = get_empty_item()
     item['label'] = i.get('episode', {}).get('title')
     item['infolabels'] = {
         'mediatype': 'episode',
         'premiered': air_date.strftime('%Y-%m-%d'),
         'year': air_date.strftime('%Y'),
         'title': item['label'],
         'episode': i.get('episode', {}).get('number'),
         'season': i.get('episode', {}).get('season'),
         'tvshowtitle': i.get('show', {}).get('title'),
         'duration': try_int(i.get('episode', {}).get('runtime', 0)) * 60,
         'plot': i.get('episode', {}).get('overview'),
         'mpaa': i.get('show', {}).get('certification')}
     item['infoproperties'] = {
         'air_date': get_region_date(air_date, 'datelong'),
         'air_time': get_region_date(air_date, 'time'),
         'air_day': air_date.strftime('%A'),
         'air_day_short': air_date.strftime('%a'),
         'air_date_short': air_date.strftime('%d %b')}
     item['unique_ids'] = {u'tvshow.{}'.format(k): v for k, v in viewitems(i.get('show', {}).get('ids', {}))}
     item['params'] = {
         'info': 'details',
         'tmdb_type': 'tv',
         'tmdb_id': i.get('show', {}).get('ids', {}).get('tmdb'),
         'episode': i.get('episode', {}).get('number'),
         'season': i.get('episode', {}).get('season')}
     return item
Example #4
0
    def run(self):
        self.xbmc_monitor.waitForAbort(
            600)  # Wait 10 minutes before doing updates to give boot time
        if self.xbmc_monitor.abortRequested():
            del self.xbmc_monitor
            return

        self.next_time = datetime.datetime.combine(
            datetime.datetime.today(),
            datetime.time(try_int(self.update_hour)))  # Get today at hour
        self.last_time = xbmc.getInfoLabel(
            'Skin.String(TMDbHelper.AutoUpdate.LastTime)')  # Get last update
        self.last_time = convert_timestamp(
            self.last_time) if self.last_time else None
        if self.last_time and self.last_time > self.next_time:
            self.next_time += datetime.timedelta(
                hours=24)  # Already updated today so set for tomorrow

        while not self.xbmc_monitor.abortRequested(
        ) and not self.exit and self.poll_time:
            if ADDON.getSettingBool('library_autoupdate'):
                if datetime.datetime.now(
                ) > self.next_time:  # Scheduled time has past so lets update
                    xbmc.executebuiltin(
                        'RunScript(plugin.video.themoviedb.helper,library_autoupdate)'
                    )
                    xbmc.executebuiltin(
                        'Skin.SetString(TMDbHelper.AutoUpdate.LastTime,{})'.
                        format(datetime.datetime.now().strftime(
                            "%Y-%m-%dT%H:%M:%S")))
                    self.next_time += datetime.timedelta(
                        hours=24)  # Set next update for tomorrow
            self.xbmc_monitor.waitForAbort(self.poll_time)

        del self.xbmc_monitor
    def _get_calendar_episodes_list(self,
                                    startdate=0,
                                    days=1,
                                    user=True,
                                    kodi_db=None):
        # Get response
        response = self.get_calendar_episodes(startdate=startdate,
                                              days=days,
                                              user=user)
        if not response:
            return

        # Reverse items for date ranges in past
        traktitems = reversed(response) if startdate < -1 else response

        items = []
        for i in traktitems:
            # For library episodes we need to check show is in the library
            if kodi_db and not user and not kodi_db.get_info(
                    info='dbid',
                    tmdb_id=i.get('show', {}).get('ids', {}).get('tmdb'),
                    tvdb_id=i.get('show', {}).get('ids', {}).get('tvdb'),
                    imdb_id=i.get('show', {}).get('ids', {}).get('imdb')):
                continue

            # Do some timezone conversion so we check that we're in the date range for our timezone
            if not date_in_range(i.get('first_aired'),
                                 utc_convert=True,
                                 start_date=startdate,
                                 days=days):
                continue
            air_date = convert_timestamp(i.get('first_aired'),
                                         utc_convert=True)
            item = {}
            item['label'] = i.get('episode', {}).get('title')
            item['infolabels'] = {
                'mediatype': 'episode',
                'premiered': air_date.strftime('%Y-%m-%d'),
                'year': air_date.strftime('%Y'),
                'title': item['label'],
                'episode': i.get('episode', {}).get('number'),
                'season': i.get('episode', {}).get('season'),
                'tvshowtitle': i.get('show', {}).get('title'),
                'duration':
                try_int(i.get('episode', {}).get('runtime', 0)) * 60,
                'plot': i.get('episode', {}).get('overview'),
                'mpaa': i.get('show', {}).get('certification')
            }
            item['infoproperties'] = {
                'air_date': get_region_date(air_date, 'datelong'),
                'air_time': get_region_date(air_date, 'time'),
                'air_day': air_date.strftime('%A'),
                'air_day_short': air_date.strftime('%a'),
                'air_date_short': air_date.strftime('%d %b')
            }
            item['unique_ids'] = {
                'tvshow.{}'.format(k): v
                for k, v in viewitems(i.get('show', {}).get('ids', {}))
            }
            item['params'] = {
                'info': 'details',
                'tmdb_type': 'tv',
                'tmdb_id': i.get('show', {}).get('ids', {}).get('tmdb'),
                'episode': i.get('episode', {}).get('number'),
                'season': i.get('episode', {}).get('season')
            }
            items.append(item)
        return items