Skip to content

Instantly share code, notes, and snippets.

@henryjfry
Last active January 25, 2020 16:58
Show Gist options
  • Save henryjfry/6b8beca3b5872991b31642d063e770b8 to your computer and use it in GitHub Desktop.
Save henryjfry/6b8beca3b5872991b31642d063e770b8 to your computer and use it in GitHub Desktop.
TMDB Helper - Artwork + Sorting: ~/.kodi/addons/plugin.video.themoviedb.helper/resources/lib/container.py
def exp_fanarttv(self):
return True
# if self.params.get('fanarttv', '').capitalize() == 'False':
# return False
# if self.params.get('fanarttv', '').capitalize() == 'True':
# return True
# if self.addon.getSettingBool('widget_fanarttv_lookup') and self.params.get('widget', '').capitalize() == 'True':
# return True
def get_sortedlist(self, items):
# if self.item_tmdbtype == 'episode':
# items = sorted(items, key=lambda x: x['infolabels']['premiered'], reverse=True)
if not items:
return
added, dbiditems, tmdbitems, lastitems, firstitems, nextpage = [], [], [], [], [], []
mixed_movies, mixed_tvshows = 0, 0
if self.item_tmdbtype in ['season', 'episode'] and self.params.get('tmdb_id'):
self.details_tv = self.tmdb.get_detailed_item('tv', self.params.get('tmdb_id'), season=self.params.get('season', None))
self.details_tv['fanart'] = self.details_tv.get('fanart')
self.details_tv['banner'] = self.details_tv.get('banner')
self.details_tv['icon'] = self.details_tv.get('icon')
self.details_tv['poster'] = self.details_tv.get('poster')
self.details_tv['thumb'] = self.details_tv.get('thumb')
if self.item_tmdbtype == 'season' and self.details_tv:
item_upnext = ListItem(library=self.library, **self.details_tv)
item_upnext.infolabels['season'] = self.addon.getLocalizedString(32043)
item_upnext.label = self.addon.getLocalizedString(32043)
item_upnext.url = {'info': 'trakt_upnext', 'type': 'tv'}
items.append(item_upnext)
for i in items:
if i.nextpage:
i.url = self.params.copy()
i.url['page'] = i.nextpage
i.icon = '{0}/resources/icons/tmdb/nextpage.png'.format(self.addonpath)
if self.params.get('nextpage'):
nextpage.append(i)
continue
name = u'{0}{1}'.format(i.label, i.imdb_id or i.tmdb_id or i.poster)
if name in added:
continue
added.append(name)
if i.mixed_type == 'tv':
mixed_tvshows += 1
elif i.mixed_type == 'movie':
mixed_movies += 1
if self.details_tv:
season_num = i.infolabels.get('season')
i.cast = self.details_tv.get('cast', []) + i.cast
i.infolabels = utils.merge_two_dicts(self.details_tv.get('infolabels', {}), i.infolabels)
i.infoproperties = utils.merge_two_dicts(self.details_tv.get('infoproperties', {}), i.infoproperties)
i.poster = i.poster or self.details_tv.get('poster')
i.fanart = i.fanart if i.fanart and i.fanart != '{0}/fanart.jpg'.format(self.addonpath) else self.details_tv.get('fanart')
i.infolabels['season'] = season_num
if self.item_tmdbtype in ['movie', 'movies']:
artwork = self.fanarttv.get_movie_allart_lc(i.tmdb_id)
i.clearart = artwork.get('clearart')
i.clearlogo = artwork.get('clearlogo')
i.banner = artwork.get('banner')
i.poster = artwork.get('poster')
i.landscape = artwork.get('landscape')
if i.landscape == '' or i.landscape is None:
i.landscape = artwork.get('fanart')
i.fanart = artwork.get('fanart')
if i.fanart == '' or i.fanart is None:
i.fanart = i.landscape
if i.thumb == '' or i.thumb is None:
i.thumb = i.landscape
if i.icon == '' or i.icon is None:
i.icon = i.poster
# xbmc.log(str(i.infolabels)+'===>TMDB HELPER_2', level=xbmc.LOGNOTICE)
# xbmc.log(str(self.item_tmdbtype)+'===>TMDB HELPER_2', level=xbmc.LOGNOTICE)
###
if self.item_tmdbtype == 'tv' or self.item_tmdbtype == 'episode' or self.item_tmdbtype == 'tvshow' or self.item_tmdbtype == 'season' or self.item_tmdbtype == 'seasons' or mixed_tvshows:
artwork = None
tvdb_id = self.tmdb.get_item_externalid('tv', i.tmdb_id, 'tvdb_id')
if i.clearart == '' or i.clearart is None:
artwork = self.fanarttv.get_tvshow_allart_lc(tvdb_id)
i.clearart = artwork.get('clearart')
if i.clearlogo == '' or i.clearlogo is None:
if artwork is None:
artwork = self.fanarttv.get_tvshow_allart_lc(tvdb_id)
i.clearlogo = artwork.get('clearlogo')
if i.banner == '' or i.banner is None:
if artwork is None:
artwork = self.fanarttv.get_tvshow_allart_lc(tvdb_id)
i.banner = artwork.get('banner')
if i.poster == '' or i.poster is None:
if artwork is None:
artwork = self.fanarttv.get_tvshow_allart_lc(tvdb_id)
i.poster = artwork.get('poster')
if i.landscape == '' or i.landscape is None:
if artwork is None:
artwork = self.fanarttv.get_tvshow_allart_lc(tvdb_id)
i.landscape = artwork.get('landscape')
if i.landscape == '' or i.landscape is None:
if artwork is None:
artwork = self.fanarttv.get_tvshow_allart_lc(tvdb_id)
i.landscape = artwork.get('fanart')
if i.thumb == '' or i.thumb is None:
i.thumb = i.landscape
if i.fanart == '' or i.fanart is None:
if artwork is None:
artwork = self.fanarttv.get_tvshow_allart_lc(tvdb_id)
i.fanart = artwork.get('fanart')
if i.fanart == '' or i.fanart is None:
if artwork is None:
artwork = self.fanarttv.get_tvshow_allart_lc(tvdb_id)
i.fanart = i.landscape
if i.icon == '' or i.icon is None:
i.icon = i.poster
### xbmc.log(str(self.item_tmdbtype)+'===>TMDB HELPER_2', level=xbmc.LOGNOTICE)
# Format label For Future Eps/Movies
if i.infolabels.get('premiered'):
# Don't format label for plugin methods specifically about the future or details/seasons
if self.params.get('info') not in ['details', 'seasons', 'trakt_calendar', 'trakt_myairing', 'trakt_anticipated']:
try:
if datetime.datetime.strptime(i.infolabels.get('premiered'), '%Y-%m-%d') > datetime.datetime.now():
i.label = '[COLOR=ffcc0000][I]{}[/I][/COLOR]'.format(i.label)
# Don't add if option enabled to hide
if self.addon.getSettingBool('hide_unaired'):
continue
except Exception as exc:
utils.kodi_log('Error: {}'.format(exc), 1)
i.dbid = self.get_db_info(
info='dbid', tmdbtype=self.item_tmdbtype, imdb_id=i.imdb_id,
originaltitle=i.infolabels.get('originaltitle'), title=i.infolabels.get('title'), year=i.infolabels.get('year'),
tvshowtitle=i.infolabels.get('tvshowtitle'), season=i.infolabels.get('season'), episode=i.infolabels.get('episode'))
i.infoproperties['widget'] = self.plugincategory
if self.item_tmdbtype == 'episode':
firstitems.append(i)
else:
if self.item_tmdbtype == 'season' and i.infolabels.get('season') == 0:
lastitems.append(i)
elif self.item_tmdbtype == 'season' and i.infolabels.get('season') == self.addon.getLocalizedString(32043):
firstitems.append(i)
elif i.dbid:
dbiditems.append(i)
else:
tmdbitems.append(i)
if mixed_movies or mixed_tvshows:
self.mixed_containercontent = 'tvshows' if mixed_tvshows > mixed_movies else 'movies'
self.numitems_dbid = len(dbiditems) or 0
self.numitems_tmdb = len(tmdbitems) or 0
xbmcplugin.setProperty(self.handle, 'NumItems.DBID', str(self.numitems_dbid))
xbmcplugin.setProperty(self.handle, 'NumItems.TMDB', str(self.numitems_tmdb))
return firstitems + dbiditems + tmdbitems + lastitems + nextpage
def list_trakthistory(self):
traktapi = TraktAPI(tmdb=self.tmdb)
userslug = traktapi.get_usernameslug(login=True)
self.item_tmdbtype = self.params.get('type')
if self.params.get('info') == 'trakt_nextepisodes':
items = traktapi.get_inprogress(userslug, limit=300, episodes=True)
self.item_tmdbtype = 'episode'
elif self.params.get('info') == 'trakt_inprogress':
items = traktapi.get_inprogress(userslug, limit=300)
elif self.params.get('info') == 'trakt_mostwatched':
items = traktapi.get_mostwatched(userslug, self.params.get('type'), limit=300)
elif self.params.get('info') == 'trakt_history':
items = traktapi.get_recentlywatched(userslug, self.params.get('type'), limit=300)
self.list_items(
items=items, url={
'info': 'trakt_upnext' if self.params.get('info') == 'trakt_inprogress' else 'details',
'type': self.item_tmdbtype})
def list_traktuserlists(self):
cat = constants.TRAKT_LISTS.get(self.params.get('info'), {})
path = cat.get('path', '')
traktapi = TraktAPI(login=cat.get('req_auth', False))
if '{user_slug}' in path:
self.params['user_slug'] = self.params.get('user_slug') or traktapi.get_usernameslug()
path = path.format(**self.params)
icon = '{0}/resources/trakt.png'.format(self.addonpath)
self.start_container()
for i in traktapi.get_response_json(path, limit=250):
if not i:
continue
i = i.get('list') or i
label = i.get('name')
label2 = i.get('user', {}).get('name')
infolabels = {}
infolabels['plot'] = i.get('description')
infolabels['rating'] = i.get('likes')
list_slug = i.get('ids', {}).get('slug')
user_slug = i.get('user', {}).get('ids', {}).get('slug')
# listitem = ListItem(label=label, label2=label2, icon=icon, thumb=icon, poster=icon, infolabels=infolabels)
listitem = ListItem(label=label, label2=label2, icon=icon, thumb=thumb, poster=poster, infolabels=infolabels)
url = {'info': 'trakt_userlist', 'user_slug': user_slug, 'list_slug': list_slug, 'type': self.params.get('type')}
listitem.url = self.set_url_params(url)
listitem.create_listitem(self.handle, **listitem.url) if not self.params.get('random') else self.randomlist.append(listitem)
self.finish_container()
def list_items(self, items=None, url=None, url_tmdb_id=None):
"""
Sort listitems and then display
url= for listitem base folderpath url params
url_tmdb_id= for listitem tmdb_id used in url
"""
items = self.get_sortedlist(items)
if not items:
return
self.item_dbtype = utils.type_convert(self.item_tmdbtype, 'dbtype')
self.containercontent = self.mixed_containercontent or utils.type_convert(self.item_tmdbtype, 'container')
trakt_watched = self.get_trakt_watched()
trakt_unwatched = self.get_trakt_unwatched()
x = 0
self.start_container()
for i in items:
i.infoproperties['numitems.dbid'] = self.numitems_dbid
i.infoproperties['numitems.tmdb'] = self.numitems_tmdb
i.get_details(self.item_dbtype, self.tmdb, self.omdb, self.params.get('localdb'))
i.get_url(url, url_tmdb_id, self.params.get('widget'), self.params.get('fanarttv'), self.params.get('nextpage'), self.params.get('extended'))
# i.get_extra_artwork(self.tmdb, self.fanarttv) if len(items) < 22 and self.exp_fanarttv() else None
i.get_extra_artwork(self.tmdb, self.fanarttv)
i.get_trakt_watched(trakt_watched) if x == 0 or self.params.get('info') != 'details' else None
i.get_trakt_unwatched(trakt=TraktAPI(tmdb=self.tmdb), request=trakt_unwatched, check_sync=self.check_sync) if x == 0 or self.params.get('info') != 'details' else None
i.create_listitem(self.handle, **i.url) if not self.params.get('random') else self.randomlist.append(i)
x += 1
self.finish_container()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment