This for an XBMC plugin I am building which displays movies you are missing from your movie collection. From what I understand, XBMC stores IMDB ids, while TMDB's collections api only returns TMDB ids for the movies in the collection. This means I have to make a call for every movie in the xbmc database in order to get their TMDB id. Below is my code:
def get_tmdb_configuration():
request = urllib2.Request("http://api.themoviedb.org/3/configuration?api_key=%s" % API_KEY, headers={"Accept" : "application/json"})
response = urllib2.urlopen(request).read()
data = json.loads(response)
return data['images']['base_url']
def get_movie_by_imdb_id(imdb_id):
request = urllib2.Request("http://api.themoviedb.org/3/movie/%s?api_key=%s" % (imdb_id,API_KEY), headers={"Accept" : "application/json"})
response = urllib2.urlopen(request).read()
data = json.loads(response)
collection = str(data["belongs_to_collection"]['id']) if data["belongs_to_collection"] is not None else None
movie = Movie(title=data['title'], id=str(data['id']),collection_id=collection,poster_path=base_image_url + data["poster_path"])
return movie
def get_collection(collection_id):
request = urllib2.Request("http://api.themoviedb.org/3/collection/%s?api_key=%s" % (collection_id,API_KEY), headers={"Accept" : "application/json"})
response = urllib2.urlopen(request).read()
data = json.loads(response)
collection = Collection(name=data["name"],id=str(data["id"]),poster_path=base_image_url + data["poster_path"])
for part in data["parts"]:
movie = Movie(title=part['title'], id=str(part['id']),own=False,collection_id=collection.id,poster_path= base_image_url + part["poster_path"])
collection.movies.append(movie)
return collection
def organize_movies_by_collections(imdb_ids):
collections ={}
for imdb_id in imdb_ids:
current_movie = get_movie_by_imdb_id(imdb_id)
if current_movie.collection_id is not None:
if current_movie.collection_id in collections:
current_collection = collection[current_movie.collection_id]
else:
current_collection = get_collection(current_movie.collection_id)
collections[current_movie.collection_id] = current_collection
movies_in_collection_owned = 0
for movie in current_collection.movies:
if movie.id == current_movie.id:
movie.own = True
movies_in_collection_owned += 1
if len(current_collection.movies) == movies_in_collection_owned:
current_collection.own_all = True
return collections
Thanks in advance for your help.
Can't find a movie or TV show? Login to create it.
Want to rate or add this item to a list?
Not a member?
Reply by Travis Bell
on July 25, 2013 at 10:31 AM
Hi yzupnick,
It doesn't look like anything crazy. Just looping through ids and when you find one that belongs to a collection, go off and retrieve the relevant collection info, right?
Reply by yzupnick
on July 25, 2013 at 9:27 PM
Correct. But if there is a way I can retrieve the imdb id from the collection API, I can significantly reduce the amount of requests I'm making.
For example: The script encounters the imdb id for "Back to the future 1" I receive the Back to the Future collection, which contains the TMDB ids of all the back to the future movies in the collection. When my script comes across the imdb id for "Back to the Future 2" I have to go make a request to get the TMDB id. If there is a way for me to get the IMDB ids with the collection, I wouldn't have to make a second request for the TMDB id.
Reply by Travis Bell
on July 26, 2013 at 10:22 AM
We only provide the IMDB for referencing purposes, it's not designed to be an ID we use for cross-referencing items. This will always be our own, internal ID.
Does XBMC not provide a way of retrieving the TMDb id?
Reply by yzupnick
on July 26, 2013 at 10:46 AM
I don't believe so. I asked over at the XBMC forums though.