From 5db6c55aff934da14dfd03c97352f744244c0d89 Mon Sep 17 00:00:00 2001 From: pQu4k3r Date: Thu, 10 Sep 2026 13:05:11 +0000 Subject: [PATCH] Auto-create new_city.cfg from successful online searches README documents that the offline city list "is created automatically during a search" if it doesn't exist, but that mechanism was never actually implemented anywhere in the codebase - new_city.cfg was only ever read, never written. Implemented it: after a successful online search (search_online), append any newly found cities to new_city.cfg, deduped by city id against what's already in the file, in the same "ID/City_Name" format the reader already expects (verified with a write/read round-trip, including a non-ASCII city name). Creates SYSTEM_DIR if needed, mirroring the pattern already used elsewhere (e.g. ForecaSetup.save in plugin.py). The offline list now builds up over time as users search, so City Selection's offline browsing (and search_offline's fallback) actually has something to work with after the fix in #4/#5. Co-Authored-By: Claude Sonnet 5 Claude-Session: https://claude.ai/code/session_01UPpumFb2PP21ATpDwJBYBB --- .../Plugins/Extensions/Foreca1/city_panel.py | 48 +++++++++++++++++++ 1 file changed, 48 insertions(+) diff --git a/usr/lib/enigma2/python/Plugins/Extensions/Foreca1/city_panel.py b/usr/lib/enigma2/python/Plugins/Extensions/Foreca1/city_panel.py index 4f846802..f486df1e 100644 --- a/usr/lib/enigma2/python/Plugins/Extensions/Foreca1/city_panel.py +++ b/usr/lib/enigma2/python/Plugins/Extensions/Foreca1/city_panel.py @@ -6,6 +6,7 @@ import requests from urllib.parse import quote_plus +from os import makedirs from os.path import exists, join from enigma import eListboxPythonMultiContent, gFont, RT_VALIGN_CENTER, eTimer, eListbox @@ -320,6 +321,7 @@ def search_online(self, search_term): # Build new list with online results new_entries = [] new_city_list = [] + offline_entries = [] for res in results: city_id = res.get("id") name = res.get("name", "") @@ -329,6 +331,8 @@ def search_online(self, search_term): display_name, city_id=city_id, is_header=False) new_entries.append(entry) new_city_list.append((display_name, city_id)) + if city_id and name: + offline_entries.append((city_id, name)) # Replace the filtered list with the online one self.filtered_list = new_entries @@ -340,8 +344,52 @@ def search_online(self, search_term): self["description"].setText( _("Found %d cities online for '%s'") % (count, search_term)) + + self._append_to_offline_city_list(offline_entries) return True + def _append_to_offline_city_list(self, entries): + """Append newly found cities to new_city.cfg so the offline list + builds up automatically over time (README-documented behavior). + Skips ids already present in the file. + """ + if not entries: + return + + city_cfg_path = join(SYSTEM_DIR, "new_city.cfg") + existing_ids = set() + if exists(city_cfg_path): + try: + with open(city_cfg_path, "r", encoding="utf-8") as f: + for line in f: + line = line.strip() + if not line or line.startswith("#") or "/" not in line: + continue + existing_ids.add(line.split("/", 1)[0]) + except Exception as e: + print(f"[CityPanel4] Error reading offline list for dedup: {e}") + return + + new_lines = [ + f"{city_id}/{name.replace(' ', '_')}" + for city_id, name in entries + if str(city_id) not in existing_ids + ] + if not new_lines: + return + + try: + if not exists(SYSTEM_DIR): + makedirs(SYSTEM_DIR, exist_ok=True) + with open(city_cfg_path, "a", encoding="utf-8") as f: + for line in new_lines: + f.write(line + "\n") + if DEBUG: + print( + f"[CityPanel4] Appended {len(new_lines)} cities to offline list") + except Exception as e: + print(f"[CityPanel4] Error appending to offline list: {e}") + def search_offline(self, search_term): """Search the local file (already loaded into self.Mlist).""" search_term_lower = search_term.lower()