From 105cf9c90ca4e8a2537da39a4082dec85ec41042 Mon Sep 17 00:00:00 2001 From: Dmitriy Yefremov Date: Mon, 5 Feb 2018 14:44:42 +0300 Subject: [PATCH] new algorithm for picons retrieving --- app/picons/picons.py | 32 ++++++++++++++++---------------- app/ui/main_app_window.py | 12 +++++++++++- app/ui/main_window.glade | 5 +++-- app/ui/picons_dialog.glade | 1 + app/ui/picons_dialog.py | 8 +++++--- 5 files changed, 36 insertions(+), 22 deletions(-) diff --git a/app/picons/picons.py b/app/picons/picons.py index 250370ea..165a3cd4 100644 --- a/app/picons/picons.py +++ b/app/picons/picons.py @@ -3,13 +3,11 @@ import shutil from collections import namedtuple from html.parser import HTMLParser -from app.commons import log +from app.commons import log, run_task from app.properties import Profile - -_ENIGMA2_PICON_NAME = "1_0_{}_{:X}_{:X}_{:X}_{:X}0000_0_0_0.png" -_NEUTRINO_PICON_NAME = "{:x}{:04x}{:04x}.png" - +_ENIGMA2_PICON_KEY = "{:X}:{:X}:{:X}0000" +_NEUTRINO_PICON_KEY = "{:x}{:04x}{:04x}.png" Provider = namedtuple("Provider", ["logo", "name", "pos", "url", "on_id", "selected"]) Picon = namedtuple("Picon", ["ref", "ssid", "v_pid"]) @@ -73,7 +71,7 @@ class PiconsParser(HTMLParser): pass @staticmethod - def parse(open_path, picons_path, tmp_path, on_id, pos, profile=Profile.ENIGMA_2): + def parse(open_path, picons_path, tmp_path, on_id, pos, picon_ids, profile=Profile.ENIGMA_2): with open(open_path, encoding="utf-8", errors="replace") as f: parser = PiconsParser() parser.reset() @@ -83,19 +81,20 @@ class PiconsParser(HTMLParser): os.makedirs(picons_path, exist_ok=True) for p in picons: try: - picon_file_name = picons_path + PiconsParser.format(p.ssid, on_id, p.v_pid, pos, profile) - shutil.copyfile(tmp_path + "www.lyngsat.com/" + p.ref.lstrip("."), picon_file_name) + name = PiconsParser.format(p.ssid, on_id, p.v_pid, pos, picon_ids, profile) + p_name = picons_path + (name if name else os.path.basename(p.ref)) + shutil.copyfile(tmp_path + "www.lyngsat.com/" + p.ref.lstrip("."), p_name) except (TypeError, ValueError) as e: - log("Picons format parse error: {} {} {}".format(p.ref, p.ssid, p.v_pid) + "\n" + str(e)) + log("Picons format parse error: {}".format(p) + "\n" + str(e)) print(e) @staticmethod - def format(ssid, on_id, v_pid, pos, profile: Profile): + def format(ssid, on_id, v_pid, pos, picon_ids, profile: Profile): tr_id = int(ssid[:-2] if len(ssid) < 4 else ssid[:2]) if profile is Profile.ENIGMA_2: - return _ENIGMA2_PICON_NAME.format(1 if v_pid else 2, int(ssid), tr_id, int(on_id), int(pos)) + return picon_ids.get(_ENIGMA2_PICON_KEY.format(int(ssid), int(on_id), int(pos)), None) elif profile is Profile.NEUTRINO_MP: - return _NEUTRINO_PICON_NAME.format(tr_id, int(on_id), int(ssid)) + return _NEUTRINO_PICON_KEY.format(tr_id, int(on_id), int(ssid)) else: return "{}.png".format(ssid) @@ -168,15 +167,16 @@ class ProviderParser(HTMLParser): def error(self, message): pass - def reset_counter(self): + def reset(self): + super().reset() self._counter = 0 def parse_providers(open_path): + parser = ProviderParser() + parser.reset() + with open(open_path, encoding="utf-8", errors="replace") as f: - parser = ProviderParser() - parser.reset() - parser.reset_counter() parser.feed(f.read()) rows = parser.rows diff --git a/app/ui/main_app_window.py b/app/ui/main_app_window.py index c40e550c..22f70e72 100644 --- a/app/ui/main_app_window.py +++ b/app/ui/main_app_window.py @@ -132,6 +132,7 @@ class MainAppWindow: self.__profile_label.set_text("Enigma2 v.4" if Profile(self.__profile) is Profile.ENIGMA_2 else "Neutrino-MP") # dynamically active elements depending on the selected view self.__tool_elements = {k: builder.get_object(k) for k in self.__DYNAMIC_ELEMENTS} + self.__picons_download_tool_button = builder.get_object("picons_download_tool_button") self.__cas_label = builder.get_object("cas_label") self.__fav_count_label = builder.get_object("fav_count_label") self.__bouquets_count_label = builder.get_object("bouquets_count_label") @@ -499,6 +500,7 @@ class MainAppWindow: self.append_services(data_path) self.update_services_counts(len(self.__services_model)) self.update_picons() + self.__picons_download_tool_button.set_sensitive(len(self.__services_model)) except FileNotFoundError as e: show_dialog(DialogType.ERROR, self.__main_window, getattr(e, "message", str(e)) + "\n\nPlease, download files from receiver or setup your path for read data!") @@ -664,6 +666,7 @@ class MainAppWindow: self.__profile = profile self.clear_current_data() self.update_services_counts() + self.__picons_download_tool_button.set_sensitive(len(self.__services_model)) def on_tree_view_key_release(self, view, event): """ Handling keystrokes """ @@ -836,8 +839,15 @@ class MainAppWindow: def on_locate_in_services(self, view): locate_in_services(view, self.__services_view, self.__main_window) + @run_idle def on_picons_loader_show(self, item): - dialog = PiconsDialog(self.__main_window, self.__options.get(self.__profile), Profile(self.__profile)) + ids = {} + if Profile(self.__profile) is Profile.ENIGMA_2: + for r in self.__services_model: + data = r[9].split("_") + ids["{}:{}:{}".format(data[3], data[5], data[6])] = r[9] + + dialog = PiconsDialog(self.__main_window, self.__options.get(self.__profile), ids, Profile(self.__profile)) dialog.show() self.update_picons() diff --git a/app/ui/main_window.glade b/app/ui/main_window.glade index ed29a9f0..74c50561 100644 --- a/app/ui/main_window.glade +++ b/app/ui/main_window.glade @@ -446,7 +446,7 @@ True False - mail-send-receive + network-transmit-receive @@ -743,7 +743,7 @@ FTP-transfer Download True - mail-send-receive + network-transmit-receive @@ -1090,6 +1090,7 @@ True + False False Picons Picons loader diff --git a/app/ui/picons_dialog.glade b/app/ui/picons_dialog.glade index e8713063..d29b04ce 100644 --- a/app/ui/picons_dialog.glade +++ b/app/ui/picons_dialog.glade @@ -416,6 +416,7 @@ + autosize Position diff --git a/app/ui/picons_dialog.py b/app/ui/picons_dialog.py index eee0d886..9851f08a 100644 --- a/app/ui/picons_dialog.py +++ b/app/ui/picons_dialog.py @@ -15,7 +15,8 @@ from .main_helper import update_entry_data class PiconsDialog: - def __init__(self, transient, options, profile=Profile.ENIGMA_2): + def __init__(self, transient, options, picon_ids, profile=Profile.ENIGMA_2): + self._picon_ids = picon_ids self._TMP_DIR = tempfile.gettempdir() + "/" self._BASE_URL = "www.lyngsat.com/packages/" self._PATTERN = re.compile("^https://www\.lyngsat\.com/[\w-]+\.html$") @@ -130,7 +131,8 @@ class PiconsDialog: self._current_process.wait() path = self._TMP_DIR + self._BASE_URL + url[url.rfind("/") + 1:] pos = "".join(c for c in prv.pos if c.isdigit()) - PiconsParser.parse(path, self._picons_path, self._TMP_DIR, prv.on_id, pos, self.get_picons_format()) + PiconsParser.parse(path, self._picons_path, self._TMP_DIR, prv.on_id, pos, + self._picon_ids, self.get_picons_format()) self.resize(self._picons_path) self.show_info_message("Done", Gtk.MessageType.INFO) @@ -225,7 +227,7 @@ class PiconsDialog: def get_selected_providers(self): """ returns selected providers """ - return [r for r in self._providers_tree_view.get_model() if r[4]] + return [r for r in self._providers_tree_view.get_model() if r[5]] @run_idle def show_dialog(self, message, dialog_type):