-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathcreate.py
More file actions
359 lines (309 loc) · 15.5 KB
/
Copy pathcreate.py
File metadata and controls
359 lines (309 loc) · 15.5 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
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
#!/usr/bin/env python3
"""
create.py — Create Tunarr channels from channels.json.
Reads channels.json (output from LLM or generate_no_ai.py), deletes all
existing Tunarr channels, and creates fresh ones with rolling-loop schedules.
Usage:
python create.py # reads channels.json
python create.py --json myfile.json # use alternate file
python create.py --probe # dry run, no changes
python create.py --no-delete # create without deleting existing
"""
import argparse
import glob
import gzip
import json
import os
import sys
import time
import uuid
from datetime import datetime, timezone
from channel_engine import (
ChannelEngineError,
set_tunarr_auth_from_config,
SHUFFLE_MAP,
api,
build_library_index,
build_schedule,
get_plex_sections,
get_transcode_config,
load_franchise_index,
resolve_content,
set_programming,
)
CONFIG_FILE = "config.json"
DEFAULT_CHANNELS_FILE = "channels.json"
# ── Config ─────────────────────────────────────────────────────────────────────
def load_config():
try:
with open(CONFIG_FILE) as f:
return json.load(f)
except FileNotFoundError:
print(f"ERROR: {CONFIG_FILE} not found.")
sys.exit(1)
# ── Destructive-op backup ──────────────────────────────────────────────────────
# Measured on a real 156-channel Tunarr: the uncompressed snapshot was 151 MB
# (full program metadata for ~1000 programs per channel). Ten of those would put
# 1.5 GB in a user's data directory, so backups are gzipped (10x, ~1.4s) and we
# keep fewer of them. 3 x ~15 MB is a safety net; 10 x 151 MB is a disk problem.
BACKUP_KEEP = 3
BACKUP_GLOB = "tunarr_backup_*.json.gz"
def backup_channels(tunarr_url, channels):
"""Dump channels + their raw programming to a timestamped file before deleting.
This is the only safety net for an irreversible wipe: a user who points
Programmarr at a Tunarr with a hand-built lineup and hits Deploy would
otherwise lose it with no way back. We store the raw
/api/channels/{id}/programming payload (not just program ids) so the file is
actually enough to rebuild from.
Never raises — a backup failure must not be the thing that blocks a deploy,
but it IS reported loudly so the user can decide to stop.
"""
try:
snapshot = []
for ch in channels:
entry = {"channel": ch}
prog = api(tunarr_url, "GET", f"/api/channels/{ch['id']}/programming")
if prog is not None:
entry["programming"] = prog
snapshot.append(entry)
ts = datetime.now(timezone.utc).strftime("%Y%m%dT%H%M%SZ")
path = f"tunarr_backup_{ts}.json.gz"
# No indent: this file is read by a restore, not by a human, and pretty
# printing 150 MB of JSON buys nothing.
with gzip.open(path, "wt", encoding="utf-8") as f:
json.dump({"saved_at": ts, "tunarr_url": tunarr_url,
"channels": snapshot}, f)
# ponytail: keep the last N by filename (timestamps sort lexically); no
# rotation config until someone asks for one.
for stale in sorted(glob.glob(BACKUP_GLOB))[:-BACKUP_KEEP]:
try:
os.remove(stale)
except OSError:
pass
size_mb = os.path.getsize(path) / 1e6
print(f" Backed up {len(snapshot)} channels ({size_mb:.1f} MB) -> {os.path.abspath(path)}")
return path
except Exception as e:
print(f" ! WARNING: could not write pre-delete backup: {e}")
print(" ! Deleting anyway — stop now (Ctrl-C) if you need these channels.")
return None
# ── Channel operations ─────────────────────────────────────────────────────────
def delete_channels(tunarr_url, probe, from_ch=None, protect=None):
protect = protect or set()
existing = api(tunarr_url, "GET", "/api/channels") or []
if not existing:
print(" No existing channels to delete")
return
in_scope = [ch for ch in existing if from_ch is None or ch.get("number", 0) >= from_ch]
targets = [ch for ch in in_scope if ch.get("number", 0) not in protect]
preserved = [ch for ch in in_scope if ch.get("number", 0) in protect]
if not targets and not preserved:
print(f" No channels >= {from_ch} to delete")
return
scope = f">= #{from_ch}" if from_ch is not None else "all"
if targets:
print(f" Deleting {len(targets)} channels ({scope})...")
if not probe:
backup_channels(tunarr_url, targets)
for ch in targets:
if probe:
print(f" [PROBE] Would delete #{ch['number']} {ch['name']}")
else:
result = api(tunarr_url, "DELETE", f"/api/channels/{ch['id']}")
if result is not None:
print(f" Deleted #{ch['number']} {ch['name']}")
time.sleep(0.1)
for ch in preserved:
print(f" {'[PROBE] ' if probe else ''}Preserving #{ch['number']} {ch['name']} (protected)")
def create_channel(tunarr_url, number, name, transcode_id, filler_list_id=None,
channel_group=None, stream_mode=None):
channel_id = str(uuid.uuid4())
# Commercials: attach a filler list at the channel level. Tunarr's FillerPicker
# fills the schedule's flex gaps (opened by build_schedule's pad_ms) with these
# clips at playback. Empty list = no commercials (default).
filler_collections = (
[{"id": filler_list_id, "weight": 100, "cooldownSeconds": 30}] if filler_list_id else []
)
body = {
"type": "new",
"channel": {
"id": channel_id,
"number": number,
"name": name,
"startTime": int(datetime.now(timezone.utc).timestamp() * 1000),
"duration": 0,
"groupTitle": channel_group or "tunarr",
"guideMinimumDuration": 30000,
"fillerRepeatCooldown": 30000,
"fillerCollections": filler_collections,
"disableFillerOverlay": False,
"transcodeConfigId": transcode_id,
"streamMode": stream_mode or "hls",
"stealth": False,
"subtitlesEnabled": False,
"icon": {"path": "", "width": 0, "duration": 0, "position": "bottom-right"},
"offline": {"mode": "pic", "picture": "", "soundtrack": ""},
"watermark": {
"enabled": False,
"width": 10,
"verticalMargin": 1,
"horizontalMargin": 1,
"position": "bottom-right",
"opacity": 100,
"animated": False,
"fixedSize": False,
"duration": 0,
"url": "",
},
"onDemand": {"enabled": False},
},
}
result = api(tunarr_url, "POST", "/api/channels", body=body)
if result and "id" not in result:
result["id"] = channel_id
return result
# ── Main ───────────────────────────────────────────────────────────────────────
def main():
parser = argparse.ArgumentParser(description="Create Tunarr channels from channels.json")
parser.add_argument("--json", default=DEFAULT_CHANNELS_FILE, help="Channel definition JSON file")
parser.add_argument("--probe", action="store_true", help="Dry run — show what would be created")
parser.add_argument("--no-delete", action="store_true", help="Skip deleting existing channels")
parser.add_argument("--from", dest="from_ch", type=int, default=None, metavar="N",
help="Only operate on channels numbered N and above (preserves lower channels)")
parser.add_argument("--protect", dest="protect", default="", metavar="NUMS",
help="Comma-separated channel numbers to protect from deletion")
args = parser.parse_args()
cfg = load_config()
set_tunarr_auth_from_config(cfg)
tunarr_url = cfg["tunarr_url"].rstrip("/")
plex_url = cfg.get("plex_url", "").rstrip("/")
plex_token = cfg.get("plex_token", "")
# Optional advanced config (absent = Tunarr defaults). streamMode is a lowercase
# enum (hls|hls_slower|mpegts|hls_direct|hls_direct_v2); normalize user input.
channel_group = cfg.get("tunarr_channel_group") or None
stream_mode = (cfg.get("tunarr_stream_mode") or "").strip().lower() or None
# ── Load channel definitions ───────────────────────────────────────────────
try:
with open(args.json, encoding="utf-8") as f:
data = json.load(f)
except FileNotFoundError:
print(f"ERROR: {args.json} not found. Run the LLM step first.")
sys.exit(1)
channels = data.get("channels", [])
if not channels:
print("ERROR: No channels found in JSON")
sys.exit(1)
channels.sort(key=lambda c: c.get("number", 999))
if args.from_ch is not None:
channels = [c for c in channels if c.get("number", 0) >= args.from_ch]
print(f"Loaded {len(channels)} channels from {args.json} (filtered to #{args.from_ch}+)")
else:
print(f"Loaded {len(channels)} channels from {args.json}")
# ── Set up Plex collection lookup if needed ────────────────────────────────
uses_collections = any(
isinstance(item, dict) and "collection" in item
for ch in channels
for item in ch.get("content", [])
)
franchise_index = load_franchise_index(".")
plex_sections = []
collection_cache = {}
if uses_collections:
if not plex_url or not plex_token:
print("ERROR: plex_url and plex_token required in config.json for collection support")
sys.exit(1)
print("\nDiscovering Plex sections for collection lookup...")
plex_sections = get_plex_sections(plex_url, plex_token)
print(f" Found {len(plex_sections)} sections")
# ── Build library index ────────────────────────────────────────────────────
print("\nIndexing Tunarr library...")
try:
movie_map, show_map = build_library_index(tunarr_url)
except ChannelEngineError as e:
print(f"ERROR: {e}")
sys.exit(1)
transcode_id = get_transcode_config(tunarr_url)
if not transcode_id and not args.probe:
print("ERROR: No transcode config found in Tunarr")
sys.exit(1)
# ── Delete existing channels ───────────────────────────────────────────────
protect_set: set[int] = set()
if args.protect:
for n in args.protect.split(","):
try:
protect_set.add(int(n.strip()))
except ValueError:
pass
if not args.no_delete:
print("\nDeleting existing channels...")
delete_channels(tunarr_url, args.probe, from_ch=args.from_ch, protect=protect_set)
# ── Create channels ────────────────────────────────────────────────────────
print(f"\n{'[PROBE] ' if args.probe else ''}Creating {len(channels)} channels...")
stats = {"created": 0, "skipped": 0, "missing_titles": []}
for ch in channels:
number = ch.get("number")
name = ch.get("name", "Unnamed")
shuffle = SHUFFLE_MAP.get(ch.get("shuffle", "shuffle"), "shuffle")
content_list = ch.get("content", [])
# Commercials (optional): attach a filler list + pad episodes to open the gap.
comm = ch.get("commercials") or {}
comm_filler = comm.get("filler_list_id")
comm_pad_ms = int(comm.get("pad_minutes", 5)) * 60000 if comm_filler else 0
resolved, missing = resolve_content(
content_list, movie_map, show_map,
plex_url=plex_url, plex_token=plex_token,
plex_sections=plex_sections, collection_cache=collection_cache,
franchise_index=franchise_index,
)
if not resolved:
print(f" SKIP #{number} {name} — no content found in library")
stats["skipped"] += 1
if missing:
stats["missing_titles"].extend([(name, t) for t in missing])
continue
if probe := args.probe:
tv_count = sum(1 for r in resolved if r["type"] == "TV")
movie_count = sum(1 for r in resolved if r["type"] == "Movie")
ep_count = sum(len(r["programs"]) for r in resolved if r["type"] == "TV")
comm_note = f" | commercials ({comm.get('pad_minutes', 5)}m gaps)" if comm_filler else ""
print(f" [PROBE] #{number} {name} | shuffle={shuffle} | "
f"{tv_count} shows ({ep_count} eps) + {movie_count} movies{comm_note}")
if missing:
print(f" Missing: {', '.join(missing[:5])}{'...' if len(missing) > 5 else ''}")
stats["created"] += 1
continue
# Create channel
ch_result = create_channel(tunarr_url, number, name, transcode_id, filler_list_id=comm_filler,
channel_group=channel_group, stream_mode=stream_mode)
if not ch_result:
print(f" FAIL #{number} {name} — channel creation failed")
stats["skipped"] += 1
continue
channel_id = ch_result.get("id")
# Build and post schedule (pad opens the commercial gap when enabled)
schedule = build_schedule(shuffle, resolved, pad_ms=comm_pad_ms, playback=ch.get("playback"))
if not schedule:
print(f" FAIL #{number} {name} — could not build schedule")
stats["skipped"] += 1
continue
prog_result = set_programming(tunarr_url, channel_id, schedule)
if prog_result is not None:
ep_count = sum(len(r["programs"]) for r in resolved)
print(f" Created #{number} {name} ({ep_count} programs, shuffle={shuffle})")
if missing:
print(f" Not found: {', '.join(missing[:5])}{'...' if len(missing) > 5 else ''}")
stats["created"] += 1
else:
print(f" FAIL #{number} {name} — programming failed")
stats["skipped"] += 1
time.sleep(0.2)
# ── Summary ────────────────────────────────────────────────────────────────
print(f"\n{'[PROBE] ' if args.probe else ''}Done: {stats['created']} created, {stats['skipped']} skipped")
if stats["missing_titles"]:
print(f"\nTitles not found in Tunarr library ({len(stats['missing_titles'])} total):")
for channel_name, title in stats["missing_titles"][:20]:
print(f" [{channel_name}] {title}")
if len(stats["missing_titles"]) > 20:
print(f" ... and {len(stats['missing_titles']) - 20} more")
if __name__ == "__main__":
main()