Example #1
0
def CollectionSync(itemID, do):
    metadata = PMS.metadata(itemID)
    if not metadata:
        Log.Warn('Unable to fetch metadata for media with id %s' % itemID)
        return

    #cancel, if metadata is not there yet
    if not 'tvdb_id' in metadata and not 'imdb_id' in metadata and not 'tmdb_id' in metadata:
        return

    if do == 'add':
        do_action = 'library'
    elif do == 'delete':
        do_action = 'unlibrary'
    else:
        return

    action = None
    if metadata['type'] == 'episode':
        action = 'show/episode/%s' % do_action
    elif metadata['type'] == 'movie':
        action = 'movie/%s' % do_action

    # Setup Data to send to Trakt
    values = {}

    if metadata['type'] == 'episode':
        if not metadata.get('tvdb_id'):
            Log.Info('Added episode has no tvdb_id')
            return

        values['tvdb_id'] = metadata['tvdb_id']
        values['title'] = metadata['title']

        if 'year' in metadata:
            values['year'] = metadata['year']

        values['episodes'] = [{'season': metadata['season'], 'episode': metadata['episode']}]

    elif metadata['type'] == 'movie':
        if not metadata.get('imdb_id') and not metadata.get('tmdb_id'):
            Log.Info('Added movie has no imdb_id and no tmdb_id')
            return

        movie = {'title': metadata['title'], 'year': metadata['year']}

        if metadata['imdb_id']:
            movie['imdb_id'] = metadata['imdb_id']
        elif metadata['tmdb_id']:
            movie['tmdb_id'] = metadata['tmdb_id']

        values['movies'] = [movie]

    if action:
        Trakt.request(action, values)
Example #2
0
    def send(self, action, data):
        response = Trakt.request(action, data, authenticate=True)

        # Log successful items
        if 'rated' in response:
            rated = response.get('rated')
            unrated = response.get('unrated')

            log.info(
                '(%s) Rated %s item%s and un-rated %s item%s',
                action,
                rated, plural(rated),
                unrated, plural(unrated)
            )
        elif 'message' in response:
            log.info('(%s) %s', action, response['message'])
        else:
            self.log_artifact(action, 'Inserted', response.get('inserted'))

        # Log skipped items, if there were any
        skipped = response.get('skipped', 0)

        if skipped > 0:
            self.log_artifact(action, 'Skipped', skipped, level='warn')
Example #3
0
def SyncSection(key):
    if Prefs['username'] is None:
        Log.Info('You need to enter you login information first.')

        return MessageContainer(
            'Login information missing',
            'You need to enter you login information first.'
        )

    prefs = (Prefs['sync_watched'], Prefs['sync_ratings'], Prefs['sync_collection'])
    if all(x is not True for x in prefs):
        Log.Info('You need to enable at least one type of actions to sync first.')

        return MessageContainer(
            'No type selected',
            'You need to enable at least one type of actions to sync first.'
        )

    # Sync the library with trakt.tv
    all_movies = []
    all_episodes = []
    ratings_movies = []
    ratings_episodes = []
    collection_movies = []
    collection_episodes = []

    for value in key.split(','):
        section = PMS.get_section(value)
        if not section:
            Log.Warn('Unable to get section with key "%s"' % value)
            continue

        item_kind = section.xpath('//MediaContainer')[0].get('viewGroup')

        # Sync movies
        if item_kind == 'movie':
            for video in PMS.get_section_videos(value):
                push_movie(all_movies, collection_movies, ratings_movies, video)

        # Sync TV Shows
        if item_kind == 'show':
            for directory in PMS.get_section_directories(value):
                push_show(all_episodes, collection_episodes, ratings_episodes, directory)

    Log.Info('Found %s movies' % len(all_movies))
    Log.Info('Found %s series' % len(all_episodes))

    if Prefs['sync_ratings'] is True:
        if len(ratings_episodes) > 0:
            Trakt.request('rate/episodes', {
                'episodes': ratings_episodes
            })

        if len(ratings_movies) > 0:
            Trakt.request('rate/movies', {
                'movies': ratings_movies
            })

    if Prefs['sync_watched'] is True:
        if len(all_movies) > 0:
            Trakt.request('movie/seen', {
                'movies': all_movies
            })

        for episode in all_episodes:
            Trakt.request('show/episode/seen', episode)

    if Prefs['sync_collection'] is True:
        if len(collection_movies) > 0:
            Trakt.request('movie/library', {
                'movies': collection_movies
            })

        for episode in collection_episodes:
            Trakt.request('show/episode/library', episode)

    Log.Info('Syncing is done!')
    Dict['Last_sync_up'] = Datetime.Now()
    return MessageContainer('Done', 'Syncing is done!')
Example #4
0
def ManuallyTrakt():
    if Prefs['username'] is None:
        Log.Info('You need to enter you login information first.')
        return MessageContainer('Login information missing', 'You need to enter you login information first.')

    if Prefs['sync_watched'] is not True and Prefs['sync_ratings'] is not True:
        Log.Info('You need to enable at least one type of actions to sync first.')
        return MessageContainer('No type selected', 'You need to enable at least one type of actions to sync first.')

    values = {'extended': 'min'}

    movie_list = None
    show_list = None

    movies_rated_list = None
    episodes_rated_list = None

    # Get watched and rated lists from trakt
    if Prefs['sync_watched'] is True:
        movie_list = Trakt.request(
            'user/library/movies/watched.json',
            values,
            param=Prefs['username']
        ).get('data')

        show_list = Trakt.request(
            'user/library/shows/watched.json',
            values,
            param=Prefs['username']
        ).get('data')

        if not all([x is not None for x in [movie_list, show_list]]):
            return MessageContainer('Network error', 'Network error while requesting watched items from trakt.')

    if Prefs['sync_ratings'] is True:
        movies_rated_list = Trakt.request(
            'user/ratings/movies.json',
            values,
            param=Prefs['username']
        ).get('data')

        episodes_rated_list = Trakt.request(
            'user/ratings/episodes.json',
            values,
            param=Prefs['username']
        ).get('data')

        if not all([x is not None for x in [movies_rated_list, episodes_rated_list]]):
            return MessageContainer('Network error', 'Network error while requesting rated items from trakt.')

    # Go through the Plex library and update flags
    for section_type, key, title in itersections():
        # Sync movies
        if section_type == 'movie':
            for video in PMS.get_section_videos(key):
                pull_movie(movie_list, movies_rated_list, video)

        # Sync TV Shows
        if section_type == 'show':
            for directory in PMS.get_section_directories(key):
                tvdb_id = match_tvdb_id(directory.get('ratingKey'))
                if not tvdb_id:
                    continue

                if tvdb_id is not None:
                    pull_show(show_list, episodes_rated_list, directory, tvdb_id)

    Log.Info('Syncing is done!')
    Dict['Last_sync_down'] = Datetime.Now()

    return MessageContainer('Done', 'Syncing is done!')