Repository navigation
Expand file tree
/
Copy pathmodels.py
More file actions
245 lines (223 loc) · 11.1 KB
/
Copy pathmodels.py
File metadata and controls
245 lines (223 loc) · 11.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
import os
import re
import logging
from sqlalchemy import create_engine, Column, Integer, String, Boolean, Text
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy.orm import sessionmaker
logging.basicConfig(level=logging.INFO)
logger = logging.getLogger(__name__)
BASE_DIR = os.path.abspath(os.path.dirname(__file__))
# Use data directory for database
DATA_DIR = os.path.join(BASE_DIR, 'data')
os.makedirs(DATA_DIR, exist_ok=True)
DB_PATH = os.path.join(DATA_DIR, 'data.db')
SQLALCHEMY_DATABASE_URL = f"sqlite:///{DB_PATH}"
engine = create_engine(
SQLALCHEMY_DATABASE_URL,
connect_args={
"check_same_thread": False,
"timeout": 5 # 5 second timeout on connections
},
pool_pre_ping=True, # Verify connection is still valid before using
pool_recycle=3600, # Recycle connections after 1 hour
)
SessionLocal = sessionmaker(autocommit=False, autoflush=False, bind=engine)
Base = declarative_base()
class Item(Base):
__tablename__ = "items"
id = Column(Integer, primary_key=True, index=True)
name = Column(String(200), nullable=False)
server_url = Column(String(200), nullable=False)
username = Column(String(200), nullable=False)
user_pass = Column(String(200), nullable=False)
languages = Column(String(200), nullable=True)
includes = Column(String(200), nullable=True)
excludes = Column(String(200), nullable=True)
xtream_includes = Column(String(10000), nullable=True)
m3u_refresh_hours = Column(Integer, nullable=True, default=0)
epg_channels = Column(String(1000), nullable=True)
provider_status = Column(String(50), nullable=True)
provider_exp_date = Column(String(50), nullable=True)
vpn_enabled = Column(Boolean, nullable=True, default=False)
vpn_config = Column(Text, nullable=True)
vpn_username = Column(String(500), nullable=True)
vpn_password = Column(String(500), nullable=True)
max_sessions = Column(Integer, nullable=True, default=1)
mqtt_enabled = Column(Boolean, nullable=True, default=False)
mqtt_host = Column(String(200), nullable=True)
mqtt_port = Column(Integer, nullable=True, default=1883)
mqtt_username = Column(String(200), nullable=True)
mqtt_password = Column(String(500), nullable=True)
mqtt_topic_prefix = Column(String(200), nullable=True, default="iptv-manager")
mqtt_ha_discovery = Column(Boolean, nullable=True, default=False)
mqtt_device_name = Column(String(200), nullable=True, default="IPTV Manager")
slug = Column(String(100), nullable=True)
proxy_username = Column(String(200), nullable=True)
proxy_password = Column(String(200), nullable=True)
enabled = Column(Boolean, nullable=False, default=True)
class AppConfig(Base):
__tablename__ = "app_config"
key = Column(String(100), primary_key=True)
value = Column(String(500), nullable=True)
class EpgChannelMap(Base):
"""Explicit tvg-id → EPG source channel-id overrides. Bypasses fuzzy name matching."""
__tablename__ = "epg_channel_map"
tvg_id = Column(String(200), primary_key=True) # our M3U tvg-id
epg_source_id = Column(String(200), nullable=False) # EPG <channel id> to use
def _slugify(name: str) -> str:
s = name.lower().strip()
s = re.sub(r"[^a-z0-9]+", "-", s)
return s.strip("-") or "provider"
def init_db():
Base.metadata.create_all(bind=engine)
with engine.connect() as conn:
from sqlalchemy import text as _text
conn.execute(_text("PRAGMA journal_mode=WAL"))
conn.execute(_text("PRAGMA synchronous=NORMAL"))
conn.commit()
# Migrate existing databases that predate new columns
with engine.connect() as conn:
from sqlalchemy import text, inspect
existing = [col["name"] for col in inspect(engine).get_columns("items")]
if "xtream_includes" not in existing:
conn.execute(text("ALTER TABLE items ADD COLUMN xtream_includes VARCHAR(10000)"))
conn.commit()
logger.info("Migrated: added xtream_includes column")
if "m3u_refresh_hours" not in existing:
conn.execute(text("ALTER TABLE items ADD COLUMN m3u_refresh_hours INTEGER DEFAULT 0"))
conn.commit()
logger.info("Migrated: added m3u_refresh_hours column")
if "provider_status" not in existing:
conn.execute(text("ALTER TABLE items ADD COLUMN provider_status VARCHAR(50)"))
conn.commit()
logger.info("Migrated: added provider_status column")
if "provider_exp_date" not in existing:
conn.execute(text("ALTER TABLE items ADD COLUMN provider_exp_date VARCHAR(50)"))
conn.commit()
logger.info("Migrated: added provider_exp_date column")
if "vpn_enabled" not in existing:
conn.execute(text("ALTER TABLE items ADD COLUMN vpn_enabled BOOLEAN DEFAULT 0"))
conn.commit()
logger.info("Migrated: added vpn_enabled column")
if "vpn_config" not in existing:
conn.execute(text("ALTER TABLE items ADD COLUMN vpn_config TEXT"))
conn.commit()
logger.info("Migrated: added vpn_config column")
if "vpn_username" not in existing:
conn.execute(text("ALTER TABLE items ADD COLUMN vpn_username VARCHAR(500)"))
conn.commit()
logger.info("Migrated: added vpn_username column")
if "vpn_password" not in existing:
conn.execute(text("ALTER TABLE items ADD COLUMN vpn_password VARCHAR(500)"))
conn.commit()
logger.info("Migrated: added vpn_password column")
if "max_sessions" not in existing:
conn.execute(text("ALTER TABLE items ADD COLUMN max_sessions INTEGER DEFAULT 1"))
conn.commit()
logger.info("Migrated: added max_sessions column")
if "mqtt_enabled" not in existing:
conn.execute(text("ALTER TABLE items ADD COLUMN mqtt_enabled BOOLEAN DEFAULT 0"))
conn.commit()
logger.info("Migrated: added mqtt_enabled column")
if "mqtt_host" not in existing:
conn.execute(text("ALTER TABLE items ADD COLUMN mqtt_host VARCHAR(200)"))
conn.commit()
logger.info("Migrated: added mqtt_host column")
if "mqtt_port" not in existing:
conn.execute(text("ALTER TABLE items ADD COLUMN mqtt_port INTEGER DEFAULT 1883"))
conn.commit()
logger.info("Migrated: added mqtt_port column")
if "mqtt_username" not in existing:
conn.execute(text("ALTER TABLE items ADD COLUMN mqtt_username VARCHAR(200)"))
conn.commit()
logger.info("Migrated: added mqtt_username column")
if "mqtt_password" not in existing:
conn.execute(text("ALTER TABLE items ADD COLUMN mqtt_password VARCHAR(500)"))
conn.commit()
logger.info("Migrated: added mqtt_password column")
if "mqtt_topic_prefix" not in existing:
conn.execute(text("ALTER TABLE items ADD COLUMN mqtt_topic_prefix VARCHAR(200) DEFAULT 'iptv-manager'"))
conn.commit()
logger.info("Migrated: added mqtt_topic_prefix column")
if "mqtt_ha_discovery" not in existing:
conn.execute(text("ALTER TABLE items ADD COLUMN mqtt_ha_discovery BOOLEAN DEFAULT 0"))
conn.commit()
logger.info("Migrated: added mqtt_ha_discovery column")
if "mqtt_device_name" not in existing:
conn.execute(text("ALTER TABLE items ADD COLUMN mqtt_device_name VARCHAR(200) DEFAULT 'IPTV Manager'"))
conn.commit()
logger.info("Migrated: added mqtt_device_name column")
if "slug" not in existing:
conn.execute(text("ALTER TABLE items ADD COLUMN slug VARCHAR(100)"))
conn.commit()
logger.info("Migrated: added slug column")
if "proxy_username" not in existing:
conn.execute(text("ALTER TABLE items ADD COLUMN proxy_username VARCHAR(200)"))
conn.commit()
logger.info("Migrated: added proxy_username column")
if "proxy_password" not in existing:
conn.execute(text("ALTER TABLE items ADD COLUMN proxy_password VARCHAR(200)"))
conn.commit()
logger.info("Migrated: added proxy_password column")
if "enabled" not in existing:
conn.execute(text("ALTER TABLE items ADD COLUMN enabled BOOLEAN NOT NULL DEFAULT 1"))
conn.commit()
logger.info("Migrated: added enabled column")
# Data migration: assign slugs to items that don't have one
rows = conn.execute(text("SELECT id, name, slug FROM items WHERE slug IS NULL OR slug = ''")).fetchall()
if rows:
used_slugs = set(
r[0] for r in conn.execute(text("SELECT slug FROM items WHERE slug IS NOT NULL AND slug != ''")).fetchall()
)
for row_id, row_name, _ in rows:
base = _slugify(row_name or "provider")
slug = base
i = 2
while slug in used_slugs:
slug = f"{base}-{i}"
i += 1
used_slugs.add(slug)
conn.execute(text("UPDATE items SET slug = :slug WHERE id = :id"), {"slug": slug, "id": row_id})
conn.commit()
logger.info("Migrated: assigned slugs to %d items", len(rows))
# Data migration: assign proxy credentials from env vars if not set
no_creds = conn.execute(
text("SELECT id FROM items WHERE proxy_username IS NULL OR proxy_username = ''")
).fetchall()
if no_creds:
env_user = os.getenv("IPTV_USERNAME", "iptv")
env_pass = os.getenv("IPTV_PASSWORD", "iptv")
conn.execute(
text("UPDATE items SET proxy_username = :u, proxy_password = :p WHERE proxy_username IS NULL OR proxy_username = ''"),
{"u": env_user, "p": env_pass},
)
conn.commit()
logger.info("Migrated: set proxy credentials for %d items", len(no_creds))
def get_db():
db = SessionLocal()
try:
yield db
finally:
db.close()
def get_app_config(db, key: str, default=None):
row = db.query(AppConfig).filter(AppConfig.key == key).first()
return row.value if row else default
def set_app_config(db, key: str, value):
row = db.query(AppConfig).filter(AppConfig.key == key).first()
if row:
row.value = value
else:
db.add(AppConfig(key=key, value=value))
db.commit()
def get_epg_channel_map(db) -> dict[str, str]:
"""Return all explicit EPG overrides as {tvg_id: epg_source_id}."""
return {row.tvg_id: row.epg_source_id for row in db.query(EpgChannelMap).all()}
def save_epg_channel_map(db, mappings: dict[str, str]) -> None:
"""Overwrite all EPG channel overrides. Empty string values are treated as deletions."""
db.query(EpgChannelMap).delete()
for tvg_id, epg_source_id in mappings.items():
tvg_id = tvg_id.strip()
epg_source_id = epg_source_id.strip()
if tvg_id and epg_source_id:
db.add(EpgChannelMap(tvg_id=tvg_id, epg_source_id=epg_source_id))
db.commit()