File size: 19,882 Bytes
cd1d350
4f89350
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
cd1d350
4f89350
 
 
 
 
 
 
 
 
 
cd1d350
4f89350
 
 
 
 
 
 
 
 
cd1d350
4f89350
 
 
 
 
 
 
 
 
cd1d350
4f89350
 
 
 
 
 
 
 
 
 
 
 
 
cd1d350
4f89350
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
beaf11d
 
 
4f89350
beaf11d
4f89350
 
beaf11d
 
 
 
 
 
 
4f89350
 
beaf11d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
cd1d350
beaf11d
 
60297b5
beaf11d
 
 
4f89350
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
from flask import Flask, render_template, request, jsonify
import os, re, json, sqlite3, logging

app = Flask(__name__)

# โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€ 1. CONFIGURATION โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€
# Use absolute paths in the persistent directory for Hugging Face
BASE_DIR = os.path.dirname(os.path.abspath(__file__))
DB_FILE = os.path.join(BASE_DIR, "favorite_sites.json")  # JSON file for backward compatibility
SQLITE_DB = os.path.join(BASE_DIR, "favorite_sites.db")  # SQLite database for persistence

# Setup logging
logging.basicConfig(level=logging.INFO)
logger = logging.getLogger(__name__)

# Domains that commonly block iframes
BLOCKED_DOMAINS = [
    "naver.com", "daum.net", "google.com",
    "facebook.com", "instagram.com", "kakao.com",
    "ycombinator.com"
]

# โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€ 2. CURATED CATEGORIES โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€
CATEGORIES = {
    "Free AI: Productivity": [
        "https://huggingface.co/spaces/ginigen/perflexity-clone",
        "https://huggingface.co/spaces/ginipick/IDEA-DESIGN",
        "https://huggingface.co/spaces/VIDraft/mouse-webgen",
        "https://huggingface.co/spaces/openfree/Vibe-Game",
        "https://huggingface.co/spaces/openfree/Game-Gallery",
        "https://huggingface.co/spaces/aiqtech/Contributors-Leaderboard",
        "https://huggingface.co/spaces/fantaxy/Model-Leaderboard",
        "https://huggingface.co/spaces/fantaxy/Space-Leaderboard",
        "https://huggingface.co/spaces/openfree/Korean-Leaderboard",
    ],
    "Free AI: Multimodal": [
        "https://huggingface.co/spaces/openfree/DreamO-video",
        "https://huggingface.co/spaces/Heartsync/NSFW-Uncensored-photo",
        "https://huggingface.co/spaces/Heartsync/NSFW-Uncensored",
        "https://huggingface.co/spaces/fantaxy/Sound-AI-SFX",
        "https://huggingface.co/spaces/ginigen/SFX-Sound-magic",
        "https://huggingface.co/spaces/ginigen/VoiceClone-TTS",
        "https://huggingface.co/spaces/aiqcamp/MCP-kokoro",
        "https://huggingface.co/spaces/aiqcamp/ENGLISH-Speaking-Scoring",
    ],
    "Free AI: Professional": [
        "https://huggingface.co/spaces/ginigen/blogger",
        "https://huggingface.co/spaces/VIDraft/money-radar",
        "https://huggingface.co/spaces/immunobiotech/drug-discovery",
        "https://huggingface.co/spaces/immunobiotech/Gemini-MICHELIN",
        "https://huggingface.co/spaces/Heartsync/Papers-Leaderboard",
        "https://huggingface.co/spaces/VIDraft/PapersImpact",
        "https://huggingface.co/spaces/ginipick/AgentX-Papers",
        "https://huggingface.co/spaces/openfree/Cycle-Navigator",
    ],
    "Free AI: Image": [
        "https://huggingface.co/spaces/ginigen/interior-design",
        "https://huggingface.co/spaces/ginigen/Workflow-Canvas",
        "https://huggingface.co/spaces/ginigen/Multi-LoRAgen",
        "https://huggingface.co/spaces/ginigen/Every-Text",
        "https://huggingface.co/spaces/ginigen/text3d-r1",
        "https://huggingface.co/spaces/ginipick/FLUXllama",
        "https://huggingface.co/spaces/Heartsync/FLUX-Vision",
        "https://huggingface.co/spaces/ginigen/VisualCloze",
        "https://huggingface.co/spaces/seawolf2357/Ghibli-Multilingual-Text-rendering",
        "https://huggingface.co/spaces/ginigen/Ghibli-Meme-Studio",
        "https://huggingface.co/spaces/VIDraft/Open-Meme-Studio",
        "https://huggingface.co/spaces/ginigen/3D-LLAMA",
    ],
    "Free AI: LLM / VLM": [
        "https://huggingface.co/spaces/VIDraft/Gemma-3-R1984-4B",
        "https://huggingface.co/spaces/VIDraft/Gemma-3-R1984-12B",
        "https://huggingface.co/spaces/ginigen/Mistral-Perflexity",
        "https://huggingface.co/spaces/aiqcamp/gemini-2.5-flash-preview",
        "https://huggingface.co/spaces/openfree/qwen3-30b-a3b-research",
        "https://huggingface.co/spaces/openfree/qwen3-235b-a22b-research",
        "https://huggingface.co/spaces/openfree/Llama-4-Maverick-17B-Research",
    ],
}

# โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€ 3. DATABASE FUNCTIONS โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€
def init_db():
    """Initialize both JSON and SQLite databases"""
    # Log database paths for debugging
    logger.info(f"JSON DB path: {DB_FILE}")
    logger.info(f"SQLite DB path: {SQLITE_DB}")
    
    # Initialize JSON file if it doesn't exist
    if not os.path.exists(DB_FILE):
        try:
            with open(DB_FILE, "w", encoding="utf-8") as f:
                json.dump([], f, ensure_ascii=False)
            logger.info("Created new JSON database file")
        except Exception as e:
            logger.error(f"Error creating JSON file: {e}")
    
    # Initialize SQLite database
    try:
        conn = sqlite3.connect(SQLITE_DB)
        cursor = conn.cursor()
        cursor.execute('''
        CREATE TABLE IF NOT EXISTS urls (
            id INTEGER PRIMARY KEY AUTOINCREMENT,
            url TEXT UNIQUE NOT NULL,
            date_added TIMESTAMP DEFAULT CURRENT_TIMESTAMP
        )
        ''')
        conn.commit()
        logger.info("SQLite database initialized successfully")
        
        # If we have data in JSON but not in SQLite (first run with new SQLite DB),
        # migrate the data from JSON to SQLite
        json_urls = load_json()
        if json_urls:
            logger.info(f"Found {len(json_urls)} URLs in JSON file")
            db_urls = load_db_sqlite()
            new_urls = 0
            for url in json_urls:
                if url not in db_urls:
                    add_url_to_sqlite(url)
                    new_urls += 1
            logger.info(f"Migrated {new_urls} new URLs from JSON to SQLite")
        
        conn.close()
    except Exception as e:
        logger.error(f"Error initializing SQLite database: {e}")
        
    # Create default URLs file if none exists
    if not load_db():
        default_urls = [
            "https://huggingface.co/spaces/ginigen/perflexity-clone",
            "https://huggingface.co/spaces/openfree/Game-Gallery",
            "https://www.google.com"
        ]
        for url in default_urls:
            add_url_to_sqlite(url)
        save_json(default_urls)
        logger.info("Added default URLs to empty database")

def load_json():
    """Load URLs from JSON file (for backward compatibility)"""
    try:
        if os.path.exists(DB_FILE):
            with open(DB_FILE, "r", encoding="utf-8") as f:
                raw = json.load(f)
            return raw if isinstance(raw, list) else []
        return []
    except Exception as e:
        logger.error(f"Error loading JSON file: {e}")
        return []

def save_json(lst):
    """Save URLs to JSON file (for backward compatibility)"""
    try:
        with open(DB_FILE, "w", encoding="utf-8") as f:
            json.dump(lst, f, ensure_ascii=False, indent=2)
        logger.info(f"Saved {len(lst)} URLs to JSON file")
        return True
    except Exception as e:
        logger.error(f"Error saving to JSON file: {e}")
        return False

def load_db_sqlite():
    """Load URLs from SQLite database"""
    try:
        conn = sqlite3.connect(SQLITE_DB)
        cursor = conn.cursor()
        cursor.execute("SELECT url FROM urls ORDER BY date_added DESC")
        urls = [row[0] for row in cursor.fetchall()]
        conn.close()
        logger.info(f"Loaded {len(urls)} URLs from SQLite database")
        return urls
    except Exception as e:
        logger.error(f"Error loading from SQLite database: {e}")
        return []

def add_url_to_sqlite(url):
    """Add a URL to SQLite database"""
    try:
        conn = sqlite3.connect(SQLITE_DB)
        cursor = conn.cursor()
        cursor.execute("INSERT INTO urls (url) VALUES (?)", (url,))
        conn.commit()
        conn.close()
        logger.info(f"Added URL to SQLite: {url}")
        return True
    except sqlite3.IntegrityError:
        # URL already exists
        logger.info(f"URL already exists in SQLite: {url}")
        return False
    except Exception as e:
        logger.error(f"Error adding URL to SQLite: {e}")
        return False

def update_url_in_sqlite(old_url, new_url):
    """Update a URL in SQLite database"""
    try:
        conn = sqlite3.connect(SQLITE_DB)
        cursor = conn.cursor()
        cursor.execute("UPDATE urls SET url = ? WHERE url = ?", (new_url, old_url))
        if cursor.rowcount > 0:
            conn.commit()
            logger.info(f"Updated URL in SQLite: {old_url} -> {new_url}")
            success = True
        else:
            logger.info(f"URL not found for update in SQLite: {old_url}")
            success = False
        conn.close()
        return success
    except sqlite3.IntegrityError:
        # New URL already exists
        logger.error(f"New URL already exists in SQLite: {new_url}")
        return False
    except Exception as e:
        logger.error(f"Error updating URL in SQLite: {e}")
        return False

def delete_url_from_sqlite(url):
    """Delete a URL from SQLite database"""
    try:
        conn = sqlite3.connect(SQLITE_DB)
        cursor = conn.cursor()
        cursor.execute("DELETE FROM urls WHERE url = ?", (url,))
        if cursor.rowcount > 0:
            conn.commit()
            logger.info(f"Deleted URL from SQLite: {url}")
            success = True
        else:
            logger.info(f"URL not found for deletion in SQLite: {url}")
            success = False
        conn.close()
        return success
    except Exception as e:
        logger.error(f"Error deleting URL from SQLite: {e}")
        return False

def load_db():
    """Primary function to load URLs - prioritizes SQLite DB but falls back to JSON"""
    sqlite_urls = load_db_sqlite()
    
    # If SQLite DB is empty, try loading from JSON
    if not sqlite_urls:
        logger.info("SQLite database empty, trying JSON file")
        json_urls = load_json()
        
        # If we found URLs in JSON, migrate them to SQLite
        if json_urls:
            logger.info(f"Migrating {len(json_urls)} URLs from JSON to SQLite")
            for url in json_urls:
                add_url_to_sqlite(url)
            return json_urls
    
    return sqlite_urls

def save_db(lst):
    """Save URLs to both SQLite and JSON"""
    logger.info(f"Saving {len(lst)} URLs to database")
    
    # Clear all URLs from SQLite and add the new list
    try:
        conn = sqlite3.connect(SQLITE_DB)
        cursor = conn.cursor()
        cursor.execute("DELETE FROM urls")
        for url in lst:
            cursor.execute("INSERT INTO urls (url) VALUES (?)", (url,))
        conn.commit()
        conn.close()
        logger.info("Successfully saved to SQLite database")
    except Exception as e:
        logger.error(f"Error saving to SQLite database: {e}")
    
    # Also save to JSON for backward compatibility
    return save_json(lst)

# โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€ 4. URL HELPERS โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€
def direct_url(hf_url):
    m = re.match(r"https?://huggingface\.co/spaces/([^/]+)/([^/?#]+)", hf_url)
    if not m:
        return hf_url
    owner, name = m.groups()
    owner = owner.lower()
    name = name.replace('.', '-').replace('_', '-').lower()
    return f"https://{owner}-{name}.hf.space"

def screenshot_url(url):
    return f"https://image.thum.io/get/fullpage/{url}"

def process_url_for_preview(url):
    """Returns (preview_url, mode)"""
    # Handle blocked domains first
    if any(d for d in BLOCKED_DOMAINS if d in url):
        return screenshot_url(url), "snapshot"
    
    # Special case handling for problematic URLs
    if "vibe-coding-tetris" in url or "World-of-Tank-GAME" in url or "Minesweeper-Game" in url:
        return screenshot_url(url), "snapshot"
    
    # General HF space handling
    try:
        if "huggingface.co/spaces" in url:
            parts = url.rstrip("/").split("/")
            if len(parts) >= 5:
                owner = parts[-2]
                name = parts[-1]
                embed_url = f"https://huggingface.co/spaces/{owner}/{name}/embed"
                return embed_url, "iframe"
    except Exception:
        return screenshot_url(url), "snapshot"
    
    # Default handling
    return url, "iframe"

# โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€ 5. API ROUTES โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€
@app.route('/api/category')
def api_category():
    cat = request.args.get('name', '')
    urls = CATEGORIES.get(cat, [])
    return jsonify([
        {
            "title": url.split('/')[-1],
            "owner": url.split('/')[-2] if '/spaces/' in url else '',
            "iframe": direct_url(url),
            "shot": screenshot_url(url),
            "hf": url
        } for url in urls
    ])

@app.route('/api/favorites')
def api_favorites():
    # Load URLs from SQLite database
    urls = load_db()
    
    page = int(request.args.get('page', 1))
    per_page = int(request.args.get('per_page', 9))
    
    total_pages = max(1, (len(urls) + per_page - 1) // per_page)
    start = (page - 1) * per_page
    end = min(start + per_page, len(urls))
    
    urls_page = urls[start:end]
    
    result = []
    for url in urls_page:
        try:
            preview_url, mode = process_url_for_preview(url)
            result.append({
                "title": url.split('/')[-1],
                "url": url,
                "preview_url": preview_url,
                "mode": mode
            })
        except Exception:
            # Fallback to screenshot mode
            result.append({
                "title": url.split('/')[-1],
                "url": url,
                "preview_url": screenshot_url(url),
                "mode": "snapshot"
            })
    
    return jsonify({
        "items": result,
        "page": page,
        "total_pages": total_pages
    })

@app.route('/api/url/add', methods=['POST'])
def add_url():
    url = request.form.get('url', '').strip()
    if not url:
        return jsonify({"success": False, "message": "URL is required"})
    
    # Check if URL already exists in database
    if not add_url_to_sqlite(url):
        return jsonify({"success": False, "message": "URL already exists"})
    
    # Also update JSON file for backward compatibility
    data = load_json()
    if url not in data:
        data.insert(0, url)
        save_json(data)
    
    return jsonify({"success": True, "message": "URL added successfully"})

@app.route('/api/url/update', methods=['POST'])
def update_url():
    old = request.form.get('old', '')
    new = request.form.get('new', '').strip()
    
    if not new:
        return jsonify({"success": False, "message": "New URL is required"})
    
    # Update in SQLite DB
    if not update_url_in_sqlite(old, new):
        return jsonify({"success": False, "message": "URL not found or new URL already exists"})
    
    # Also update JSON file for backward compatibility
    data = load_json()
    try:
        idx = data.index(old)
        data[idx] = new
        save_json(data)
    except ValueError:
        # If URL not in JSON, add it
        data.append(new)
        save_json(data)
    
    return jsonify({"success": True, "message": "URL updated successfully"})

@app.route('/api/url/delete', methods=['POST'])
def delete_url():
    url = request.form.get('url', '')
    
    # Delete from SQLite DB
    if not delete_url_from_sqlite(url):
        return jsonify({"success": False, "message": "URL not found"})
    
    # Also update JSON file for backward compatibility
    data = load_json()
    try:
        data.remove(url)
        save_json(data)
    except ValueError:
        pass
    
    return jsonify({"success": True, "message": "URL deleted successfully"})

# โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€ 6. MAIN ROUTES โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€
@app.route('/')
def home():
    # Create a simple initial template for debugging
    template_dir = os.path.join(BASE_DIR, 'templates')
    os.makedirs(template_dir, exist_ok=True)
    
    index_html = '''<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <title>AI Favorite Sites</title>
    <style>
        body { font-family: Arial, sans-serif; margin: 0; padding: 20px; }
        h1 { text-align: center; }
    </style>
</head>
<body>
    <h1>๐ŸŒŸ AI Favorite Sites</h1>
    <p style="text-align: center;">
        <a href="https://discord.gg/openfreeai" target="_blank">
            <img src="https://img.shields.io/static/v1?label=Discord&message=Openfree%20AI&color=%230000ff&labelColor=%23800080&logo=discord&logoColor=white&style=for-the-badge" alt="Discord">
        </a>
    </p>
    <div id="content">Loading...</div>

    <script>
        // Simple alert to check if JavaScript is working
        window.onload = function() {
            document.getElementById('content').innerHTML = 'JavaScript is working! Loading content...';
            
            // List available categories
            const cats = {{cats|tojson}};
            let catList = '<ul>';
            cats.forEach(cat => {
                catList += '<li>' + cat + '</li>';
            });
            catList += '</ul>';
            
            document.getElementById('content').innerHTML += '<p>Available categories:</p>' + catList;
        };
    </script>
</body>
</html>'''
    
    with open(os.path.join(template_dir, 'index.html'), 'w', encoding='utf-8') as f:
        f.write(index_html)
    
    # Log for debugging
    logger.info(f"Template written to: {os.path.join(template_dir, 'index.html')}")
    logger.info(f"Categories: {list(CATEGORIES.keys())}")
    
    return render_template('index.html', cats=list(CATEGORIES.keys()))

# Initialize database on startup
init_db()

# Define a function to ensure database consistency
def ensure_db_consistency():
    """Make sure both databases are in sync"""
    try:
        # Get URLs from both sources
        sqlite_urls = load_db_sqlite()
        json_urls = load_json()
        
        # Combine and deduplicate
        all_urls = list(set(sqlite_urls + json_urls))
        
        # If there are differences, update both databases
        if len(all_urls) != len(sqlite_urls) or len(all_urls) != len(json_urls):
            logger.info("Database inconsistency detected, synchronizing...")
            # Save to both databases
            save_db(all_urls)
            
            # Double-check if save was successful
            sqlite_check = load_db_sqlite()
            json_check = load_json()
            
            if len(sqlite_check) != len(all_urls) or len(json_check) != len(all_urls):
                logger.error(f"Database synchronization failed! SQLite: {len(sqlite_check)}, JSON: {len(json_check)}, Expected: {len(all_urls)}")
            else:
                logger.info("Database synchronization successful")
    except Exception as e:
        logger.error(f"Error during database consistency check: {e}")

# For Flask 2.0+ compatibility
@app.before_request
def before_request_func():
    # Use a flag to run this only once
    if not hasattr(app, '_got_first_request'):
        ensure_db_consistency()
        app._got_first_request = True
        
        # Log database status
        logger.info(f"Database status - SQLite: {len(load_db_sqlite())} URLs, JSON: {len(load_json())} URLs")

if __name__ == '__main__':
    app.run(host='0.0.0.0', port=7860)