Karan Adhikari commited on
Commit
20fd956
Β·
unverified Β·
1 Parent(s): e36e660

Update stremio_routes.py

Browse files
Files changed (1) hide show
  1. Backend/fastapi/routes/stremio_routes.py +163 -161
Backend/fastapi/routes/stremio_routes.py CHANGED
@@ -245,165 +245,6 @@ async def get_manifest(token: str, token_data: dict = Depends(verify_token)):
245
  }
246
 
247
 
248
- @router.get("/{token}/configure")
249
- async def configure_addon(token: str):
250
- """
251
- Configure/update page for the Stremio addon.
252
- Uses the correct stremio://addon_install?manifest= deep-link so Stremio
253
- actually shows the Install/Update dialog when the button is clicked.
254
- """
255
- from urllib.parse import quote
256
- from fastapi.responses import HTMLResponse
257
- from Backend import db as _db
258
-
259
- manifest_url = f"{Telegram.BASE_URL}/stremio/{token}/manifest.json"
260
- # Universal Stremio web install β€” works on desktop and mobile
261
- web_install_url = f"https://web.stremio.com/#/?addon_manifest={quote(manifest_url, safe='')}"
262
-
263
- # Fetch user info for display
264
- token_doc = await _db.get_api_token(token)
265
- user_name = "Unknown"
266
- expiry_str = "N/A"
267
- status_color = "#ef4444"
268
- status_text = "Unknown"
269
-
270
- if token_doc:
271
- uid = token_doc.get("user_id")
272
- if uid:
273
- try:
274
- user = await _db.get_user(int(uid))
275
- if user:
276
- user_name = user.get("first_name") or user.get("username") or f"User {uid}"
277
- sub_status = user.get("subscription_status", "")
278
- expiry = user.get("subscription_expiry")
279
- if expiry:
280
- expiry_str = expiry.strftime("%d %b %Y").lstrip("0")
281
- if sub_status == "active":
282
- status_color = "#22c55e"
283
- status_text = "βœ… Active"
284
- else:
285
- status_color = "#ef4444"
286
- status_text = "πŸ”΄ Expired"
287
- except Exception:
288
- pass
289
-
290
- html = f"""<!DOCTYPE html>
291
- <html lang="en">
292
- <head>
293
- <meta charset="utf-8">
294
- <meta name="viewport" content="width=device-width, initial-scale=1">
295
- <title>Update Telegram Stremio Addon</title>
296
- <style>
297
- * {{ box-sizing: border-box; margin: 0; padding: 0; }}
298
- body {{
299
- font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', sans-serif;
300
- background: #0f0f1a; color: #e2e8f0;
301
- min-height: 100vh; display: flex; align-items: center; justify-content: center;
302
- padding: 24px;
303
- }}
304
- .card {{
305
- background: #1e1e2e; border: 1px solid #2d2d44; border-radius: 16px;
306
- padding: 40px 32px; max-width: 480px; width: 100%; text-align: center;
307
- }}
308
- .logo {{ font-size: 48px; margin-bottom: 12px; }}
309
- h1 {{ font-size: 1.5rem; font-weight: 700; color: #f8fafc; margin-bottom: 6px; }}
310
- .sub-title {{ color: #94a3b8; font-size: 0.9rem; margin-bottom: 28px; }}
311
- .info-row {{
312
- display: flex; justify-content: space-between; align-items: center;
313
- background: #2a2a3e; border-radius: 10px; padding: 12px 16px;
314
- margin-bottom: 12px; font-size: 0.9rem;
315
- }}
316
- .info-label {{ color: #94a3b8; }}
317
- .info-val {{ font-weight: 600; color: #f1f5f9; }}
318
- .status-badge {{
319
- display: inline-block; padding: 2px 10px; border-radius: 999px;
320
- font-size: 0.8rem; font-weight: 700;
321
- background: {status_color}22; color: {status_color};
322
- }}
323
- .btn-update {{
324
- display: block; width: 100%;
325
- background: linear-gradient(135deg, #7c3aed, #4f46e5);
326
- color: white; font-weight: 700; font-size: 1rem;
327
- padding: 14px 24px; border-radius: 12px; border: none;
328
- cursor: pointer; text-decoration: none; margin: 28px 0 12px;
329
- transition: opacity 0.2s;
330
- }}
331
- .btn-update:hover {{ opacity: 0.85; }}
332
- .btn-web {{
333
- display: block; color: #6366f1; font-size: 0.85rem;
334
- text-decoration: underline; margin-bottom: 20px;
335
- }}
336
- .steps {{
337
- background: #2a2a3e; border-radius: 10px; padding: 14px 18px;
338
- margin: 16px 0; text-align: left; font-size: 0.85rem; color: #cbd5e1;
339
- }}
340
- .steps b {{ color: #f1f5f9; }}
341
- .steps ol {{ margin-top: 8px; margin-left: 18px; line-height: 1.8; }}
342
- .url-box {{
343
- background: #111827; border: 1px solid #374151; border-radius: 8px;
344
- padding: 10px 14px; font-family: monospace; font-size: 0.75rem;
345
- color: #94a3b8; word-break: break-all; text-align: left; margin-top: 16px;
346
- }}
347
- .btn-copy {{
348
- margin-top: 10px; width: 100%; padding: 10px;
349
- background: #1e293b; border: 1px solid #374151; color: #94a3b8;
350
- border-radius: 8px; cursor: pointer; font-size: 0.85rem; transition: all 0.2s;
351
- }}
352
- .btn-copy:hover {{ background: #334155; color: #f1f5f9; }}
353
- .hint {{ color: #64748b; font-size: 0.78rem; margin-top: 6px; }}
354
- </style>
355
- </head>
356
- <body>
357
- <div class="card">
358
- <div class="logo">🎬</div>
359
- <h1>Telegram Stremio Addon</h1>
360
- <p class="sub-title">Click the button below to install or update your addon in Stremio.</p>
361
-
362
- <div class="info-row">
363
- <span class="info-label">User</span>
364
- <span class="info-val">{user_name}</span>
365
- </div>
366
- <div class="info-row">
367
- <span class="info-label">Status</span>
368
- <span class="status-badge">{status_text}</span>
369
- </div>
370
- <div class="info-row">
371
- <span class="info-label">Expires</span>
372
- <span class="info-val">{expiry_str}</span>
373
- </div>
374
-
375
- <a href="{web_install_url}" class="btn-update" target="_blank">
376
- ⚑ Install / Update in Stremio
377
- </a>
378
-
379
- <div class="steps">
380
- <b>Or install manually:</b>
381
- <ol>
382
- <li>Open Stremio β†’ <b>Add-ons</b> tab</li>
383
- <li>Click the <b>πŸ” Search / URL</b> icon</li>
384
- <li>Paste the URL below and press Enter</li>
385
- </ol>
386
- </div>
387
-
388
- <div class="url-box" id="murl">{manifest_url}</div>
389
- <button onclick="copyUrl()" class="btn-copy">πŸ“‹ Copy URL</button>
390
- <script>
391
- function copyUrl() {{
392
- navigator.clipboard.writeText('{manifest_url}').then(() => {{
393
- const b = document.querySelector('.btn-copy');
394
- b.textContent = 'βœ… Copied!';
395
- setTimeout(() => b.textContent = 'πŸ“‹ Copy URL', 2000);
396
- }});
397
- }}
398
- </script>
399
- </div>
400
- </body>
401
- </html>"""
402
- return HTMLResponse(html)
403
-
404
-
405
-
406
-
407
  @router.get("/{token}/catalog/{media_type}/{id}/{extra:path}.json")
408
  @router.get("/{token}/catalog/{media_type}/{id}.json")
409
  async def get_catalog(token: str, media_type: str, id: str, extra: Optional[str] = None, token_data: dict = Depends(verify_token)):
@@ -604,7 +445,7 @@ async def get_streams(
604
  )
605
 
606
  original_url = f"{BASE_URL}/dl/{token}/{quality.get('id')}/video.mkv"
607
- proxy_url = f"{Telegram.HTTP_PROXY_URL}{original_url}" if Telegram.PROXY and Telegram.HTTP_PROXY_URL else None
608
 
609
  if Telegram.SHOW_PROXY_AND_NON_PROXY_BOTH and proxy_url:
610
  streams.append({
@@ -647,4 +488,165 @@ async def get_streams(
647
  seen[s["name"]] = seen.get(s["name"], 0) + 1
648
  s["name"] = f"{s['name']} ({seen[s['name']]})"
649
 
650
- return {"streams": streams}
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
245
  }
246
 
247
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
248
  @router.get("/{token}/catalog/{media_type}/{id}/{extra:path}.json")
249
  @router.get("/{token}/catalog/{media_type}/{id}.json")
250
  async def get_catalog(token: str, media_type: str, id: str, extra: Optional[str] = None, token_data: dict = Depends(verify_token)):
 
445
  )
446
 
447
  original_url = f"{BASE_URL}/dl/{token}/{quality.get('id')}/video.mkv"
448
+ proxy_url = f"{Telegram.HTTP_PROXY_URL}{original_url}" if Telegram.HTTP_PROXY_URL else None
449
 
450
  if Telegram.SHOW_PROXY_AND_NON_PROXY_BOTH and proxy_url:
451
  streams.append({
 
488
  seen[s["name"]] = seen.get(s["name"], 0) + 1
489
  s["name"] = f"{s['name']} ({seen[s['name']]})"
490
 
491
+ return {"streams": streams}
492
+
493
+
494
+
495
+
496
+
497
+
498
+ @router.get("/{token}/configure")
499
+ async def configure_addon(token: str):
500
+ """
501
+ Configure/update page for the Stremio addon.
502
+ Uses the correct stremio://addon_install?manifest= deep-link so Stremio
503
+ actually shows the Install/Update dialog when the button is clicked.
504
+ """
505
+ from urllib.parse import quote
506
+ from fastapi.responses import HTMLResponse
507
+ from Backend import db as _db
508
+
509
+ manifest_url = f"{Telegram.BASE_URL}/stremio/{token}/manifest.json"
510
+ # Universal Stremio web install β€” works on desktop and mobile
511
+ web_install_url = f"https://web.stremio.com/#/?addon_manifest={quote(manifest_url, safe='')}"
512
+
513
+ # Fetch user info for display
514
+ token_doc = await _db.get_api_token(token)
515
+ user_name = "Unknown"
516
+ expiry_str = "N/A"
517
+ status_color = "#ef4444"
518
+ status_text = "Unknown"
519
+
520
+ if token_doc:
521
+ uid = token_doc.get("user_id")
522
+ if uid:
523
+ try:
524
+ user = await _db.get_user(int(uid))
525
+ if user:
526
+ user_name = user.get("first_name") or user.get("username") or f"User {uid}"
527
+ sub_status = user.get("subscription_status", "")
528
+ expiry = user.get("subscription_expiry")
529
+ if expiry:
530
+ expiry_str = expiry.strftime("%d %b %Y").lstrip("0")
531
+ if sub_status == "active":
532
+ status_color = "#22c55e"
533
+ status_text = "βœ… Active"
534
+ else:
535
+ status_color = "#ef4444"
536
+ status_text = "πŸ”΄ Expired"
537
+ except Exception:
538
+ pass
539
+
540
+ html = f"""<!DOCTYPE html>
541
+ <html lang="en">
542
+ <head>
543
+ <meta charset="utf-8">
544
+ <meta name="viewport" content="width=device-width, initial-scale=1">
545
+ <title>Update Telegram Stremio Addon</title>
546
+ <style>
547
+ * {{ box-sizing: border-box; margin: 0; padding: 0; }}
548
+ body {{
549
+ font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', sans-serif;
550
+ background: #0f0f1a; color: #e2e8f0;
551
+ min-height: 100vh; display: flex; align-items: center; justify-content: center;
552
+ padding: 24px;
553
+ }}
554
+ .card {{
555
+ background: #1e1e2e; border: 1px solid #2d2d44; border-radius: 16px;
556
+ padding: 40px 32px; max-width: 480px; width: 100%; text-align: center;
557
+ }}
558
+ .logo {{ font-size: 48px; margin-bottom: 12px; }}
559
+ h1 {{ font-size: 1.5rem; font-weight: 700; color: #f8fafc; margin-bottom: 6px; }}
560
+ .sub-title {{ color: #94a3b8; font-size: 0.9rem; margin-bottom: 28px; }}
561
+ .info-row {{
562
+ display: flex; justify-content: space-between; align-items: center;
563
+ background: #2a2a3e; border-radius: 10px; padding: 12px 16px;
564
+ margin-bottom: 12px; font-size: 0.9rem;
565
+ }}
566
+ .info-label {{ color: #94a3b8; }}
567
+ .info-val {{ font-weight: 600; color: #f1f5f9; }}
568
+ .status-badge {{
569
+ display: inline-block; padding: 2px 10px; border-radius: 999px;
570
+ font-size: 0.8rem; font-weight: 700;
571
+ background: {status_color}22; color: {status_color};
572
+ }}
573
+ .btn-update {{
574
+ display: block; width: 100%;
575
+ background: linear-gradient(135deg, #7c3aed, #4f46e5);
576
+ color: white; font-weight: 700; font-size: 1rem;
577
+ padding: 14px 24px; border-radius: 12px; border: none;
578
+ cursor: pointer; text-decoration: none; margin: 28px 0 12px;
579
+ transition: opacity 0.2s;
580
+ }}
581
+ .btn-update:hover {{ opacity: 0.85; }}
582
+ .btn-web {{
583
+ display: block; color: #6366f1; font-size: 0.85rem;
584
+ text-decoration: underline; margin-bottom: 20px;
585
+ }}
586
+ .steps {{
587
+ background: #2a2a3e; border-radius: 10px; padding: 14px 18px;
588
+ margin: 16px 0; text-align: left; font-size: 0.85rem; color: #cbd5e1;
589
+ }}
590
+ .steps b {{ color: #f1f5f9; }}
591
+ .steps ol {{ margin-top: 8px; margin-left: 18px; line-height: 1.8; }}
592
+ .url-box {{
593
+ background: #111827; border: 1px solid #374151; border-radius: 8px;
594
+ padding: 10px 14px; font-family: monospace; font-size: 0.75rem;
595
+ color: #94a3b8; word-break: break-all; text-align: left; margin-top: 16px;
596
+ }}
597
+ .btn-copy {{
598
+ margin-top: 10px; width: 100%; padding: 10px;
599
+ background: #1e293b; border: 1px solid #374151; color: #94a3b8;
600
+ border-radius: 8px; cursor: pointer; font-size: 0.85rem; transition: all 0.2s;
601
+ }}
602
+ .btn-copy:hover {{ background: #334155; color: #f1f5f9; }}
603
+ .hint {{ color: #64748b; font-size: 0.78rem; margin-top: 6px; }}
604
+ </style>
605
+ </head>
606
+ <body>
607
+ <div class="card">
608
+ <div class="logo">🎬</div>
609
+ <h1>Telegram Stremio Addon</h1>
610
+ <p class="sub-title">Click the button below to install or update your addon in Stremio.</p>
611
+
612
+ <div class="info-row">
613
+ <span class="info-label">User</span>
614
+ <span class="info-val">{user_name}</span>
615
+ </div>
616
+ <div class="info-row">
617
+ <span class="info-label">Status</span>
618
+ <span class="status-badge">{status_text}</span>
619
+ </div>
620
+ <div class="info-row">
621
+ <span class="info-label">Expires</span>
622
+ <span class="info-val">{expiry_str}</span>
623
+ </div>
624
+
625
+ <a href="{web_install_url}" class="btn-update" target="_blank">
626
+ ⚑ Install / Update in Stremio
627
+ </a>
628
+
629
+ <div class="steps">
630
+ <b>Or install manually:</b>
631
+ <ol>
632
+ <li>Open Stremio β†’ <b>Add-ons</b> tab</li>
633
+ <li>Click the <b>πŸ” Search / URL</b> icon</li>
634
+ <li>Paste the URL below and press Enter</li>
635
+ </ol>
636
+ </div>
637
+
638
+ <div class="url-box" id="murl">{manifest_url}</div>
639
+ <button onclick="copyUrl()" class="btn-copy">πŸ“‹ Copy URL</button>
640
+ <script>
641
+ function copyUrl() {{
642
+ navigator.clipboard.writeText('{manifest_url}').then(() => {{
643
+ const b = document.querySelector('.btn-copy');
644
+ b.textContent = 'βœ… Copied!';
645
+ setTimeout(() => b.textContent = 'πŸ“‹ Copy URL', 2000);
646
+ }});
647
+ }}
648
+ </script>
649
+ </div>
650
+ </body>
651
+ </html>"""
652
+ return HTMLResponse(html)