jellyfin-kodi/resources/lib/objects/musicvideos.py

341 lines
11 KiB
Python
Raw Normal View History

2016-10-10 08:19:00 +00:00
# -*- coding: utf-8 -*-
##################################################################################################
import logging
import urllib
import api
import embydb_functions as embydb
import _kodi_musicvideos
from _common import Items
2016-10-10 11:14:10 +00:00
from utils import window, language as lang, catch_except
2016-10-10 08:19:00 +00:00
##################################################################################################
log = logging.getLogger("EMBY."+__name__)
##################################################################################################
class MusicVideos(Items):
2016-10-10 08:19:00 +00:00
2016-10-10 11:14:10 +00:00
2016-10-10 08:19:00 +00:00
def __init__(self, embycursor, kodicursor, pdialog=None):
2016-10-10 11:14:10 +00:00
2016-10-10 08:19:00 +00:00
self.embycursor = embycursor
self.emby_db = embydb.Embydb_Functions(self.embycursor)
self.kodicursor = kodicursor
self.kodi_db = _kodi_musicvideos.KodiMusicVideos(self.kodicursor)
2016-10-10 08:19:00 +00:00
self.pdialog = pdialog
2016-10-10 11:14:10 +00:00
Items.__init__(self)
2016-10-10 08:19:00 +00:00
def _get_func(self, item_type, action):
if item_type == "MusicVideo":
actions = {
'added': self.added,
'update': self.add_update,
'userdata': self.updateUserdata,
'remove': self.remove
}
else:
log.info("Unsupported item_type: %s", item_type)
actions = {}
return actions.get(action)
def compare_all(self):
pdialog = self.pdialog
# Pull the list of musicvideos in Kodi
views = self.emby_db.getView_byType('musicvideos')
2016-10-10 11:14:10 +00:00
log.info("Media folders: %s", views)
2016-10-10 08:19:00 +00:00
try:
all_kodimvideos = dict(self.emby_db.get_checksum('MusicVideo'))
except ValueError:
all_kodimvideos = {}
all_embymvideosIds = set()
updatelist = []
for view in views:
if self.should_stop():
return False
# Get items per view
viewId = view['id']
viewName = view['name']
if pdialog:
pdialog.update(
heading=lang(29999),
message="%s %s..." % (lang(33028), viewName))
all_embymvideos = self.emby.getMusicVideos(viewId, basic=True, dialog=pdialog)
for embymvideo in all_embymvideos['Items']:
if self.should_stop():
return False
API = api.API(embymvideo)
itemid = embymvideo['Id']
all_embymvideosIds.add(itemid)
if all_kodimvideos.get(itemid) != API.get_checksum():
# Only update if musicvideo is not in Kodi or checksum is different
updatelist.append(itemid)
2016-10-10 11:14:10 +00:00
log.info("MusicVideos to update for %s: %s", viewName, updatelist)
2016-10-10 08:19:00 +00:00
embymvideos = self.emby.getFullItems(updatelist)
self.total = len(updatelist)
del updatelist[:]
if pdialog:
pdialog.update(heading="Processing %s / %s items" % (viewName, self.total))
self.count = 0
for embymvideo in embymvideos:
# Process individual musicvideo
if self.should_stop():
return False
self.title = embymvideo['Name']
self.update_pdialog()
self.add_update(embymvideo, view)
self.count += 1
##### PROCESS DELETES #####
for kodimvideo in all_kodimvideos:
if kodimvideo not in all_embymvideosIds:
self.remove(kodimvideo)
2016-10-10 11:14:10 +00:00
log.info("MusicVideos compare finished.")
2016-10-10 08:19:00 +00:00
return True
def added(self, items, total=None, view=None):
for item in super(MusicVideos, self).added(items, total):
2016-10-10 08:19:00 +00:00
if self.add_update(item, view):
2016-10-10 09:01:13 +00:00
self.content_pop(item.get('Name', "unknown"))
2016-10-10 08:19:00 +00:00
2016-10-11 23:00:54 +00:00
@catch_except()
2016-10-10 08:19:00 +00:00
def add_update(self, item, view=None):
# Process single music video
kodicursor = self.kodicursor
emby_db = self.emby_db
artwork = self.artwork
API = api.API(item)
# If the item already exist in the local Kodi DB we'll perform a full item update
# If the item doesn't exist, we'll add it to the database
update_item = True
itemid = item['Id']
emby_dbitem = emby_db.getItem_byId(itemid)
try:
mvideoid = emby_dbitem[0]
fileid = emby_dbitem[1]
pathid = emby_dbitem[2]
2016-10-10 11:14:10 +00:00
log.info("mvideoid: %s fileid: %s pathid: %s", mvideoid, fileid, pathid)
2016-10-10 08:19:00 +00:00
except TypeError:
update_item = False
2016-10-10 11:14:10 +00:00
log.debug("mvideoid: %s not found", itemid)
2016-10-10 08:19:00 +00:00
# mvideoid
mvideoid = self.kodi_db.create_entry()
2016-10-10 08:19:00 +00:00
else:
if self.kodi_db.get_musicvideo(mvideoid) is None:
2016-10-10 08:19:00 +00:00
# item is not found, let's recreate it.
update_item = False
2016-10-10 11:14:10 +00:00
log.info("mvideoid: %s missing from Kodi, repairing the entry.", mvideoid)
2016-10-10 08:19:00 +00:00
if not view:
# Get view tag from emby
viewtag, viewid, mediatype = self.emby.getView_embyId(itemid)
2016-10-10 11:14:10 +00:00
log.debug("View tag found: %s", viewtag)
2016-10-10 08:19:00 +00:00
else:
viewtag = view['name']
viewid = view['id']
# fileId information
checksum = API.get_checksum()
dateadded = API.get_date_created()
userdata = API.get_userdata()
playcount = userdata['PlayCount']
dateplayed = userdata['LastPlayedDate']
# item details
runtime = API.get_runtime()
plot = API.get_overview()
title = item['Name']
year = item.get('ProductionYear')
genres = item['Genres']
genre = " / ".join(genres)
studios = API.get_studios()
studio = " / ".join(studios)
artist = " / ".join(item.get('Artists'))
album = item.get('Album')
track = item.get('Track')
people = API.get_people()
director = " / ".join(people['Director'])
2016-10-10 11:14:10 +00:00
2016-10-10 08:19:00 +00:00
##### GET THE FILE AND PATH #####
playurl = API.get_file_path()
if "\\" in playurl:
# Local path
filename = playurl.rsplit("\\", 1)[1]
else: # Network share
filename = playurl.rsplit("/", 1)[1]
if self.direct_path:
# Direct paths is set the Kodi way
if not self.path_validation(playurl):
return False
2016-10-10 11:14:10 +00:00
2016-10-10 08:19:00 +00:00
path = playurl.replace(filename, "")
window('emby_pathverified', value="true")
else:
# Set plugin path and media flags using real filename
path = "plugin://plugin.video.emby.musicvideos/"
params = {
'filename': filename.encode('utf-8'),
'id': itemid,
'dbid': mvideoid,
'mode': "play"
}
filename = "%s?%s" % (path, urllib.urlencode(params))
##### UPDATE THE MUSIC VIDEO #####
if update_item:
2016-10-10 11:14:10 +00:00
log.info("UPDATE mvideo itemid: %s - Title: %s", itemid, title)
2016-10-10 08:19:00 +00:00
# Update the music video entry
self.kodi_db.update_musicvideo(title, runtime, director, studio, year, plot, album,
artist, genre, track, mvideoid)
2016-10-10 08:19:00 +00:00
# Update the checksum in emby table
emby_db.updateReference(itemid, checksum)
2016-10-10 11:14:10 +00:00
2016-10-10 08:19:00 +00:00
##### OR ADD THE MUSIC VIDEO #####
else:
2016-10-10 11:14:10 +00:00
log.info("ADD mvideo itemid: %s - Title: %s", itemid, title)
2016-10-10 08:19:00 +00:00
# Add path
pathid = self.kodi_db.add_path(path)
2016-10-10 08:19:00 +00:00
# Add the file
fileid = self.kodi_db.add_file(filename, pathid)
2016-10-10 11:14:10 +00:00
2016-10-10 08:19:00 +00:00
# Create the musicvideo entry
self.kodi_db.add_musicvideo(mvideoid, fileid, title, runtime, director, studio,
year, plot, album, artist, genre, track)
2016-10-10 08:19:00 +00:00
# Create the reference in emby table
emby_db.addReference(itemid, mvideoid, "MusicVideo", "musicvideo", fileid, pathid,
2016-10-10 11:14:10 +00:00
checksum=checksum, mediafolderid=viewid)
2016-10-10 08:19:00 +00:00
# Update the path
self.kodi_db.update_path(pathid, path, "musicvideos", "metadata.local")
# Update the file
self.kodi_db.update_file(fileid, filename, pathid, dateadded)
2016-10-10 08:19:00 +00:00
# Process cast
people = item['People']
artists = item['ArtistItems']
for artist in artists:
artist['Type'] = "Artist"
people.extend(artists)
people = artwork.get_people_artwork(people)
self.kodi_db.add_people(mvideoid, people, "musicvideo")
2016-10-10 08:19:00 +00:00
# Process genres
self.kodi_db.add_genres(mvideoid, genres, "musicvideo")
2016-10-10 08:19:00 +00:00
# Process artwork
artwork.add_artwork(artwork.get_all_artwork(item), mvideoid, "musicvideo", kodicursor)
# Process stream details
streams = API.get_media_streams()
self.kodi_db.add_streams(fileid, streams, runtime)
2016-10-10 08:19:00 +00:00
# Process studios
self.kodi_db.add_studios(mvideoid, studios, "musicvideo")
2016-10-10 08:19:00 +00:00
# Process tags: view, emby tags
tags = [viewtag]
tags.extend(item['Tags'])
if userdata['Favorite']:
tags.append("Favorite musicvideos")
self.kodi_db.add_tags(mvideoid, tags, "musicvideo")
2016-10-10 08:19:00 +00:00
# Process playstates
resume = API.adjust_resume(userdata['Resume'])
total = round(float(runtime), 6)
self.kodi_db.add_playstate(fileid, resume, total, playcount, dateplayed)
2016-10-10 08:19:00 +00:00
return True
def updateUserdata(self, item):
# This updates: Favorite, LastPlayedDate, Playcount, PlaybackPositionTicks
# Poster with progress bar
emby_db = self.emby_db
API = api.API(item)
2016-10-10 11:14:10 +00:00
2016-10-10 08:19:00 +00:00
# Get emby information
itemid = item['Id']
checksum = API.get_checksum()
userdata = API.get_userdata()
runtime = API.get_runtime()
# Get Kodi information
emby_dbitem = emby_db.getItem_byId(itemid)
try:
mvideoid = emby_dbitem[0]
fileid = emby_dbitem[1]
2016-10-10 11:14:10 +00:00
log.info("Update playstate for musicvideo: %s fileid: %s", item['Name'], fileid)
2016-10-10 08:19:00 +00:00
except TypeError:
return
# Process favorite tags
if userdata['Favorite']:
self.kodi_db.get_tag(mvideoid, "Favorite musicvideos", "musicvideo")
2016-10-10 08:19:00 +00:00
else:
self.kodi_db.remove_tag(mvideoid, "Favorite musicvideos", "musicvideo")
2016-10-10 08:19:00 +00:00
# Process playstates
playcount = userdata['PlayCount']
dateplayed = userdata['LastPlayedDate']
resume = API.adjust_resume(userdata['Resume'])
total = round(float(runtime), 6)
self.kodi_db.add_playstate(fileid, resume, total, playcount, dateplayed)
2016-10-10 08:19:00 +00:00
emby_db.updateReference(itemid, checksum)
def remove(self, itemid):
# Remove mvideoid, fileid, pathid, emby reference
emby_db = self.emby_db
kodicursor = self.kodicursor
artwork = self.artwork
emby_dbitem = emby_db.getItem_byId(itemid)
try:
mvideoid = emby_dbitem[0]
fileid = emby_dbitem[1]
pathid = emby_dbitem[2]
2016-10-10 11:14:10 +00:00
log.info("Removing mvideoid: %s fileid: %s pathid: %s", mvideoid, fileid, pathid)
2016-10-10 08:19:00 +00:00
except TypeError:
return
# Remove the emby reference
emby_db.removeItem(itemid)
2016-10-10 08:19:00 +00:00
# Remove artwork
artwork.delete_artwork(mvideoid, "musicvideo", self.kodicursor)
self.kodi_db.remove_musicvideo(mvideoid, fileid)
2016-10-10 08:19:00 +00:00
if self.direct_path:
self.kodi_db.remove_path(pathid)
2016-10-10 08:19:00 +00:00
2016-10-10 11:14:10 +00:00
log.info("Deleted musicvideo %s from kodi database", itemid)