PlexKodiConnect/resources/lib/playlists/db.py

117 lines
3.5 KiB
Python
Raw Normal View History

2018-07-12 05:24:27 +10:00
#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
Synced playlists are stored in our plex.db. Interact with it through this
module
"""
from __future__ import absolute_import, division, unicode_literals
2018-07-12 05:24:27 +10:00
from logging import getLogger
from .common import Playlist, PlaylistError
2018-10-25 02:17:02 +11:00
from ..plex_db import PlexDB
2018-11-09 07:22:16 +11:00
from ..kodi_db import kodiid_from_filename
2018-07-12 05:24:27 +10:00
from .. import path_ops, utils, variables as v
###############################################################################
LOG = getLogger('PLEX.playlists.db')
###############################################################################
def plex_playlist_ids():
"""
Returns a list of all Plex ids of the playlists already in our DB
"""
2018-10-25 02:17:02 +11:00
with PlexDB() as plexdb:
2018-10-25 22:19:46 +11:00
return list(plexdb.playlist_ids())
2018-07-12 05:24:27 +10:00
def kodi_playlist_paths():
"""
Returns a list of all Kodi playlist paths of the playlists already synced
"""
2018-10-25 02:17:02 +11:00
with PlexDB() as plexdb:
2018-10-25 22:19:46 +11:00
return list(plexdb.kodi_playlist_paths())
2018-07-12 05:24:27 +10:00
def update_playlist(playlist, delete=False):
"""
Assumes that all sync operations are over. Takes playlist [Playlist]
and creates/updates the corresponding Plex playlists table entry
Pass delete=True to delete the playlist entry
"""
2018-10-25 02:17:02 +11:00
with PlexDB() as plexdb:
2018-07-12 05:24:27 +10:00
if delete:
2018-10-25 02:17:02 +11:00
plexdb.delete_playlist(playlist)
2018-07-12 05:24:27 +10:00
else:
2018-10-25 02:17:02 +11:00
plexdb.add_playlist(playlist)
2018-07-12 05:24:27 +10:00
def get_playlist(path=None, plex_id=None):
2018-07-12 05:24:27 +10:00
"""
Returns the playlist as a Playlist for either the plex_id or path
2018-07-12 05:24:27 +10:00
"""
playlist = Playlist()
2018-10-25 02:17:02 +11:00
with PlexDB() as plexdb:
playlist = plexdb.playlist(playlist, plex_id, path)
2018-07-12 05:24:27 +10:00
return playlist
def _m3u_iterator(text):
"""
Yields e.g. plugin://plugin.video.plexkodiconnect.movies/?plex_id=xxx
"""
lines = iter(text.split('\n'))
for line in lines:
if line.startswith('#EXTINF:'):
next_line = next(lines).strip()
if next_line.startswith('#EXT-KX-OFFSET:'):
yield next(lines).strip()
else:
yield next_line
2018-07-12 05:24:27 +10:00
def m3u_to_plex_ids(playlist):
"""
Adapter to process *.m3u playlist files. Encoding is not uniform!
"""
plex_ids = list()
with open(path_ops.encode_path(playlist.kodi_path), 'rb') as f:
text = f.read()
try:
text = text.decode(v.M3U_ENCODING)
except UnicodeDecodeError:
LOG.warning('Fallback to ISO-8859-1 decoding for %s', playlist)
text = text.decode('ISO-8859-1')
for entry in _m3u_iterator(text):
plex_id = utils.REGEX_PLEX_ID.search(entry)
if plex_id:
plex_id = plex_id.group(1)
plex_ids.append(plex_id)
else:
# Add-on paths not working, try direct
2018-11-09 07:22:16 +11:00
kodi_id, kodi_type = kodiid_from_filename(entry,
db_type=playlist.kodi_type)
2018-07-12 05:24:27 +10:00
if not kodi_id:
continue
2018-10-25 02:17:02 +11:00
with PlexDB() as plexdb:
item = plexdb.item_by_kodi_id(kodi_id, kodi_type)
if item:
plex_ids.append(item['plex_id'])
2018-07-12 05:24:27 +10:00
return plex_ids
def playlist_file_to_plex_ids(playlist):
"""
Takes the playlist file located at path [unicode] and parses it.
Returns a list of plex_ids (str) or raises PL.PlaylistError if a single
item cannot be parsed from Kodi to Plex.
"""
if playlist.kodi_extension == 'm3u':
plex_ids = m3u_to_plex_ids(playlist)
else:
LOG.error('Unsupported playlist extension: %s',
playlist.kodi_extension)
raise PlaylistError
return plex_ids