Why Gemfury? Push, build, and install  RubyGems npm packages Python packages Maven artifacts PHP packages Go Modules Debian packages RPM packages NuGet packages

Repository URL to install this package:

Details    
Size: Mime:
# !/usr/bin/python
# coding=utf-8
#
# Copyright (C) 2018-2025 by dream-alpha
#
# In case of reuse of this source code please do not remove this copyright.
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# For more information on the GNU General Public License see:
# <http://www.gnu.org/licenses/>.


import os
from time import time, strftime, localtime
from Components.config import config
from .ChannelUtils import readChannelDict, readChannelList
from .TVMagazineData import TVMagazineData
from .FileUtils import createDirectory, deleteDirectory
from .WebRequests import WebRequests
from .Index import idx
from .DateTimeUtils import timestamp_to_day_int
from .EventUtils import find_time_event_index
from .Debug import logger


class Cache:
    def __init__(self):
        logger.info("...")
        self.events = {}
        self.channel_dict = readChannelDict()
        self.channel_list = readChannelList(self.channel_dict)
        self.date_str = strftime("%Y-%m-%d", localtime(int(time())))
        self.tvmagazine_data = TVMagazineData(self.channel_dict)
        self.temp_dir = os.path.join(
            config.plugins.tvmagazinecockpit.temp_dir.value, self.date_str)
        self.cache_path = os.path.join(self.temp_dir, "events.json")
        self.data_source_id = config.plugins.tvmagazinecockpit.data_source.value + "_id"
        createDirectory(self.temp_dir)

    def downloadEvents(self):
        if not os.path.exists(self.cache_path):
            logger.debug("channel_list: %s", self.channel_list)
            self.tvmagazine_data.downloadEvents(
                self.date_str,
                self.channel_list,
                self.events,
                self.downloadEventsCallback
            )

    def downloadEventsCallback(self, events):
        logger.info("events: %s", events)
        start_time = timestamp_to_day_int(time()) + 20 * 3600 + 15 * 60

        events_of_the_day = events.get(self.date_str, {})
        for service_ref in events_of_the_day:
            channel_id = self.channel_dict.get(
                service_ref, {}).get(self.data_source_id, "")
            event_list = events_of_the_day.get(service_ref, {})
            i = find_time_event_index(event_list, start_time)
            if i != -1:
                event = event_list[i]
                event = self.tvmagazine_data.getDetailedEvent(event)
                if event:
                    url = event[idx["photo_url"]]
                    ident = "%s-%s" % (event[idx["startTime"]], channel_id)
                    path = os.path.join(
                        self.temp_dir, "programpix-" + ident + ".jpg")
                    WebRequests().downloadFile(url, path)

    def cleanup(self):
        logger.info("Cleaning up old cache directories...")
        adir = os.path.dirname(self.temp_dir)
        for name in os.listdir(adir):
            path = os.path.join(adir, name)
            if os.path.isdir(path) and name != self.date_str:
                deleteDirectory(path)