jellyfin-kodi/resources/lib/read_embyserver.py

603 lines
21 KiB
Python
Raw Normal View History

# -*- coding: utf-8 -*-
#################################################################################################
import logging
import hashlib
2016-12-03 09:11:38 +00:00
import threading
import Queue
import xbmc
import downloadutils
import database
from utils import window, settings
from contextlib import closing
#################################################################################################
log = logging.getLogger("EMBY."+__name__)
#################################################################################################
2016-12-03 09:11:38 +00:00
class DownloadThreader(threading.Thread):
is_finished = False
def __init__(self, queue, output):
self.queue = queue
self.output = output
threading.Thread.__init__(self)
def run(self):
try:
query = self.queue.get()
except Queue.Empty:
self.is_finished = True
return
2016-12-03 09:11:38 +00:00
try:
result = downloadutils.DownloadUtils().downloadUrl(query['url'],
parameters=query.get('params'))
if result:
self.output.extend(result['Items'])
except Exception as error:
log.error(error)
self.queue.task_done()
self.is_finished = True
class Read_EmbyServer():
2016-12-03 09:11:38 +00:00
limitIndex = min(int(settings('limitIndex')), 50)
download_limit = int(settings('downloadThreads'))
download_threads = list()
def __init__(self):
2016-12-03 09:11:38 +00:00
self.doUtils = downloadutils.DownloadUtils()
self.userId = window('emby_currUser')
self.server = window('emby_server%s' % self.userId)
def get_emby_url(self, handler):
return "{server}/emby/%s" % handler
2016-12-03 09:11:38 +00:00
def _add_worker_thread(self, queue, output):
while True:
for thread in self.download_threads:
if thread.is_finished:
self.download_threads.remove(thread)
if window('emby_online') != "true":
# Something happened
log.error("Server is not online, don't start new download thread")
queue.task_done()
return False
if len(self.download_threads) < self.download_limit:
# Start new "daemon thread" - actual daemon thread is not supported in Kodi
new_thread = DownloadThreader(queue, output)
counter = 0
worked = False
while counter < 10:
try:
new_thread.start()
worked = True
break
except:
counter = counter + 1
xbmc.sleep(1000)
if worked:
self.download_threads.append(new_thread)
return True
else:
return False
2016-12-03 09:11:38 +00:00
else:
log.info("Waiting for empty download spot: %s", len(self.download_threads))
xbmc.sleep(100)
def split_list(self, itemlist, size):
# Split up list in pieces of size. Will generate a list of lists
return [itemlist[i:i+size] for i in range(0, len(itemlist), size)]
def getItem(self, itemid):
# This will return the full item
2017-01-24 23:14:48 +00:00
item = self.doUtils.downloadUrl("{server}/emby/Users/{UserId}/Items/%s?format=json" % itemid)
return item
2016-12-03 09:11:38 +00:00
def getItems(self, item_list):
items = []
2016-12-03 09:11:38 +00:00
queue = Queue.Queue()
2016-12-03 09:11:38 +00:00
url = "{server}/emby/Users/{UserId}/Items?&format=json"
for item_ids in self.split_list(item_list, self.limitIndex):
# Will return basic information
params = {
2016-12-03 09:11:38 +00:00
'Ids': ",".join(item_ids),
'Fields': "Etag"
}
2016-12-03 09:11:38 +00:00
queue.put({'url': url, 'params': params})
if not self._add_worker_thread(queue, items):
break
2016-12-03 09:11:38 +00:00
queue.join()
return items
2016-12-03 09:11:38 +00:00
def getFullItems(self, item_list):
items = []
2016-12-03 09:11:38 +00:00
queue = Queue.Queue()
2016-12-03 09:11:38 +00:00
url = "{server}/emby/Users/{UserId}/Items?format=json"
for item_ids in self.split_list(item_list, self.limitIndex):
params = {
2016-12-03 09:11:38 +00:00
"Ids": ",".join(item_ids),
"Fields": (
"Path,Genres,SortName,Studios,Writer,ProductionYear,Taglines,"
"CommunityRating,OfficialRating,CumulativeRunTimeTicks,"
"Metascore,AirTime,DateCreated,MediaStreams,People,Overview,"
"CriticRating,CriticRatingSummary,Etag,ShortOverview,ProductionLocations,"
"Tags,ProviderIds,ParentId,RemoteTrailers,SpecialEpisodeNumbers,"
2016-04-24 00:14:21 +00:00
"MediaSources,VoteCount"
)
}
2016-12-03 09:11:38 +00:00
queue.put({'url': url, 'params': params})
if not self._add_worker_thread(queue, items):
break
2016-12-03 09:11:38 +00:00
queue.join()
return items
2016-06-16 05:43:36 +00:00
def getFilteredSection(self, parentid, itemtype=None, sortby="SortName", recursive=True,
2016-08-18 06:41:18 +00:00
limit=None, sortorder="Ascending", filter_type=""):
params = {
'ParentId': parentid,
'IncludeItemTypes': itemtype,
'CollapseBoxSetItems': False,
'IsVirtualUnaired': False,
'IsMissing': False,
'Recursive': recursive,
'Limit': limit,
'SortBy': sortby,
'SortOrder': sortorder,
2016-09-05 01:19:51 +00:00
'Filters': filter_type,
2016-06-16 05:43:36 +00:00
'Fields': (
"Path,Genres,SortName,Studios,Writer,ProductionYear,Taglines,"
"CommunityRating,OfficialRating,CumulativeRunTimeTicks,"
"Metascore,AirTime,DateCreated,MediaStreams,People,Overview,"
"CriticRating,CriticRatingSummary,Etag,ShortOverview,ProductionLocations,"
"Tags,ProviderIds,ParentId,RemoteTrailers,SpecialEpisodeNumbers"
)
}
2016-12-03 09:11:38 +00:00
return self.doUtils.downloadUrl("{server}/emby/Users/{UserId}/Items?format=json", parameters=params)
def getTvChannels(self):
2016-06-16 05:43:36 +00:00
params = {
'EnableImages': True,
2016-06-16 05:43:36 +00:00
'Fields': (
"Path,Genres,SortName,Studios,Writer,ProductionYear,Taglines,"
"CommunityRating,OfficialRating,CumulativeRunTimeTicks,"
"Metascore,AirTime,DateCreated,MediaStreams,People,Overview,"
"CriticRating,CriticRatingSummary,Etag,ShortOverview,ProductionLocations,"
"Tags,ProviderIds,ParentId,RemoteTrailers,SpecialEpisodeNumbers"
)
}
2016-06-16 05:43:36 +00:00
url = "{server}/emby/LiveTv/Channels/?userid={UserId}&format=json"
2016-12-03 09:11:38 +00:00
return self.doUtils.downloadUrl(url, parameters=params)
def getTvRecordings(self, groupid):
2016-06-16 05:43:36 +00:00
if groupid == "root":
groupid = ""
params = {
'GroupId': groupid,
'EnableImages': True,
2016-06-16 05:43:36 +00:00
'Fields': (
"Path,Genres,SortName,Studios,Writer,ProductionYear,Taglines,"
"CommunityRating,OfficialRating,CumulativeRunTimeTicks,"
"Metascore,AirTime,DateCreated,MediaStreams,People,Overview,"
"CriticRating,CriticRatingSummary,Etag,ShortOverview,ProductionLocations,"
"Tags,ProviderIds,ParentId,RemoteTrailers,SpecialEpisodeNumbers"
)
}
2016-06-16 05:43:36 +00:00
url = "{server}/emby/LiveTv/Recordings/?userid={UserId}&format=json"
2016-12-03 09:11:38 +00:00
return self.doUtils.downloadUrl(url, parameters=params)
2016-10-10 08:19:00 +00:00
def getSection(self, parentid, itemtype=None, sortby="SortName", artist_id=None, basic=False, dialog=None):
items = {
'Items': [],
'TotalRecordCount': 0
}
# Get total number of items
url = "{server}/emby/Users/{UserId}/Items?format=json"
params = {
'ParentId': parentid,
2016-10-10 08:19:00 +00:00
'ArtistIds': artist_id,
'IncludeItemTypes': itemtype,
2016-11-11 02:33:06 +00:00
'LocationTypes': "FileSystem,Remote,Offline",
'CollapseBoxSetItems': False,
'IsVirtualUnaired': False,
'IsMissing': False,
'Recursive': True,
'Limit': 1
}
try:
2016-12-03 09:11:38 +00:00
result = self.doUtils.downloadUrl(url, parameters=params)
total = result['TotalRecordCount']
items['TotalRecordCount'] = total
except Exception as error: # Failed to retrieve
2016-12-03 09:11:38 +00:00
log.debug("%s:%s Failed to retrieve the server response: %s", url, params, error)
else:
index = 0
jump = self.limitIndex
2016-12-03 09:11:38 +00:00
queue = Queue.Queue()
while index < total:
# Get items by chunk to increase retrieval speed at scale
params = {
'ParentId': parentid,
2016-10-10 08:19:00 +00:00
'ArtistIds': artist_id,
'IncludeItemTypes': itemtype,
'CollapseBoxSetItems': False,
'IsVirtualUnaired': False,
2016-12-03 09:11:38 +00:00
'EnableTotalRecordCount': False,
2016-11-11 02:33:06 +00:00
'LocationTypes': "FileSystem,Remote,Offline",
'IsMissing': False,
'Recursive': True,
'StartIndex': index,
'Limit': jump,
'SortBy': sortby,
'SortOrder': "Ascending",
}
if basic:
params['Fields'] = "Etag"
else:
params['Fields'] = (
"Path,Genres,SortName,Studios,Writer,ProductionYear,Taglines,"
"CommunityRating,OfficialRating,CumulativeRunTimeTicks,"
"Metascore,AirTime,DateCreated,MediaStreams,People,Overview,"
"CriticRating,CriticRatingSummary,Etag,ShortOverview,ProductionLocations,"
"Tags,ProviderIds,ParentId,RemoteTrailers,SpecialEpisodeNumbers,"
2016-04-24 00:11:29 +00:00
"MediaSources,VoteCount"
)
2016-12-03 09:11:38 +00:00
queue.put({'url': url, 'params': params})
if not self._add_worker_thread(queue, items['Items']):
break
index += jump
if dialog:
percentage = int((float(index) / float(total))*100)
dialog.update(percentage)
queue.join()
if dialog:
dialog.update(100)
return items
2016-11-05 07:16:59 +00:00
def get_views(self, root=False):
if not root:
url = "{server}/emby/Users/{UserId}/Views?format=json"
else: # Views ungrouped
url = "{server}/emby/Users/{UserId}/Items?Sortby=SortName&format=json"
2016-12-03 09:11:38 +00:00
return self.doUtils.downloadUrl(url)
2016-11-05 07:16:59 +00:00
def getViews(self, mediatype="", root=False, sortedlist=False):
# Build a list of user views
views = []
mediatype = mediatype.lower()
try:
2016-11-05 07:16:59 +00:00
items = self.get_views(root)['Items']
except Exception as error:
log.debug("Error retrieving views for type: %s error:%s" % (mediatype, error))
else:
for item in items:
if item['Type'] in ("Channel", "PlaylistsFolder"):
# Filter view types
continue
# 3/4/2016 OriginalCollectionType is added
itemtype = item.get('OriginalCollectionType', item.get('CollectionType', "mixed"))
if item['Name'] not in ('Collections', 'Trailers', 'Playlists'):
if sortedlist:
views.append({
2016-03-31 18:35:23 +00:00
'name': item['Name'],
'type': itemtype,
2016-03-31 18:35:23 +00:00
'id': item['Id']
})
elif (itemtype == mediatype or
(itemtype == "mixed" and mediatype in ("movies", "tvshows"))):
views.append({
2016-03-31 18:35:23 +00:00
'name': item['Name'],
'type': itemtype,
2016-03-31 18:35:23 +00:00
'id': item['Id']
})
return views
def verifyView(self, parentid, itemid):
params = {
'ParentId': parentid,
'CollapseBoxSetItems': False,
'IsVirtualUnaired': False,
2016-11-11 02:33:06 +00:00
'LocationTypes': "FileSystem,Remote,Offline",
'IsMissing': False,
'Recursive': True,
'Ids': itemid
}
try:
2016-12-03 09:11:38 +00:00
result = self.doUtils.downloadUrl("{server}/emby/Users/{UserId}/Items?format=json", parameters=params)
total = result['TotalRecordCount']
except Exception as error:
# Something happened to the connection
log.info("Error getting item count: " + str(error))
2016-12-03 09:11:38 +00:00
return False
2016-12-03 09:11:38 +00:00
return True if total else False
def getMovies(self, parentId, basic=False, dialog=None):
2016-03-31 18:35:23 +00:00
return self.getSection(parentId, "Movie", basic=basic, dialog=dialog)
def getBoxset(self, dialog=None):
2016-03-31 18:35:23 +00:00
return self.getSection(None, "BoxSet", dialog=dialog)
def getMovies_byBoxset(self, boxsetid):
2016-03-31 18:35:23 +00:00
return self.getSection(boxsetid, "Movie")
def getMusicVideos(self, parentId, basic=False, dialog=None):
2016-03-31 18:35:23 +00:00
return self.getSection(parentId, "MusicVideo", basic=basic, dialog=dialog)
def getHomeVideos(self, parentId):
2016-03-31 18:35:23 +00:00
return self.getSection(parentId, "Video")
def getShows(self, parentId, basic=False, dialog=None):
2016-03-31 18:35:23 +00:00
return self.getSection(parentId, "Series", basic=basic, dialog=dialog)
def getSeasons(self, showId):
items = {
'Items': [],
'TotalRecordCount': 0
}
params = {
'IsVirtualUnaired': False,
'Fields': "Etag"
}
2016-06-16 05:43:36 +00:00
url = "{server}/emby/Shows/%s/Seasons?UserId={UserId}&format=json" % showId
try:
2016-12-03 09:11:38 +00:00
result = self.doUtils.downloadUrl(url, parameters=params)
except Exception as error:
log.info("Error getting Seasons form server: " + str(error))
result = None
if result is not None:
items = result
return items
def getEpisodes(self, parentId, basic=False, dialog=None):
2016-03-31 18:35:23 +00:00
return self.getSection(parentId, "Episode", basic=basic, dialog=dialog)
def getEpisodesbyShow(self, showId):
2016-03-31 18:35:23 +00:00
return self.getSection(showId, "Episode")
def getEpisodesbySeason(self, seasonId):
2016-03-31 18:35:23 +00:00
return self.getSection(seasonId, "Episode")
2016-10-10 08:19:00 +00:00
def getArtists(self, parent_id=None, dialog=None):
items = {
'Items': [],
'TotalRecordCount': 0
}
# Get total number of items
url = "{server}/emby/Artists?UserId={UserId}&format=json"
params = {
2016-10-18 11:43:40 +00:00
'ParentId': parent_id,
'Recursive': True,
'Limit': 1
}
try:
2016-12-03 09:11:38 +00:00
result = self.doUtils.downloadUrl(url, parameters=params)
total = result['TotalRecordCount']
items['TotalRecordCount'] = total
except Exception as error: # Failed to retrieve
2016-12-03 09:11:38 +00:00
log.debug("%s:%s Failed to retrieve the server response: %s", url, params, error)
else:
2016-10-10 08:19:00 +00:00
index = 0
jump = self.limitIndex
2016-12-03 09:11:38 +00:00
queue = Queue.Queue()
while index < total:
# Get items by chunk to increase retrieval speed at scale
params = {
2016-10-10 08:19:00 +00:00
'ParentId': parent_id,
'Recursive': True,
'IsVirtualUnaired': False,
2016-12-03 09:11:38 +00:00
'EnableTotalRecordCount': False,
2016-11-11 02:33:06 +00:00
'LocationTypes': "FileSystem,Remote,Offline",
'IsMissing': False,
'StartIndex': index,
'Limit': jump,
'SortBy': "SortName",
'SortOrder': "Ascending",
'Fields': (
"Etag,Genres,SortName,Studios,Writer,ProductionYear,"
"CommunityRating,OfficialRating,CumulativeRunTimeTicks,Metascore,"
"AirTime,DateCreated,MediaStreams,People,ProviderIds,Overview"
)
}
2016-12-03 09:11:38 +00:00
queue.put({'url': url, 'params': params})
if not self._add_worker_thread(queue, items['Items']):
break
2016-12-03 09:11:38 +00:00
index += jump
if dialog:
percentage = int((float(index) / float(total))*100)
dialog.update(percentage)
2016-12-03 09:11:38 +00:00
queue.join()
if dialog:
dialog.update(100)
return items
def getAlbums(self, basic=False, dialog=None):
2016-03-31 18:35:23 +00:00
return self.getSection(None, "MusicAlbum", sortby="DateCreated", basic=basic, dialog=dialog)
def getAlbumsbyArtist(self, artistId):
2016-10-10 08:19:00 +00:00
return self.getSection(None, "MusicAlbum", sortby="DateCreated", artist_id=artistId)
def getSongs(self, basic=False, dialog=None):
2016-03-31 18:35:23 +00:00
return self.getSection(None, "Audio", basic=basic, dialog=dialog)
def getSongsbyAlbum(self, albumId):
2016-06-16 05:43:36 +00:00
return self.getSection(albumId, "Audio")
def getAdditionalParts(self, itemId):
items = {
'Items': [],
'TotalRecordCount': 0
}
2016-06-16 05:43:36 +00:00
url = "{server}/emby/Videos/%s/AdditionalParts?UserId={UserId}&format=json" % itemId
try:
2016-12-03 09:11:38 +00:00
result = self.doUtils.downloadUrl(url)
except Exception as error:
log.info("Error getting additional parts form server: " + str(error))
result = None
if result is not None:
items = result
return items
def sortby_mediatype(self, itemids):
sorted_items = {}
# Sort items
items = self.getFullItems(itemids)
for item in items:
mediatype = item.get('Type')
if mediatype:
sorted_items.setdefault(mediatype, []).append(item)
return sorted_items
def updateUserRating(self, itemid, favourite=None):
# Updates the user rating to Emby
2016-12-03 09:11:38 +00:00
doUtils = self.doUtils.downloadUrl
2016-06-16 05:43:36 +00:00
if favourite:
2016-06-16 05:43:36 +00:00
url = "{server}/emby/Users/{UserId}/FavoriteItems/%s?format=json" % itemid
doUtils(url, action_type="POST")
elif not favourite:
2016-06-16 05:43:36 +00:00
url = "{server}/emby/Users/{UserId}/FavoriteItems/%s?format=json" % itemid
doUtils(url, action_type="DELETE")
2016-04-24 00:11:29 +00:00
else:
log.info("Error processing user rating.")
log.info("Update user rating to emby for itemid: %s | favourite: %s" % (itemid, favourite))
2016-06-20 00:24:42 +00:00
def refreshItem(self, itemid):
url = "{server}/emby/Items/%s/Refresh?format=json" % itemid
params = {
'Recursive': True,
'ImageRefreshMode': "FullRefresh",
'MetadataRefreshMode': "FullRefresh",
'ReplaceAllImages': False,
'ReplaceAllMetadata': True
}
2016-12-03 09:11:38 +00:00
self.doUtils.downloadUrl(url, postBody=params, action_type="POST")
def deleteItem(self, itemid):
2016-06-21 20:26:42 +00:00
url = "{server}/emby/Items/%s?format=json" % itemid
2016-12-03 09:11:38 +00:00
self.doUtils.downloadUrl(url, action_type="DELETE")
def getUsers(self, server):
url = "%s/emby/Users/Public?format=json" % server
try:
2016-12-03 09:11:38 +00:00
users = self.doUtils.downloadUrl(url, authenticate=False)
except Exception as error:
log.info("Error getting users from server: " + str(error))
users = []
return users
def loginUser(self, server, username, password=None):
password = password or ""
url = "%s/emby/Users/AuthenticateByName?format=json" % server
data = {'username': username, 'password': hashlib.sha1(password).hexdigest()}
2016-12-03 09:11:38 +00:00
user = self.doUtils.downloadUrl(url, postBody=data, action_type="POST", authenticate=False)
return user
def get_single_item(self, media_type, parent_id):
params = {
'ParentId': parent_id,
'Recursive': True,
'Limit': 1,
'IncludeItemTypes': media_type
}
url = self.get_emby_url('Users/{UserId}/Items?format=json')
2016-12-03 09:11:38 +00:00
return self.doUtils.downloadUrl(url, parameters=params)