my-private-iptv-m3u/scripts/config_manager.py
stoney420 3c1e14558f
All checks were successful
Generate M3U Playlist / build (push) Successful in 1m34s
Update scripts/config_manager.py
2025-06-27 23:58:01 +02:00

214 lines
No EOL
12 KiB
Python

"""
Configuration Manager - FIXED for correct file paths
"""
import json
import os
import logging
from pathlib import Path
class ConfigManager:
"""Centralized configuration management with FIXED paths."""
def __init__(self, config_dir="config"):
# Get the parent directory (go up from scripts/ to root)
script_dir = Path(__file__).parent
root_dir = script_dir.parent
self.config_dir = root_dir / config_dir
self.config_dir.mkdir(exist_ok=True)
# FIXED: File paths now point to root directory
self.channels_file = str(root_dir / "channels.txt")
self.playlist_file = str(root_dir / "playlist.m3u")
self.import_file = str(root_dir / "bulk_import.m3u")
self.log_file = str(root_dir / "playlist_update.log")
# Config files in config subdirectory
self.settings_file = self.config_dir / "settings.json"
self.patterns_file = self.config_dir / "patterns.json"
self.group_overrides_file = self.config_dir / "group_overrides.json"
# Load configurations
self.settings = self._load_settings()
self.patterns = self._load_patterns()
self.group_overrides = self._load_group_overrides()
# Debug logging
logging.info(f"Root directory: {root_dir}")
logging.info(f"Config channels_file: {self.channels_file}")
logging.info(f"Config import_file: {self.import_file}")
logging.info(f"Config playlist_file: {self.playlist_file}")
# Check if files exist
logging.info(f"bulk_import.m3u exists: {os.path.exists(self.import_file)}")
logging.info(f"channels.txt exists: {os.path.exists(self.channels_file)}")
logging.info("Configuration manager initialized with FIXED paths")
def _load_settings(self):
"""Load settings with comprehensive defaults."""
defaults = {
"remove_duplicates": True,
"sort_channels": True,
"backup_before_import": True,
"auto_cleanup_import": True,
"auto_detect_country": True,
"detect_quality": True,
"skip_adult_content": True,
"min_channel_name_length": 2,
"max_workers": 4,
"enable_health_check": False,
"health_check_timeout": 5,
"create_backup": True,
"max_backups": 5,
"log_level": "INFO"
}
if self.settings_file.exists():
try:
with open(self.settings_file, 'r', encoding='utf-8') as f:
user_settings = json.load(f)
merged = {**defaults, **user_settings}
logging.info(f"Loaded user settings from {self.settings_file}")
return merged
except (json.JSONDecodeError, IOError) as e:
logging.warning(f"Could not load settings, using defaults: {e}")
else:
# Create default settings file
with open(self.settings_file, 'w', encoding='utf-8') as f:
json.dump(defaults, f, indent=2)
logging.info(f"Created default settings file: {self.settings_file}")
return defaults
def _load_patterns(self):
"""Load country detection patterns from external config."""
default_patterns = {
"country_patterns": {
"🇺🇸 United States": ["cbs", "nbc", "abc", "fox", "espn", "cnn", "hbo", " usa", " us ", ".us", "america", "nfl"],
"🇬🇧 United Kingdom": ["bbc", "itv", "sky", "channel 4", "e4", " uk", ".uk", "british", "premier league"],
"🇨🇦 Canada": ["cbc", "ctv", "global", "canada", "canadian", " ca ", ".ca"],
"🇩🇪 Germany": ["ard", "zdf", "rtl", "sat.1", "pro7", "germany", "german", " de ", ".de"],
"🇫🇷 France": ["tf1", "france 2", "m6", "canal+", "france", "french", " fr ", ".fr"],
"🇪🇸 Spain": ["tve", "antena 3", "telecinco", "spain", "spanish", " es ", ".es"],
"🇮🇹 Italy": ["rai", "mediaset", "canale 5", "italy", "italian", " it ", ".it"],
"🇳🇱 Netherlands": ["npo", "rtl nl", "netherlands", "dutch", "holland", " nl ", ".nl"],
"🇧🇪 Belgium": ["vtm", "één", "canvas", "belgium", "belgian", " be ", ".be"],
"🇨🇭 Switzerland": ["srf", "rts", "switzerland", "swiss", " ch ", ".ch"],
"🇦🇹 Austria": ["orf", "austria", "austrian", " at ", ".at"],
"🇵🇹 Portugal": ["rtp", "sic", "tvi", "portugal", "portuguese", " pt ", ".pt"],
"🇮🇪 Ireland": ["rte", "tg4", "ireland", "irish", " ie ", ".ie"],
"🇸🇪 Sweden": ["svt", "tv4", "sweden", "swedish", " se ", ".se"],
"🇳🇴 Norway": ["nrk", "tv 2 no", "norway", "norwegian", " no ", ".no"],
"🇩🇰 Denmark": ["dr", "tv2 dk", "denmark", "danish", " dk ", ".dk"],
"🇫🇮 Finland": ["yle", "mtv3", "finland", "finnish", " fi ", ".fi"],
"🇮🇸 Iceland": ["ruv", "iceland", "icelandic", " is ", ".is"],
"🇷🇺 Russia": ["channel one", "rossiya", "ntv", "russia", "russian", " ru ", ".ru"],
"🇵🇱 Poland": ["tvp", "polsat", "tvn", "poland", "polish", " pl ", ".pl"],
"🇨🇿 Czech Republic": ["ct", "nova", "prima", "czech", " cz ", ".cz"],
"🇸🇰 Slovakia": ["rtvs", "markiza", "slovakia", "slovak", " sk ", ".sk"],
"🇭🇺 Hungary": ["mtv hu", "rtl klub", "hungary", "hungarian", " hu ", ".hu"],
"🇺🇦 Ukraine": ["1+1", "inter", "ictv", "ukraine", "ukrainian", " ua ", ".ua"],
"🇷🇴 Romania": ["tvr", "pro tv", "romania", "romanian", " ro ", ".ro"],
"🇧🇬 Bulgaria": ["btv", "nova bg", "bulgaria", "bulgarian", " bg ", ".bg"],
"🇭🇷 Croatia": ["hrt", "nova tv hr", "croatia", "croatian", " hr ", ".hr"],
"🇷🇸 Serbia": ["rts", "pink", "serbia", "serbian", " rs ", ".rs"],
"🇬🇷 Greece": ["ert", "mega gr", "greece", "greek", " gr ", ".gr"],
"🇧🇷 Brazil": ["globo", "band", "sbt", "brazil", "brasil", " br ", ".br"],
"🇦🇷 Argentina": ["telefe", "canal 13", "argentina", " ar ", ".ar"],
"🇲🇽 Mexico": ["televisa", "tv azteca", "mexico", "méxico", " mx ", ".mx"],
"🇨🇱 Chile": ["tvn", "mega", "chile", "chilean", " cl ", ".cl"],
"🇨🇴 Colombia": ["caracol", "rcn", "colombia", "colombian", " co ", ".co"],
"🇵🇪 Peru": ["america tv pe", "peru", "peruvian", " pe ", ".pe"],
"🇻🇪 Venezuela": ["venevision", "venezuela", "venezuelan", " ve ", ".ve"],
"🇨🇳 China": ["cctv", "phoenix", "china", "chinese", " cn ", ".cn"],
"🇯🇵 Japan": ["nhk", "fuji", "tv asahi", "japan", "japanese", " jp ", ".jp"],
"🇰🇷 South Korea": ["kbs", "sbs kr", "mbc kr", "korea", "korean", " kr ", ".kr"],
"🇰🇵 North Korea": ["kctv", "north korea", "dprk"],
"🇹🇼 Taiwan": ["cts", "ctv", "tvbs", "taiwan", "taiwanese", " tw ", ".tw"],
"🇭🇰 Hong Kong": ["tvb", "atv", "hong kong", "hongkong", " hk ", ".hk"],
"🇹🇭 Thailand": ["ch3", "ch7", "thai pbs", "thailand", "thai", " th ", ".th"],
"🇻🇳 Vietnam": ["vtv", "htv", "vietnam", "vietnamese", " vn ", ".vn"],
"🇮🇩 Indonesia": ["tvri", "sctv", "rcti", "indonesia", "indonesian", " id ", ".id"],
"🇲🇾 Malaysia": ["tv1", "tv3", "astro", "malaysia", "malaysian", " my ", ".my", "my:"],
"🇸🇬 Singapore": ["channel 5", "channel 8", "singapore", " sg ", ".sg"],
"🇵🇭 Philippines": ["abs-cbn", "gma", "philippines", "filipino", " ph ", ".ph"],
"🇮🇳 India": ["star plus", "zee tv", "colors", "sony tv", "india", "indian", "hindi", " in ", ".in"],
"🇵🇰 Pakistan": ["ptv", "geo tv", "ary", "pakistan", "pakistani", " pk ", ".pk"],
"🇧🇩 Bangladesh": ["btv", "channel i", "bangladesh", "bangladeshi", " bd ", ".bd"],
"🇱🇰 Sri Lanka": ["rupavahini", "sirasa", "sri lanka", " lk ", ".lk"],
"🇳🇵 Nepal": ["nepal tv", "kantipur", "nepal", "nepali", " np ", ".np"],
"🇦🇫 Afghanistan": ["rta", "tolo tv", "afghanistan", "afghan", " af ", ".af"],
"🇦🇺 Australia": ["abc au", "seven", "nine", "ten", "australia", "australian", "aussie", " au ", ".au"],
"🇳🇿 New Zealand": ["tvnz", "tvnz 1", "tvnz 2", "three nz", "tvnz duke", "new zealand", "kiwi", " nz ", ".nz"],
"🇸🇦 Arabic": ["al jazeera", "mbc", "lbc", "dubai tv", "arabic", "arab", "qatar", "dubai", "saudi"],
"🇮🇱 Israel": ["kan", "keshet 12", "israel", "israeli", "hebrew", " il ", ".il"],
"🇹🇷 Turkey": ["trt", "atv", "kanal d", "turkey", "turkish", " tr ", ".tr", "tr |"],
"🇮🇷 Iran": ["irib", "press tv", "iran", "iranian", "persian", " ir ", ".ir"],
"🇪🇬 Egypt": ["nile tv", "cbc egypt", "egypt", "egyptian", " eg ", ".eg"],
"🇿🇦 South Africa": ["sabc", "etv", "mnet", "south africa", " za ", ".za"],
"🇳🇬 Nigeria": ["nta", "channels tv", "nigeria", "nigerian", " ng ", ".ng"]
},
"country_prefixes": {
"🇺🇦 Ukraine": ["ua |"],
"🇵🇱 Poland": ["pl |"],
"🇹🇷 Turkey": ["tr |"],
"🇲🇾 Malaysia": ["my:", "my |"],
"🇬🇧 United Kingdom": ["uk:", "uk |"],
"🇺🇸 United States": ["us:", "us |"]
},
"quality_patterns": {
"4K": ["4k", "uhd", "2160p"],
"FHD": ["fhd", "1080p", "1080"],
"HD": ["hd", "720p", "720"],
"SD": ["sd", "480p", "360p"]
},
"adult_keywords": ["xxx", "adult", "porn", "sex", "erotic", "playboy", "18+"]
}
if self.patterns_file.exists():
try:
with open(self.patterns_file, 'r', encoding='utf-8') as f:
user_patterns = json.load(f)
# Deep merge with defaults
for category, patterns in user_patterns.items():
if category in default_patterns:
if isinstance(default_patterns[category], dict):
default_patterns[category].update(patterns)
else:
default_patterns[category] = patterns
else:
default_patterns[category] = patterns
logging.info(f"Loaded user patterns from {self.patterns_file}")
except (json.JSONDecodeError, IOError) as e:
logging.warning(f"Could not load patterns, using defaults: {e}")
else:
# Create default patterns file for user customization
with open(self.patterns_file, 'w', encoding='utf-8') as f:
json.dump(default_patterns, f, indent=2, ensure_ascii=False)
logging.info(f"Created default patterns file: {self.patterns_file}")
return default_patterns
def _load_group_overrides(self):
"""Load group overrides."""
if self.group_overrides_file.exists():
try:
with open(self.group_overrides_file, 'r', encoding='utf-8') as f:
overrides = json.load(f)
logging.info(f"Loaded {len(overrides)} group overrides")
return overrides
except (json.JSONDecodeError, IOError) as e:
logging.warning(f"Could not load group overrides: {e}")
else:
# Create empty overrides file as example
example_overrides = {
"example_keyword": "🇺🇸 United States",
"another_keyword": "🇬🇧 United Kingdom"
}
with open(self.group_overrides_file, 'w', encoding='utf-8') as f:
json.dump(example_overrides, f, indent=2, ensure_ascii=False)
logging.info(f"Created example group overrides file: {self.group_overrides_file}")
return {}