jellyfin-kodi/resources/lib/playlist.py

162 lines
4.9 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
from utils import window, JSONRPC
from database import DatabaseConn
from contextlib import closing
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-09-09 03:13:25 +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-09-09 03:13:25 +00:00
def play_all(self, item_ids, start_at):
2016-03-31 17:32:40 +00:00
with DatabaseConn('emby') as conn:
with closing(conn.cursor()) as 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()
2016-03-31 17:32:40 +00:00
log.info("---*** PLAY ALL ***---")
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-03-31 17:32:40 +00:00
if start_at:
# Seek to the starting position
window('emby_customplaylist.seektime', str(start_at))
2016-09-09 03:13:25 +00:00
for item_id in item_ids:
2016-03-31 17:32:40 +00:00
log.info("Adding %s to playlist", item_id)
item = emby_db.getItem_byId(item_id)
try:
db_id = item[0]
media_type = item[4]
2016-09-09 03:13:25 +00:00
except TypeError:
# Item is not found in our database, add item manually
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
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-03-31 17:32:40 +00:00
self.verify_playlist()
2016-03-31 17:32:40 +00:00
2016-09-09 03:13:25 +00:00
def modify_playlist(self, item_ids):
2016-03-31 17:32:40 +00:00
with DatabaseConn('emby') as conn:
with closing(conn.cursor()) as cursor:
emby_db = embydb.Embydb_Functions(cursor)
2016-03-31 17:32:40 +00:00
log.info("---*** ADD TO PLAYLIST ***---")
log.info("Items: %s", item_ids)
2016-03-31 17:32:40 +00:00
playlist = xbmc.PlayList(xbmc.PLAYLIST_VIDEO)
2016-03-31 17:32:40 +00:00
for item_id in item_ids:
2016-09-09 03:13:25 +00:00
log.info("Adding %s to playlist", item_id)
item = emby_db.getItem_byId(item_id)
try:
db_id = item[0]
media_type = item[4]
2016-09-09 03:13:25 +00:00
except TypeError:
# Item is not found in our database, add item manually
item = self.emby.getItem(item_id)
self.add_to_xbmc_playlist(playlist, item)
2016-03-31 17:32:40 +00:00
else: # Add to playlist
self.add_to_playlist(db_id, media_type)
2016-03-31 17:32:40 +00:00
self.verify_playlist()
2016-03-31 17:32:40 +00:00
return playlist
2016-09-09 03:13:25 +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-09-09 03:13:25 +00:00
log.info("Failed to retrieve playurl")
2016-03-31 17:32:40 +00:00
return
2016-09-09 03:13:25 +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-09-09 03:13:25 +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-09-09 03:13:25 +00:00
params = {
2016-03-31 17:32:40 +00:00
2016-09-09 03:13:25 +00:00
'playlistid': 1
2016-03-31 17:32:40 +00:00
}
2016-09-09 03:13:25 +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-09-09 03:13:25 +00:00
params['item'] = {'file': url}
2016-03-31 17:32:40 +00:00
2016-09-09 03:13:25 +00:00
log.debug(JSONRPC('Playlist.Add').execute(params))
2016-03-31 17:32:40 +00:00
2016-09-09 03:13:25 +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-09-09 03:13:25 +00:00
params = {
2016-03-31 17:32:40 +00:00
2016-09-09 03:13:25 +00:00
'playlistid': 1,
'position': position
2016-03-31 17:32:40 +00:00
}
2016-09-09 03:13:25 +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-09-09 03:13:25 +00:00
@classmethod
def verify_playlist(cls):
log.debug(JSONRPC('Playlist.GetItems').execute({'playlistid': 1}))
2016-03-31 17:32:40 +00:00
2016-09-09 03:13:25 +00:00
@classmethod
def remove_from_playlist(cls, position):
2016-03-31 17:32:40 +00:00
2016-09-09 03:13:25 +00:00
params = {
2016-03-31 17:32:40 +00:00
2016-09-09 03:13:25 +00:00
'playlistid': 1,
'position': position
2016-03-31 17:32:40 +00:00
}
2016-09-09 03:13:25 +00:00
log.debug(JSONRPC('Playlist.Remove').execute(params))