2014-10-12, 12:47
Hi there,
Kept reading the script and TMDB specs - turns out a better implementation would be using the API
I added this file (download.py)
You would call download() to get the object of downloaded files, and use that to update the DB.
Actually, this works already, without downloading the files!... not quite sure how, but looks like once the update is made in the DB, the images are downloaded by XBMC the first time they are requested by the gui (in movie lists).
Thoughts?
Kept reading the script and TMDB specs - turns out a better implementation would be using the API
I added this file (download.py)
PHP Code:
import urllib, urllib2, json
import xbmc, xbmcaddon
from log import log
from database import ARTWORK_TYPE_POSTER, ARTWORK_TYPE_FANART
__addon__ = xbmcaddon.Addon( "script.moviesetart" )
download_path = xbmc.translatePath( __addon__.getSetting( "download_folder_path" ) ).decode('utf-8')
apiurl = 'http://api.themoviedb.org/3/'
apikey = '4be68d7eab1fbd1b6fd8a3b80a65a95e'
base_image_url = None
def _download_getjson( url, params=(), queryparams={} ):
url = apiurl + url % params + '?' + _download_format_query(queryparams)
request = urllib2.Request(url)
request.add_header('Accept', 'application/json')
data = json.loads(urllib2.urlopen(request).read())
return data
def _download_base_url():
global base_image_url
if not base_image_url:
config = _download_getjson( 'configuration' )
base_image_url = config['images']['base_url'] + 'original'
return base_image_url
def _download_format_query( queryparams ):
query = ['api_key=' + apikey]
for key, value in queryparams.items():
query.append( key + '=' + urllib.quote_plus(value) )
return '&'.join([ str(i) for i in query ])
def download( movieset, artwork_type_filenames ):
log( 'downloader: Searching for collection: ' + movieset['label'] )
baseurl = _download_base_url()
artwork = {}
search = _download_getjson( 'search/collection', queryparams={'query': movieset['label']} )
if search['total_results'] == 1:
data = search['results'][0]
'''
Might be a nice implementation later -
present a choice to the user
data = _download_getjson( 'collection/%s/images', data['id'] )
'''
if data['poster_path']:
artwork[ARTWORK_TYPE_POSTER] = baseurl + data['poster_path']
if data['backdrop_path']:
artwork[ARTWORK_TYPE_FANART] = baseurl + data['backdrop_path']
else:
log( 'downloader: Error while matching collection: ' + movieset['label'] )
return 0
if bool(artwork):
for artwork_type, artwork_link in artwork.items():
log( 'downloader: Found type ' + artwork_type + ' link: ' + artwork_link )
return artwork
else:
log( 'downloader: No artwork in collection: ' + movieset['label'] )
return 0
You would call download() to get the object of downloaded files, and use that to update the DB.
Actually, this works already, without downloading the files!... not quite sure how, but looks like once the update is made in the DB, the images are downloaded by XBMC the first time they are requested by the gui (in movie lists).
Thoughts?