def get_trakt_movies(self): pchtrakt.logger.info(' [YAMJ] Getting movies from trakt.tv') # Collection url = '/sync/collection/movies' #url = '/users/%s/collection/movies' % (TraktUsername) movies = trakt_apiv2(url) for movie in movies: trakt_movie = { 'title': movie['movie']['title'], 'year': movie['movie']['year'], } if 'imdb' in movie['movie']['ids']: trakt_movie['imdb'] = movie['movie']['ids']['imdb'] if 'tmdb' in movie['movie']['ids']: trakt_movie['tmdb'] = movie['movie']['ids']['tmdb'] #trakt_movie['id'] = "" self.trakt_movies.append(trakt_movie) movies = '' #Clean from collection, keep commented #url = '/movie/unlibrary/' + TraktAPI #params = {'movies': trakt_movies} #response = trakt_api('POST', url, params) #url = '/movie/unseen/' + TraktAPI #response = trakt_api('POST', url, params) # Seen url = '/sync/watched/movies' #url = '/users/%s/watched/movies' % (TraktUsername) seen_movies = trakt_apiv2(url) # Add playcounts to trakt collection for seen in seen_movies: if 'imdb' in seen['movie']['ids']: for movie in self.trakt_movies: if 'imdb' in movie: if seen['movie']['ids']['imdb'] == movie['imdb']: movie['plays'] = seen['plays'] elif 'tmdb' in seen['movie']['ids']: for movie in self.trakt_movies: if 'tmdb' in movie: if seen['movie']['ids']['tmdb'] == movie['tmdb']: movie['plays'] = seen['plays'] elif 'title' in seen: for movie in self.trakt_movies: if 'title' in movie: if seen['title'] == movie['title']: movie['plays'] = seen['plays'] for movie in self.trakt_movies: if not 'plays' in movie: movie['plays'] = 0 seen_movies = ''
def YAMJ_movies_watched_to_trakt(self): pchtrakt.logger.info(' [YAMJ] Comparing YAMJ watched movies against trakt.tv') self.YAMJ_movies_to_trakt = [] if self.trakt_movies and self.YAMJ_movies: for i in range(len(self.trakt_movies)): for movie in self.YAMJ_movies: if movie['playcount'] != 0: if 'imdb' in self.trakt_movies[i]: if movie['imdbnumber'] == self.trakt_movies[i]['imdb']: if self.trakt_movies[i]['plays'] < movie['playcount']: x_loop_must_break = False for x in self.YAMJ_movies_to_trakt: try: if movie['imdbnumber'] == x['movies'][0]['ids']['imdb']: x_loop_must_break = True break except: if movie['imdbnumber'] == x['ids']['imdb']: x_loop_must_break = True break if x_loop_must_break: break self.YAMJ_movies_to_trakt.append(self.convert_YAMJ_movie_to_trakt(movie, watched_at = True)) elif 'tmdb' in self.trakt_movies[i]: if movie['imdbnumber'] == self.trakt_movies[i]['tmdb']: if self.trakt_movies[i]['plays'] < movie['playcount']: self.YAMJ_movies_to_trakt.append(self.convert_YAMJ_movie_to_trakt(movie, watched_at = True)) elif movie['title'] == self.trakt_movies[i]['movies'][0]['title']: if self.trakt_movies[i]['plays'] < movie['playcount']: self.YAMJ_movies_to_trakt.append(self.convert_YAMJ_movie_to_trakt(movie, watched_at = True)) if self.YAMJ_movies_to_trakt: pchtrakt.logger.info(' [YAMJ] %s movies playcount will be updated on trakt.tv' % len(self.YAMJ_movies_to_trakt)) # Send request to update playcounts on trakt.tv url = '/sync/history' params = {'movies': self.YAMJ_movies_to_trakt} try: pchtrakt.logger.info(' [YAMJ] Updating watched status for movies on trakt.tv...') response = trakt_apiv2(url, params, sync=True) pchtrakt.logger.info(' [YAMJ] Marked %s as watched out of %s movies' % (response['added']['movies'], len(self.YAMJ_movies_to_trakt))) if len(response['not_found']['movies']) != 0: for skip in response['not_found']['movies']: pchtrakt.logger.info(' [YAMJ] could not add -->%s' % skip['not_found']['movies'][0]['title'].encode('utf-8')) except Exception, e: pchtrakt.logger.info(' [YAMJ] Failed to update playcount for movies on trakt.tv') pchtrakt.logger.info(e)
def YAMJ_shows_watched_to_trakt(self): pchtrakt.logger.info(' [YAMJ] Comparing YAMJ watched TV shows against trakt.tv') self.YAMJ_shows_to_trakt = [] if self.YAMJ_shows and self.trakt_shows: tvdb_ids = {} imdb_ids = {} for i in range(len(self.trakt_shows)): if 'tvdb' in self.trakt_shows[i]: tvdb_ids[self.trakt_shows[i]['tvdb']] = i if 'imdb' in self.trakt_shows[i]: imdb_ids[self.trakt_shows[i]['imdb']] = i for show in self.YAMJ_shows.values(): if 'imdbnumber' in show: if show['imdbnumber'].startswith('tt'): if self.search(imdb_ids, show['imdbnumber']):#if show['imdbnumber'] in imdb_ids.keys(): trakt_show = self.trakt_shows[imdb_ids.get(int(show['imdbnumber']))] trakt_show_watched = { 'title': show['title'], 'imdb': show['imdbnumber'], 'episodes': [] } for YAMJ_ep in show['episodes']: for trakt_ep in trakt_show['episodes']: if trakt_ep['season'] == YAMJ_ep['season']: if trakt_ep['episode'] == YAMJ_ep['episode']: if trakt_ep['plays'] == 0 and YAMJ_ep['playcount'] >= 1: trakt_show_watched['episodes'].append( { 'season': YAMJ_ep['season'], 'episode': YAMJ_ep['episode'], 'date': YAMJ_ep['date'] } ) if trakt_show_watched['episodes']: self.YAMJ_shows_to_trakt.append(trakt_show_watched) else: if self.searchtv(tvdb_ids, show['imdbnumber']):#if show['imdbnumber'] in tvdb_ids.keys(): trakt_show = self.trakt_shows[tvdb_ids.get(int(show['imdbnumber']))] trakt_show_watched = { 'title': show['title'], 'tvdb': show['imdbnumber'], 'episodes': [] } for YAMJ_ep in show['episodes']: for trakt_ep in trakt_show['episodes']: if trakt_ep['season'] == YAMJ_ep['season']: if trakt_ep['episode'] == YAMJ_ep['episode']: if trakt_ep['plays'] == 0 and YAMJ_ep['playcount'] >= 1: trakt_show_watched['episodes'].append( { 'season': YAMJ_ep['season'], 'episode': YAMJ_ep['episode'], 'watched_at': YAMJ_ep['date'] } ) if trakt_show_watched['episodes']: self.YAMJ_shows_to_trakt.append(trakt_show_watched) if self.YAMJ_shows_to_trakt: pchtrakt.logger.info(' [YAMJ] %s TV shows have episodes that will be marked as watched in trakt.tv collection' % len(self.YAMJ_shows_to_trakt)) for i in range(len(self.YAMJ_shows_to_trakt)): #convert YAMJ show into something trakt will understand self.YAMJ_shows_to_trakt[i] = self.convert_YAMJ_show_to_trakt(self.YAMJ_shows_to_trakt[i]) # Send request to add TV shows to trakt.tv url = '/sync/history' for show in self.YAMJ_shows_to_trakt: try: pchtrakt.logger.info(' [YAMJ] -->%s' % show['shows'][0]['title']) trakt = trakt_apiv2(url, show, sync=True) if trakt['added']['episodes'] != 0 and len(trakt['not_found']['episodes']) != 0: pchtrakt.logger.info(' [YAMJ] Successfully marked %s episodes watched out of %s' % (trakt['added']['episodes'], trakt['added']['episodes'] + trakt['not_found']['episodes'])) else: pchtrakt.logger.info(' [YAMJ] Successfully marked %s episodes watched out of %s' % (trakt['added']['episodes'], trakt['added']['episodes'])) except Exception, e: pchtrakt.logger.info(' [YAMJ] Failed to mark %s\'s episodes as watched in trakt.tv collection' % show['shows'][0]['title']) pchtrakt.logger.info(e) else: pchtrakt.logger.info(' [YAMJ] trakt.tv TV show watched status is up to date')
def YAMJ_shows_to_trakt(self): pchtrakt.logger.info(' [YAMJ] Checking for YAMJ episodes that are not in trakt.tv collection') self.YAMJ_shows_to_trakt = [] def clean_episodes(shows): if shows: for show in shows: episodes = [] for episode in show['episodes']: episodes.append({'season': episode['season'], 'episode': episode['episode']}) show['episodes'] = episodes return shows if self.YAMJ_shows and self.trakt_shows: t_shows = copy.deepcopy(self.trakt_shows) t_shows = clean_episodes(t_shows) x_shows = copy.deepcopy(self.YAMJ_shows.values()) x_shows = clean_episodes(x_shows) tvdb_ids = {} imdb_ids = {} for i in range(len(t_shows)): if 'tvdb' in t_shows[i]: tvdb_ids[t_shows[i]['tvdb']] = i if 'imdb' in t_shows[i]: imdb_ids[t_shows[i]['imdb']] = i for show in x_shows: if 'imdbnumber' in show: if show['imdbnumber'].startswith('tt'): if self.search(imdb_ids, show['imdbnumber']) == False:#if not show['imdbnumber'] in imdb_ids: self.YAMJ_shows_to_trakt.append(show) #trakt_show = convert_YAMJ_show_to_trakt(show) for episode in trakt_show['episodes']: episode['plays'] = 0 #trakt_shows.append(trakt_show) else: t_index = imdb_ids.get(int(show['imdbnumber'])) YAMJ_show = { 'title': show['title'], 'imdbnumber': show['imdbnumber'], 'episodes': [] } for episode in show['episodes']: if episode['episode'] == 0: continue if episode not in t_shows[t_index]['episodes']: YAMJ_show['episodes'].append(episode) self.trakt_shows[t_index]['episodes'].append(episode) self.trakt_shows[t_index]['episodes'][-1]['plays'] = 0 if YAMJ_show['episodes']: self.YAMJ_shows_to_trakt.append(YAMJ_show) else: if self.searchtv(tvdb_ids, show['imdbnumber']) == False:# if not show['imdbnumber'] in tvdb_ids: self.YAMJ_shows_to_trakt.append(show) #trakt_show = convert_YAMJ_show_to_trakt(show) #for season in trakt_show['shows'][0]['seasons']: # for episode in season['episodes']: # episode['plays'] = 0 #trakt_shows.append(trakt_show) else: t_index = tvdb_ids.get(int(show['imdbnumber'])) #tvdb_ids.get(int(show['imdbnumber'])) YAMJ_show = { 'title': show['title'], 'imdbnumber': show['imdbnumber'], 'episodes': [] } for episode in show['episodes']: if episode['episode'] == 0: continue if episode not in t_shows[t_index]['episodes']: YAMJ_show['episodes'].append(episode) self.trakt_shows[t_index]['episodes'].append(episode) self.trakt_shows[t_index]['episodes'][-1]['plays'] = 0 if YAMJ_show['episodes']: self.YAMJ_shows_to_trakt.append(YAMJ_show) if self.YAMJ_shows_to_trakt: pchtrakt.logger.info(' [YAMJ] %s TV shows have episodes missing from trakt.tv collection' % len(self.YAMJ_shows_to_trakt)) for i in range(len(self.YAMJ_shows_to_trakt)): #convert YAMJ show into something trakt will understand self.YAMJ_shows_to_trakt[i] = self.convert_YAMJ_show_to_trakt(self.YAMJ_shows_to_trakt[i]) # Send request to add TV shows to trakt.tv url = '/sync/collection' #data = {'shows': [{'title': 'Mad Men', 'year': 2007, 'ids': {'trakt': 4, 'slug': 'mad-men', 'tvdb': 80337, 'imdb': 'tt0804503', 'tmdb': 1104, 'tvrage': 16356}, 'seasons': [{'number': 1, 'episodes': [{'number': 1},{'number': 2}]}]}]} for show in self.YAMJ_shows_to_trakt: try: #params = {'shows': [show]} pchtrakt.logger.info(' [YAMJ] -->%s' % show['shows'][0]['title']) trakt = trakt_apiv2(url, show, sync=True) if trakt['added']['episodes'] > 0: pchtrakt.logger.info(' [YAMJ] Added %s' % trakt['added']['episodes']) if trakt['updated']['episodes'] > 0: pchtrakt.logger.info(' [YAMJ] Updated %s' % trakt['updated']['episodes']) if trakt['existing']['episodes'] > 0: pchtrakt.logger.info(' [YAMJ] Modified %s' % trakt['existing']['episodes']) #if trakt['not_found']['episodes']: except Exception, e: pchtrakt.logger.info(' [YAMJ] Failed to add %s\'s new episodes to trakt.tv collection' % show['title']) pchtrakt.logger.info(e) else: pchtrakt.logger.info(' [YAMJ] trakt.tv TV show collection is up to date')
def get_trakt_shows(self): pchtrakt.logger.info(' [YAMJ] Getting TV shows from trakt') # Collection #url = '/users/%s/collection/shows' % (TraktUsername) url = '/sync/collection/shows' collection_shows = trakt_apiv2(url) for show in collection_shows: trakt_show = { 'title': show['show']['title'], 'episodes': [] } if 'imdb' in show['show']['ids']: trakt_show['imdb'] = show['show']['ids']['imdb'] if 'tvdb' in show['show']['ids']: trakt_show['tvdb'] = show['show']['ids']['tvdb'] for season in show['seasons']: for episode in season['episodes']: ep = {'season': season['number'], 'episode': episode['number'], 'plays': 0} trakt_show['episodes'].append(ep) #Clean from collection, keep commented #url = '/show/episode/unseen/' + TraktAPI #response = trakt_api('post', url, trakt_show) #url = '/show/episode/unlibrary/' + TraktAPI #response = trakt_api('post', url, trakt_show) self.trakt_shows.append(trakt_show) collection_shows = '' # Seen url = '/sync/watched/shows' #url = '/users/%s/watched/shows' % (TraktUsername) seen_shows = trakt_apiv2(url) show = '' for show in seen_shows: for season in show['seasons']: for episode in season['episodes']: for trakt_show in self.trakt_shows: if ('imdb' in show['show']['ids'] and 'imdb' in trakt_show) and show['show']['ids']['imdb'] != None and show['show']['ids']['imdb'] == trakt_show['imdb']: try: #if len(show['show']['ids']['imdb']) > 0: for trakt_episode in trakt_show['episodes']: if trakt_episode['season'] == season['number'] and trakt_episode['episode'] == episode['number']: trakt_episode['plays'] = 1 except Exception as e: pass elif ('tvdb' in show['show']['ids'] and 'tvdb' in trakt_show) and show['show']['ids']['tvdb'] != None and show['show']['ids']['tvdb'] == trakt_show['tvdb']: try: #if len(show['show']['ids']['tvdb']) > 0: for trakt_episode in trakt_show['episodes']: if trakt_episode['season'] == season['number'] and trakt_episode['episode'] == episode['number']: trakt_episode['plays'] = 1 except Exception as e: pass else: try: if show['show']['title'] == trakt_show['title']: for trakt_episode in trakt_show['episodes']: if trakt_episode['season'] == season['number'] and trakt_episode['episode'] == episode['number']: trakt_episode['plays'] = 1 except: if show['show']['title'] == trakt_show['shows'][0]['title']: for trakt_episode in trakt_show['episodes']: if trakt_episode['season'] == season['number'] and trakt_episode['episode'] == episode['number']: trakt_episode['plays'] = 1 seen_shows = '' show = ''
def YAMJ_movies_to_trakt(self): pchtrakt.logger.info(' [YAMJ] Checking for YAMJ movies that are not in trakt.tv collection') self.YAMJ_movies_to_trakt = [] if self.trakt_movies and self.YAMJ_movies: imdb_ids = [x['imdb'] for x in self.trakt_movies if 'imdb' in x] tmdb_ids = [x['tmdb'] for x in self.trakt_movies if 'tmdb' in x] titles = [x['title'] for x in self.trakt_movies if 'title' in x] if self.YAMJ_movies: for movie in self.YAMJ_movies: if 'imdbnumber' in movie: if movie['imdbnumber'].startswith('tt'): if self.trakt_movies: if self.search(imdb_ids, movie['imdbnumber']) == False: self.YAMJ_movies_to_trakt.append(movie) #trakt_movie = convert_YAMJ_movie_to_trakt(movie)# do we need these below? #if not 'plays' in trakt_movie[0]:trakt_movie['movies'] # trakt_movie[0]['plays'] = 0 #trakt_movies.append(trakt_movie) else: self.YAMJ_movies_to_trakt.append(movie) #trakt_movie = convert_YAMJ_movie_to_trakt(movie) #if not 'plays' in trakt_movie[0]: # trakt_movie[0]['plays'] = 0 ##trakt_movies.append(trakt_movie) else: if self.trakt_movies: if self.searchtv(tmdb_ids, movie['imdbnumber']) == False:#if not movie['imdbnumber'] in tmdb_ids: self.YAMJ_movies_to_trakt.append(movie) #trakt_movie = convert_YAMJ_movie_to_trakt(movie) #if not 'plays' in trakt_movie[0]: # trakt_movie[0]['plays'] = 0 #trakt_movies.append(trakt_movie) else: self.YAMJ_movies_to_trakt.append(movie) #trakt_movie = convert_YAMJ_movie_to_trakt(movie) #if not 'plays' in trakt_movie[0]: # trakt_movie[0]['plays'] = 0 ##trakt_movies.append(trakt_movie) elif not movie['title'] in titles and not movie in self.YAMJ_movies_to_trakt: self.YAMJ_movies_to_trakt.append(movie) #trakt_movie = convert_YAMJ_movie_to_trakt(movie) #if not 'plays' in trakt_movie[0]: # trakt_movie[0]['plays'] = 0 #trakt_movies.append(trakt_movie) if self.YAMJ_movies_to_trakt: pchtrakt.logger.info(' [YAMJ] Checking for %s movies will be added to trakt.tv collection' % len(self.YAMJ_movies_to_trakt)) for i in range(len(self.YAMJ_movies_to_trakt)): #convert YAMJ movie into something trakt will understand self.YAMJ_movies_to_trakt[i] = self.convert_YAMJ_movie_to_trakt(self.YAMJ_movies_to_trakt[i]) # Send request to add movies to trakt.tv url = '/sync/collection' params = {'movies': self.YAMJ_movies_to_trakt} try: pchtrakt.logger.info(' [YAMJ] Adding movies to trakt.tv collection...') response = trakt_apiv2(url, params, sync=True) if response['added']['movies'] != 0: if len(response['not_found']['movies']) !=0: pchtrakt.logger.info(' [YAMJ] Successfully added %s out of %s to your collection' % (response['added']['movies'], response['added']['movies'] + response['existing']['movies'] + len(response['not_found']['movies']))) pchtrakt.logger.info(' [YAMJ] Failed to add the following %s titles to your collection' % len(response['not_found']['movies'])) for failed in response['not_found']['movies']: pchtrakt.logger.info(' [YAMJ] Failed to add %s' % failed['movies'][0]['title'].encode('utf-8', 'replace')) else: pchtrakt.logger.info(' [YAMJ] Successfully added %s out of %s to your collection' % (response['added']['movies'], response['added']['movies'] + response['existing']['movies'])) if response['existing']['movies'] != 0: pchtrakt.logger.info(' [YAMJ] %s titles were found in your collection already' % response['existing']['movies']) except Exception, e: pchtrakt.logger.info(' [YAMJ] Failed to add movies to trakt.tv collection') pchtrakt.logger.info(e)