fuzeman/trakt.py

New API Methods

Closed this issue · 9 comments

Hey fuzeman, do you mind adding the methods named here to your module? They are needed for the official kodi trakt addon to make manual ratings to work. For more info see my comment in the script.trakt repository: trakt/script.trakt#183 (comment)

The summary methods are available on the develop branch, for example (source):

Trakt['movies'].get('tron-legacy-2010')

Trakt['shows'].get(1390)

Trakt['shows'].seasons('tt0944947')
Trakt['shows'].season('game-of-thrones', 1)

Trakt['shows'].episode('game-of-thrones', 1, 1)

See the trakt.tv API docs for more details on the accepted show/movie identifiers:

Read the notes here #19 (comment) for more details on the develop branch changes.


I can't see a trakt.tv API call for the getShowRatingForUser() method, looking at the implementation of getMovieRatingForUser() (source) it just retrieves the ratings for all movies, then looks for the specific movie.

Concerning the getShowRatingForUser() and getMovieRatingForUser() are implemented by me so nevermind. But I might actually be able to improve this code if I can access the items by their values?

so instead of

def findMovieMatchInList(id, list, idType = 'imdb'):
    return next((item.to_dict() for key, item in list.items() if any(idType in key for key, value in item.keys if str(value) == str(id))), {})

def findShowMatchInList(id, list, idType):
    return next((item for key, item in list.items() if  any(idType in key for key, value in item.keys if str(value) == str(id))), {})

def findEpisodeMatchInList(id, seasonNumber, episodeNumber, list, idType = 'tvdb'):

    show = findShowMatchInList(id,list, idType)

    logger.debug("findEpisodeMatchInList %s" % show)
    if not show:
        return {}
    else:
        if not seasonNumber in show.seasons:
            return {}
        else:   
            season = show.seasons[seasonNumber]
            if not episodeNumber in season.episodes:
                return {}
            else:   
                episode = season.episodes[episodeNumber]
                return episode.to_dict()

could I use something like this? would this work with all id types we have? or is it just working for imdb?

item[('imdb', 'tt1104001')]

Currently the /sync get methods just return a dictionary where objects are mapped by the pk field. This would be imdb for movies and tvdb for shows.

If you want to lookup an object via the alternative keys (tmdb, tvrage, etc..) you can do something like this (source):

for key, item in table.items():
    for alt_key in item.keys[1:]:
        table[alt_key] = item

This would probably be quite costly (on CPU usage) if you are doing this often, caching this dictionary and checking for changes with Trakt['sync'].last_activities() before rebuilding it might be a good idea.

Well seems to me that this is not really different from the code above (just that I use next etc)

Caching is a whole other problem, as I first need to figure out how I want to cache. Speaking of that is there a way to get the URL called from the wrapper? Might be handy for caching keys?

If you are doing a single lookup on a dictionary your current method would probably be faster, multiple lookups on a dictionary would be faster if you were to map the alternative keys and just access the items directly.

Not sure what the best way would be to return the URL called in methods.. I might have some time to start implementing a cache for data from /sync soon (#12), memory usage is always a pain to tackle in these systems though..

Well that would be great of course.
I was thinking about creating text files that hold the cache results as json. Just as extendedInfo for Kodi is working.

We'll also need the lookup endpoint http://docs.trakt.apiary.io/#reference/search/id-lookup/get-id-lookup-results
As I need a tool to get from the tvdb id to the imdb id of a show :/

I've started on the search interface here: https://github.com/fuzeman/trakt.py/tree/feature/search

Still a bit more work to do (some fields are missing, "show" data isn't available on episodes), will merge it into develop when it's finished.

The search interface is now available on the "develop" branch, will have a new "master" version released later today.