2018-10-26 01:07:34 +11:00
|
|
|
#!/usr/bin/env python
|
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
from __future__ import absolute_import, division, unicode_literals
|
|
|
|
|
|
|
|
|
|
|
|
class Sections(object):
|
2019-02-08 06:15:49 +11:00
|
|
|
def all_sections(self):
|
2018-10-26 01:07:34 +11:00
|
|
|
"""
|
2019-02-08 06:15:49 +11:00
|
|
|
Returns an iterator for all sections
|
2018-10-26 01:07:34 +11:00
|
|
|
"""
|
|
|
|
self.cursor.execute('SELECT * FROM sections')
|
2019-02-08 06:15:49 +11:00
|
|
|
return (self.entry_to_section(x) for x in self.cursor)
|
2018-10-26 01:07:34 +11:00
|
|
|
|
|
|
|
def section(self, section_id):
|
|
|
|
"""
|
2019-02-08 06:15:49 +11:00
|
|
|
For section_id, returns the dict
|
2018-10-26 01:07:34 +11:00
|
|
|
section_id INTEGER PRIMARY KEY,
|
2019-04-27 18:41:07 +10:00
|
|
|
uuid TEXT,
|
2018-10-26 01:07:34 +11:00
|
|
|
section_name TEXT,
|
|
|
|
plex_type TEXT,
|
2019-02-08 06:15:49 +11:00
|
|
|
sync_to_kodi BOOL,
|
|
|
|
last_sync INTEGER
|
2018-10-26 01:07:34 +11:00
|
|
|
"""
|
|
|
|
self.cursor.execute('SELECT * FROM sections WHERE section_id = ? LIMIT 1',
|
|
|
|
(section_id, ))
|
2019-02-08 06:15:49 +11:00
|
|
|
return self.entry_to_section(self.cursor.fetchone())
|
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
def entry_to_section(entry):
|
|
|
|
if not entry:
|
|
|
|
return
|
|
|
|
return {
|
|
|
|
'section_id': entry[0],
|
2019-04-27 18:41:07 +10:00
|
|
|
'uuid': entry[1],
|
|
|
|
'section_name': entry[2],
|
|
|
|
'plex_type': entry[3],
|
2019-02-08 06:15:49 +11:00
|
|
|
'sync_to_kodi': entry[4] == 1,
|
|
|
|
'last_sync': entry[5]
|
|
|
|
}
|
2018-10-26 01:07:34 +11:00
|
|
|
|
|
|
|
def section_id_by_name(self, section_name):
|
|
|
|
"""
|
|
|
|
Returns the section_id for section_name (or None)
|
|
|
|
"""
|
2018-11-27 03:23:35 +11:00
|
|
|
self.cursor.execute('SELECT section_id FROM sections WHERE section_name = ? LIMIT 1',
|
2018-10-26 01:07:34 +11:00
|
|
|
(section_name, ))
|
|
|
|
try:
|
|
|
|
return self.cursor.fetchone()[0]
|
|
|
|
except TypeError:
|
|
|
|
pass
|
|
|
|
|
2019-04-27 18:41:07 +10:00
|
|
|
def add_section(self, section_id, uuid, section_name, plex_type,
|
2019-02-08 06:15:49 +11:00
|
|
|
sync_to_kodi, last_sync):
|
2018-10-26 01:07:34 +11:00
|
|
|
"""
|
|
|
|
Appends a Plex section to the Plex sections table
|
|
|
|
sync=False: Plex library won't be synced to Kodi
|
|
|
|
"""
|
|
|
|
query = '''
|
|
|
|
INSERT OR REPLACE INTO sections(
|
2019-02-08 06:15:49 +11:00
|
|
|
section_id,
|
2019-04-27 18:41:07 +10:00
|
|
|
uuid,
|
2019-02-08 06:15:49 +11:00
|
|
|
section_name,
|
|
|
|
plex_type,
|
|
|
|
sync_to_kodi,
|
|
|
|
last_sync)
|
|
|
|
VALUES (?, ?, ?, ?, ?, ?)
|
2018-10-26 01:07:34 +11:00
|
|
|
'''
|
|
|
|
self.cursor.execute(query,
|
|
|
|
(section_id,
|
2019-04-27 18:41:07 +10:00
|
|
|
uuid,
|
2018-10-26 01:07:34 +11:00
|
|
|
section_name,
|
|
|
|
plex_type,
|
2019-02-08 06:15:49 +11:00
|
|
|
sync_to_kodi,
|
|
|
|
last_sync))
|
|
|
|
|
|
|
|
def update_section(self, section_id, section_name):
|
|
|
|
"""
|
|
|
|
Updates the section with section_id
|
|
|
|
"""
|
|
|
|
query = 'UPDATE sections SET section_name = ? WHERE section_id = ?'
|
|
|
|
self.cursor.execute(query, (section_name, section_id))
|
2018-10-26 01:07:34 +11:00
|
|
|
|
|
|
|
def remove_section(self, section_id):
|
|
|
|
"""
|
|
|
|
Removes the Plex db entry for the section with section_id
|
|
|
|
"""
|
|
|
|
self.cursor.execute('DELETE FROM sections WHERE section_id = ?',
|
|
|
|
(section_id, ))
|
2019-02-08 06:15:49 +11:00
|
|
|
|
|
|
|
def update_section_sync(self, section_id, sync_to_kodi):
|
|
|
|
"""
|
|
|
|
Updates whether we should sync sections_id (sync=True) or not
|
|
|
|
"""
|
|
|
|
if sync_to_kodi:
|
|
|
|
query = '''
|
|
|
|
UPDATE sections
|
|
|
|
SET sync_to_kodi = ?
|
|
|
|
WHERE section_id = ?
|
|
|
|
'''
|
|
|
|
else:
|
|
|
|
# Set last_sync = 0 in order to force a full sync if reactivated
|
|
|
|
query = '''
|
|
|
|
UPDATE sections
|
|
|
|
SET sync_to_kodi = ?, last_sync = 0
|
|
|
|
WHERE section_id = ?
|
|
|
|
'''
|
|
|
|
self.cursor.execute(query, (sync_to_kodi, section_id))
|
|
|
|
|
|
|
|
def update_section_last_sync(self, section_id, last_sync):
|
|
|
|
"""
|
|
|
|
Updates the timestamp for the section
|
|
|
|
"""
|
|
|
|
self.cursor.execute('UPDATE sections SET last_sync = ? WHERE section_id = ?',
|
|
|
|
(last_sync, section_id))
|
|
|
|
|
|
|
|
def force_full_sync(self):
|
|
|
|
"""
|
|
|
|
Sets the last_sync flag to 0 for every section
|
|
|
|
"""
|
|
|
|
self.cursor.execute('UPDATE sections SET last_sync = 0')
|