jellyfin-kodi/resources/lib/playlist.py

161 lines
4.5 KiB
Python
Raw Normal View History

2016-03-31 17:32:40 +00:00
# -*- coding: utf-8 -*-
#################################################################################################
import logging
2016-03-31 17:32:40 +00:00
import xbmc
import xbmcgui
import playutils
import playbackutils
import embydb_functions as embydb
import read_embyserver as embyserver
2016-10-28 05:02:47 +00:00
from utils import window, kodiSQL, JSONRPC
2016-03-31 17:32:40 +00:00
#################################################################################################
log = logging.getLogger("EMBY."+__name__)
2016-03-31 17:32:40 +00:00
#################################################################################################
2016-03-31 17:32:40 +00:00
2016-10-28 05:02:47 +00:00
class Playlist(object):
2016-03-31 17:32:40 +00:00
2016-06-16 21:24:07 +00:00
def __init__(self):
2016-03-31 17:32:40 +00:00
self.emby = embyserver.Read_EmbyServer()
2016-10-28 05:02:47 +00:00
def play_all(self, item_ids, start_at):
2016-03-31 17:32:40 +00:00
2016-10-28 05:02:47 +00:00
conn = kodiSQL('emby')
cursor = conn.cursor()
emby_db = embydb.Embydb_Functions(cursor)
2016-03-31 17:32:40 +00:00
player = xbmc.Player()
playlist = xbmc.PlayList(xbmc.PLAYLIST_VIDEO)
playlist.clear()
log.info("---*** PLAY ALL ***---")
2016-10-28 05:02:47 +00:00
log.info("Items: %s and start at: %s", item_ids, start_at)
2016-03-31 17:32:40 +00:00
started = False
window('emby_customplaylist', value="true")
2016-10-28 05:02:47 +00:00
if start_at:
2016-03-31 17:32:40 +00:00
# Seek to the starting position
2016-10-28 05:02:47 +00:00
window('emby_customplaylist.seektime', str(start_at))
for item_id in item_ids:
2016-03-31 17:32:40 +00:00
2016-10-28 05:02:47 +00:00
log.info("Adding %s to playlist", item_id)
item = emby_db.getItem_byId(item_id)
2016-03-31 17:32:40 +00:00
try:
2016-10-28 05:02:47 +00:00
db_id = item[0]
media_type = item[4]
2016-03-31 17:32:40 +00:00
except TypeError:
# Item is not found in our database, add item manually
2016-10-28 05:02:47 +00:00
log.info("Item was not found in the database, manually adding item")
item = self.emby.getItem(item_id)
self.add_to_xbmc_playlist(playlist, item)
2016-03-31 17:32:40 +00:00
2016-10-28 05:02:47 +00:00
else: # Add to playlist
self.add_to_playlist(db_id, media_type)
2016-03-31 17:32:40 +00:00
if not started:
started = True
player.play(playlist)
2016-10-28 05:02:47 +00:00
self.verify_playlist()
cursor.close()
2016-03-31 17:32:40 +00:00
2016-10-28 05:02:47 +00:00
def modify_playlist(self, item_ids):
2016-03-31 17:32:40 +00:00
2016-10-28 05:02:47 +00:00
conn = kodiSQL('emby')
cursor = conn.cursor()
emby_db = embydb.Embydb_Functions(cursor)
2016-03-31 17:32:40 +00:00
log.info("---*** ADD TO PLAYLIST ***---")
2016-10-28 05:02:47 +00:00
log.info("Items: %s", item_ids)
2016-03-31 17:32:40 +00:00
playlist = xbmc.PlayList(xbmc.PLAYLIST_VIDEO)
2016-10-28 05:02:47 +00:00
for item_id in item_ids:
log.info("Adding %s to playlist", item_id)
item = emby_db.getItem_byId(item_id)
2016-03-31 17:32:40 +00:00
try:
2016-10-28 05:02:47 +00:00
db_id = item[0]
media_type = item[4]
2016-03-31 17:32:40 +00:00
except TypeError:
# Item is not found in our database, add item manually
2016-10-28 05:02:47 +00:00
item = self.emby.getItem(item_id)
self.add_to_xbmc_playlist(playlist, item)
2016-03-31 17:32:40 +00:00
2016-10-28 05:02:47 +00:00
else: # Add to playlist
self.add_to_playlist(db_id, media_type)
2016-03-31 17:32:40 +00:00
2016-10-28 05:02:47 +00:00
self.verify_playlist()
cursor.close()
2016-03-31 17:32:40 +00:00
return playlist
2016-10-28 05:02:47 +00:00
@classmethod
def add_to_xbmc_playlist(cls, playlist, item):
2016-03-31 17:32:40 +00:00
playurl = playutils.PlayUtils(item).getPlayUrl()
if not playurl:
2016-10-28 05:02:47 +00:00
log.info("Failed to retrieve playurl")
2016-03-31 17:32:40 +00:00
return
2016-10-28 05:02:47 +00:00
log.info("Playurl: %s", playurl)
2016-03-31 17:32:40 +00:00
listitem = xbmcgui.ListItem()
playbackutils.PlaybackUtils(item).setProperties(playurl, listitem)
playlist.add(playurl, listitem)
2016-10-28 05:02:47 +00:00
@classmethod
def add_to_playlist(cls, db_id=None, media_type=None, url=None):
2016-03-31 17:32:40 +00:00
2016-10-28 05:02:47 +00:00
params = {
2016-03-31 17:32:40 +00:00
2016-10-28 05:02:47 +00:00
'playlistid': 1
2016-03-31 17:32:40 +00:00
}
2016-10-28 05:02:47 +00:00
if db_id is not None:
params['item'] = {'%sid' % media_type: int(db_id)}
2016-03-31 17:32:40 +00:00
else:
2016-10-28 05:02:47 +00:00
params['item'] = {'file': url}
2016-03-31 17:32:40 +00:00
2016-10-28 05:02:47 +00:00
log.debug(JSONRPC('Playlist.Add').execute(params))
2016-03-31 17:32:40 +00:00
2016-10-28 05:02:47 +00:00
@classmethod
def insert_to_playlist(cls, position, db_id=None, media_type=None, url=None):
2016-03-31 17:32:40 +00:00
2016-10-28 05:02:47 +00:00
params = {
2016-03-31 17:32:40 +00:00
2016-10-28 05:02:47 +00:00
'playlistid': 1,
'position': position
2016-03-31 17:32:40 +00:00
}
2016-10-28 05:02:47 +00:00
if db_id is not None:
params['item'] = {'%sid' % media_type: int(db_id)}
else:
params['item'] = {'file': url}
log.debug(JSONRPC('Playlist.Insert').execute(params))
2016-03-31 17:32:40 +00:00
2016-10-28 05:02:47 +00:00
@classmethod
def verify_playlist(cls):
log.debug(JSONRPC('Playlist.GetItems').execute({'playlistid': 1}))
2016-03-31 17:32:40 +00:00
2016-10-28 05:02:47 +00:00
@classmethod
def remove_from_playlist(cls, position):
2016-03-31 17:32:40 +00:00
2016-10-28 05:02:47 +00:00
params = {
2016-03-31 17:32:40 +00:00
2016-10-28 05:02:47 +00:00
'playlistid': 1,
'position': position
2016-03-31 17:32:40 +00:00
}
2016-10-28 05:02:47 +00:00
log.debug(JSONRPC('Playlist.Remove').execute(params))