#!/usr/bin/env python3
"""Patch mini_app.py: add default site URLs"""
import re

with open('/home/matthiasberner/Schreibtisch/kodi/xstreamDownloader/mini_app.py') as f:
    c = f.read()

old = "    url = request.args.get('url', '')\n    result = _run_site_in_subprocess('showEntries', ident, url)"
new = """    url = request.args.get('url', '')
    DEFAULTS = {
        'hdfilme': 'https://hdfilme.stream/filme',
        'fhdfilme': 'https://fhdfilme.stream',
        'aniworld': 'https://aniworld.to',
        'serienstream': 'https://serienstream.to',
        'burningseries': 'https://burningseries.cx',
        'filmpalast': 'https://filmpalast.to',
        'kinoger': 'https://kinoger.com',
        'kkiste': 'https://kkiste.movie',
        'netzkino': 'https://netzkino.com',
        'moflix-stream': 'https://moflix-stream.com',
        'topstreamfilm': 'https://topstreamfilm.com',
        'streamcloud': 'https://streamcloud.store',
        'megakino': 'https://megakino.com',
        'einschalten': 'https://einschalten.io',
        'internetarchive': 'https://archive.org',
        'animetoast': 'https://animetoast.org',
        'tmdb_browser': '',
        'api_sites': '',
    }
    if not url and ident in DEFAULTS:
        url = DEFAULTS[ident]
    result = _run_site_in_subprocess('showEntries', ident, url)"""

if old in c and 'DEFAULTS' not in c:
    c = c.replace(old, new)
    open('/home/matthiasberner/Schreibtisch/kodi/xstreamDownloader/mini_app.py', 'w').write(c)
    print("Patched OK")
else:
    print("Already patched or pattern not found")
    # Show what's there
    idx = c.find("url = request.args.get('url'")
    if idx >= 0:
        print("Current code around line:")
        print(c[idx:idx+200])
