2020-03-22 15:37:15 +01:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
""" Metadata module """
|
|
|
|
|
|
|
|
from __future__ import absolute_import, division, unicode_literals
|
|
|
|
|
2020-11-10 23:18:41 +01:00
|
|
|
import os
|
|
|
|
|
2020-03-22 15:37:15 +01:00
|
|
|
from resources.lib import kodiutils
|
|
|
|
from resources.lib.viervijfzes import CHANNELS
|
2020-10-26 10:25:57 +01:00
|
|
|
from resources.lib.viervijfzes.content import CACHE_AUTO, CACHE_PREVENT, ContentApi, Program
|
2020-03-22 15:37:15 +01:00
|
|
|
|
|
|
|
|
|
|
|
class Metadata:
|
|
|
|
""" Code responsible for the management of the local cached metadata """
|
|
|
|
|
|
|
|
def __init__(self):
|
|
|
|
""" Initialise object """
|
2020-04-01 11:01:22 +02:00
|
|
|
self._api = ContentApi(cache_path=kodiutils.get_cache_path())
|
2020-03-22 15:37:15 +01:00
|
|
|
|
|
|
|
def update(self):
|
|
|
|
""" Update the metadata with a foreground progress indicator """
|
|
|
|
# Create progress indicator
|
2020-06-19 15:20:20 +02:00
|
|
|
progress = kodiutils.progress(message=kodiutils.localize(30715)) # Updating metadata...
|
2020-03-22 15:37:15 +01:00
|
|
|
|
|
|
|
def update_status(i, total):
|
|
|
|
""" Update the progress indicator """
|
2020-06-19 15:20:20 +02:00
|
|
|
progress.update(int(((i + 1) / total) * 100),
|
|
|
|
kodiutils.localize(30716, index=i + 1, total=total)) # Updating metadata ({index}/{total})...
|
2020-03-22 15:37:15 +01:00
|
|
|
return progress.iscanceled()
|
|
|
|
|
2020-04-01 11:01:22 +02:00
|
|
|
self.fetch_metadata(callback=update_status, refresh=True)
|
2020-03-22 15:37:15 +01:00
|
|
|
|
|
|
|
# Close progress indicator
|
|
|
|
progress.close()
|
|
|
|
|
2020-04-01 11:01:22 +02:00
|
|
|
def fetch_metadata(self, callback=None, refresh=False):
|
2020-03-22 15:37:15 +01:00
|
|
|
""" Fetch the metadata for all the items in the catalog
|
|
|
|
:type callback: callable
|
2020-04-01 11:01:22 +02:00
|
|
|
:type refresh: bool
|
2020-03-22 15:37:15 +01:00
|
|
|
"""
|
|
|
|
# Fetch all items from the catalog
|
|
|
|
items = []
|
|
|
|
for channel in list(CHANNELS):
|
2020-04-01 11:01:22 +02:00
|
|
|
items.extend(self._api.get_programs(channel, CACHE_PREVENT))
|
2020-03-22 15:37:15 +01:00
|
|
|
count = len(items)
|
|
|
|
|
|
|
|
# Loop over all of them and download the metadata
|
|
|
|
for index, item in enumerate(items):
|
|
|
|
if isinstance(item, Program):
|
2020-04-01 11:01:22 +02:00
|
|
|
self._api.get_program(item.channel, item.path, CACHE_PREVENT if refresh else CACHE_AUTO)
|
2020-03-22 15:37:15 +01:00
|
|
|
|
|
|
|
# Run callback after every item
|
|
|
|
if callback and callback(index, count):
|
2020-06-19 15:20:20 +02:00
|
|
|
# Stop when callback returns True
|
2020-03-22 15:37:15 +01:00
|
|
|
return False
|
|
|
|
|
|
|
|
return True
|
2020-06-19 15:20:20 +02:00
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
def clean():
|
|
|
|
""" Clear metadata (called from settings) """
|
|
|
|
cache_path = kodiutils.get_cache_path()
|
|
|
|
_, files = kodiutils.listdir(cache_path)
|
|
|
|
for filename in files:
|
2020-11-10 23:18:41 +01:00
|
|
|
if not kodiutils.delete(os.path.join(cache_path, filename)):
|
|
|
|
return kodiutils.ok_dialog(message=kodiutils.localize(30721)) # Clearing local metadata failed
|
2020-06-19 15:20:20 +02:00
|
|
|
|
|
|
|
kodiutils.set_setting('metadata_last_updated', '0')
|
2020-11-10 23:18:41 +01:00
|
|
|
return kodiutils.ok_dialog(message=kodiutils.localize(30714)) # Local metadata is cleared
|