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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
#!/usr/bin/env python3
"""
Cara Master Index Dataset Explorer CLI Tool
Zero External Dependencies (Runs on standard Python 3.7+ across Windows, macOS, Linux).
"""

import sys
import os
import re
import json
import sqlite3
import argparse
from urllib.parse import urlparse
from collections import Counter

# Dynamic discovery of catalog.db (100% portable & anonymous)
DB_CANDIDATE_PATHS = [
    "catalog.db",
    os.path.join(os.path.dirname(os.path.abspath(__file__)), "catalog.db"),
    "../catalog.db",
]

COMMON_PLACEHOLDERS = [
    "username",
    "<username>",
    "your_username",
    "yourname",
    "your_name",
    "handle",
    "<handle>",
    "artist",
    "<artist>"
]


def find_database() -> str:
    """Auto-detects catalog.db location dynamically."""
    for path in DB_CANDIDATE_PATHS:
        if os.path.exists(path) and os.path.getsize(path) > 1000000:
            return path
    return None


def get_db_connection():
    """Connects to catalog.db with clean error guidance if missing."""
    db_path = find_database()
    if not db_path:
        print("\n" + "=" * 80)
        print("❌ DATABASE NOT FOUND: 'catalog.db'")
        print("=" * 80)
        print("Please make sure 'catalog.db' is located in the same folder as this script!")
        print(f"Current working directory: {os.getcwd()}")
        print("=" * 80 + "\n")
        sys.exit(1)

    try:
        conn = sqlite3.connect(db_path)
        conn.row_factory = sqlite3.Row
        return conn
    except Exception as e:
        print(f"\n❌ Error opening database: {e}\n")
        sys.exit(1)


def sanitize_username(input_str: str) -> str:
    """Cleans artist handle input."""
    if not input_str:
        return ""
    cleaned = input_str.strip().lstrip("<").rstrip(">").strip()
    
    if "cara.app" in cleaned or "http" in cleaned:
        path = urlparse(cleaned).path.strip("/")
        parts = path.split("/")
        if len(parts) > 0 and parts[0] not in ["post", "search", "explore"]:
            cleaned = parts[0]
        elif len(parts) > 1 and parts[0] == "user":
            cleaned = parts[1]

    return cleaned.lstrip("@").strip()


def sanitize_post_id(input_str: str) -> str:
    """Cleans post input and extracts UUID."""
    if not input_str:
        return ""
    cleaned = input_str.strip().lstrip("<").rstrip(">").strip()
    
    uuid_match = re.search(r'[0-9a-fA-F]{8}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{12}', cleaned)
    if uuid_match:
        return uuid_match.group(0).lower()

    if "/" in cleaned:
        cleaned = cleaned.split("/")[-1].split("?")[0]
    return cleaned.strip()


def sanitize_art_query(input_str: str) -> str:
    """Extracts clean image hash / filename from CDN URL."""
    if not input_str:
        return ""
    cleaned = input_str.strip().lstrip("<").rstrip(">").strip()
    if "/" in cleaned:
        cleaned = cleaned.split("/")[-1].split("?")[0]
    return cleaned.strip()


def explore_user(username_raw: str, is_overview: bool = False):
    """
    Explores all posts and artworks by an artist handle.
    - is_overview=True: Shows stats and clean list of posts without image URLs.
    - is_overview=False: Shows full breakdown with every master slide and thumbnail URL.
    """
    username = sanitize_username(username_raw)
    
    if username.lower() in [p.lower() for p in COMMON_PLACEHOLDERS]:
        print("\n" + "=" * 80)
        print(f"😅 I doubt your account is named '{username}' — did you forget to type your own name?")
        print("Example: python tool.py --user mayonnaisejar1446 --overview")
        print("=" * 80 + "\n")
        return

    if not username:
        print("\n⚠️ Please specify an artist handle (e.g. 'python tool.py --user mayonnaisejar1446')\n")
        return

    conn = get_db_connection()
    cur = conn.cursor()

    # 1. Fetch Posts from artworks table
    cur.execute("""
        SELECT post_id, cdn_url as cover_url, created_at 
        FROM artworks 
        WHERE author_slug = ? COLLATE NOCASE
        ORDER BY created_at DESC;
    """, (username,))
    posts = cur.fetchall()

    if not posts:
        cur.execute("SELECT DISTINCT author_slug FROM master_artworks_12m WHERE author_slug = ? COLLATE NOCASE;", (username,))
        found_slug = cur.fetchone()
        if not found_slug:
            print("\n" + "=" * 80)
            print(f"❌ Artist '@{username}' was not found in the database.")
            print("Tip: Double-check the exact handle as it appears in the artist's Cara profile URL.")
            print("=" * 80 + "\n")
            conn.close()
            return

    # 2. Fetch Master Artworks
    cur.execute("""
        SELECT post_id, slide_number, title, cdn_url, width, height, created_at 
        FROM master_artworks_12m 
        WHERE author_slug = ? COLLATE NOCASE
        ORDER BY post_id, slide_number;
    """, (username,))
    artworks = cur.fetchall()

    # 3. Fetch Post Metadata
    cur.execute("""
        SELECT post_id, author_name, title, content, softwares, tags, created_at 
        FROM post_metadata 
        WHERE author_slug = ? COLLATE NOCASE;
    """, (username,))
    meta_rows = {r["post_id"]: r for r in cur.fetchall()}

    post_artworks = {}
    for art in artworks:
        post_artworks.setdefault(art["post_id"], []).append(art)

    # Aggregate stats
    display_name = None
    all_softwares = []
    all_tags = []
    all_dates = []

    for pid, meta in meta_rows.items():
        if meta["author_name"] and not display_name:
            display_name = meta["author_name"]
        if meta["created_at"]:
            all_dates.append(meta["created_at"][:10])
        try:
            sw = json.loads(meta["softwares"])
            if isinstance(sw, list):
                all_softwares.extend(sw)
        except Exception:
            pass
        try:
            tg = json.loads(meta["tags"])
            if isinstance(tg, list):
                all_tags.extend(tg)
        except Exception:
            pass

    top_software = [s for s, _ in Counter(all_softwares).most_common(5)]
    top_tags = [t for t, _ in Counter(all_tags).most_common(8)]
    date_range = f"{min(all_dates)} to {max(all_dates)}" if all_dates else "N/A"

    name_header = f"@{username}" + (f" ({display_name})" if display_name else "")

    print("\n" + "=" * 80)
    print(f"👤 ARTIST SUMMARY: {name_header}")
    print("=" * 80)
    print(f"📊 Total Posts (Cover Thumbnails): {len(posts):,}")
    print(f"🖼️ Total Master Artworks (Full-Res): {len(artworks):,}")
    if top_software:
        print(f"💻 Primary Software:              {', '.join(top_software)}")
    if top_tags:
        print(f"🏷️ Top Tags:                      {', '.join(top_tags)}")
    if all_dates:
        print(f"📅 Activity Span:                 {date_range}")
    print(f"🔗 Profile Link:                  https://cara.app/{username}")
    print("=" * 80)

    if is_overview:
        print(f"📋 POSTS OVERVIEW ({len(posts)} posts): [Tip: Omit --overview to view all image links]")
        print("-" * 80)
        for idx, p in enumerate(posts, 1):
            pid = p["post_id"]
            arts = post_artworks.get(pid, [])
            meta = meta_rows.get(pid)

            title = "Untitled"
            if meta and meta["title"] and meta["title"].strip():
                title = meta["title"].strip()
            elif arts and arts[0]["title"] and arts[0]["title"].strip():
                title = arts[0]["title"].strip()

            date_str = p["created_at"][:10] if p["created_at"] else "Unknown"
            slides_info = f"{len(arts)} master slide(s)" if arts else "1 cover image"

            print(f"[{idx}] 📌 {title} ({date_str}) | {slides_info}")
            print(f"    🔗 https://cara.app/post/{pid}")

    else:
        print("📋 DETAILED POSTS & ARTWORK LINKS:")
        print("-" * 80)
        for idx, p in enumerate(posts, 1):
            pid = p["post_id"]
            arts = post_artworks.get(pid, [])
            meta = meta_rows.get(pid)

            title = "Untitled"
            if meta and meta["title"] and meta["title"].strip():
                title = meta["title"].strip()
            elif arts and arts[0]["title"] and arts[0]["title"].strip():
                title = arts[0]["title"].strip()

            date_str = p["created_at"][:10] if p["created_at"] else "Unknown"

            print(f"\n[{idx}] 📌 {title} ({date_str})")
            print(f"    🔗 Post:      https://cara.app/post/{pid}")
            if p["cover_url"]:
                print(f"    🖼️ Thumbnail: {p['cover_url']}")

            if arts:
                for a in arts:
                    res = f"[{a['width']}x{a['height']}]" if a['width'] and a['height'] else "[Full-Res]"
                    print(f"    • Slide {a['slide_number']} {res}: {a['cdn_url']}")

    print("\n" + "=" * 80 + "\n")
    conn.close()


def explore_post(post_raw: str):
    """Explores a post by URL or UUID."""
    post_id = sanitize_post_id(post_raw)
    if not post_id or post_id.lower() in ["post url", "<post url>", "post_id", "<post_id>"]:
        print("\n" + "=" * 80)
        print("😅 Did you copy '<Post url>' literally from the README?")
        print("Example: python tool.py --post https://cara.app/post/5aac58cb-c33f-4622-9199-d6531e8f47f8")
        print("=" * 80 + "\n")
        return

    conn = get_db_connection()
    cur = conn.cursor()

    cur.execute("""
        SELECT id, post_id, author_slug, slide_number, title, cdn_url, width, height, created_at 
        FROM master_artworks_12m 
        WHERE post_id = ?
        ORDER BY slide_number;
    """, (post_id,))
    slides = cur.fetchall()

    cur.execute("SELECT cdn_url FROM artworks WHERE post_id = ?;", (post_id,))
    cover_row = cur.fetchone()
    cover_url = cover_row["cdn_url"] if cover_row else None

    if not slides and not cover_url:
        print("\n" + "=" * 80)
        print(f"❌ Post ID '{post_id}' was not found in the master catalog.")
        print("=" * 80 + "\n")
        conn.close()
        return

    cur.execute("SELECT * FROM post_metadata WHERE post_id = ?;", (post_id,))
    meta = cur.fetchone()

    author = slides[0]["author_slug"] if slides else (meta["author_slug"] if meta else "unknown")
    title = (slides[0]["title"] if slides and slides[0]["title"] else None) or (meta["title"] if meta and meta["title"] else "Untitled")

    print("\n" + "=" * 80)
    print(f"📌 POST ID:   {post_id}")
    print(f"👤 ARTIST:    @{author} (https://cara.app/{author})")
    print(f"🎨 TITLE:     {title}")
    print(f"🔗 WEB LINK:  https://cara.app/post/{post_id}")

    if cover_url:
        print(f"🖼️ THUMBNAIL: {cover_url}")

    if meta:
        try:
            tags = json.loads(meta["tags"])
            softwares = json.loads(meta["softwares"])
            if softwares:
                print(f"💻 SOFTWARE:  {softwares}")
            if tags:
                print(f"🏷️ TAGS:      {tags}")
            if meta["content"]:
                print(f"\n📝 POST BODY:\n{meta['content'].strip()}")
        except Exception:
            pass

    if slides:
        print("\n🖼️ MASTER ARTWORKS (" + str(len(slides)) + " slide(s)):")
        print("-" * 80)
        for s in slides:
            res = f"{s['width']}x{s['height']}" if s['width'] and s['height'] else "Full-Res"
            print(f"  • Slide {s['slide_number']} [{res}]: {s['cdn_url']}")
    print("=" * 80 + "\n")
    conn.close()


def explore_artwork(art_raw: str):
    """Reverse traces an artwork CDN link or image filename back to its post and creator."""
    raw_clean = art_raw.strip().lstrip("<").rstrip(">").strip()
    art_hash = sanitize_art_query(raw_clean)

    if not art_hash or art_hash.lower() in ["cdn url", "<cdn url>", "cdn_url", "<cdn_url>"]:
        print("\n" + "=" * 80)
        print("😅 Did you copy '<CDN url>' literally from the README?")
        print("Example: python tool.py --art otawrou-8L8tjCPtnNl1TStnB3Psu-0000-00091.gif")
        print("=" * 80 + "\n")
        return

    conn = get_db_connection()
    cur = conn.cursor()

    # Fast Path 1: Exact URL match
    cur.execute("SELECT post_id, author_slug, slide_number, title, cdn_url, width, height, created_at FROM master_artworks_12m WHERE cdn_url = ?;", (raw_clean,))
    match = cur.fetchone()

    # Fast Path 2: Exact Filename match
    if not match:
        cur.execute("SELECT post_id, author_slug, slide_number, title, cdn_url, width, height, created_at FROM master_artworks_12m WHERE cdn_url LIKE ?;", (f"https://cdn.cara.app/production/posts/%/{art_hash}",))
        match = cur.fetchone()

    # Fallback Path 3: Substring search
    if not match:
        cur.execute("SELECT post_id, author_slug, slide_number, title, cdn_url, width, height, created_at FROM master_artworks_12m WHERE cdn_url LIKE ?;", (f"%{art_hash}%",))
        match = cur.fetchone()

    # Fallback Path 4: Check cover thumbnails in artworks
    if not match:
        cur.execute("SELECT post_id, author_slug, cdn_url, created_at FROM artworks WHERE cdn_url LIKE ?;", (f"%{art_hash}%",))
        cover_match = cur.fetchone()
        if cover_match:
            print("\n" + "=" * 80)
            print("🔍 REVERSE ARTWORK TRACER (Feed Cover Thumbnail)")
            print("=" * 80)
            print(f"🖼️ Thumbnail:  {cover_match['cdn_url']}")
            print(f"📌 Post ID:    {cover_match['post_id']}")
            print(f"👤 Creator:    @{cover_match['author_slug']} (https://cara.app/{cover_match['author_slug']})")
            print(f"🔗 Direct Web: https://cara.app/post/{cover_match['post_id']}")
            print("=" * 80 + "\n")
            conn.close()
            return

    if not match:
        print("\n" + "=" * 80)
        print(f"❌ Artwork image '{art_hash}' was not found in the database.")
        print("=" * 80 + "\n")
        conn.close()
        return

    pid = match["post_id"]
    cur.execute("SELECT * FROM post_metadata WHERE post_id = ?;", (pid,))
    meta = cur.fetchone()

    print("\n" + "=" * 80)
    print("🔍 REVERSE ARTWORK TRACER (Master Full-Res Artwork)")
    print("=" * 80)
    print(f"🖼️ Image Link:  {match['cdn_url']}")
    print(f"📐 Resolution:  {match['width']}x{match['height']} (Slide {match['slide_number']})")
    print(f"📌 Post ID:     {match['post_id']}")
    print(f"👤 Creator:     @{match['author_slug']} (https://cara.app/{match['author_slug']})")
    print(f"🎨 Title:       {match['title'] or 'Untitled'}")
    print(f"📅 Upload Date: {match['created_at']}")
    print(f"🔗 Direct Web:  https://cara.app/post/{match['post_id']}")

    if meta:
        try:
            tags = json.loads(meta["tags"])
            if tags:
                print(f"🏷️ Post Tags:   {tags[:6]}")
        except Exception:
            pass

    print("=" * 80 + "\n")
    conn.close()


def interactive_menu():
    """Interactive CLI menu when run with zero arguments."""
    print("\n" + "=" * 80)
    print("🎨 CARA MASTER INDEX DATASET EXPLORER")
    print("=" * 80)
    print("Choose an option:")
    print("  1. Look up an Artist / Username (Summary Overview)")
    print("  2. Look up an Artist / Username (Full Artwork Links)")
    print("  3. Look up a Post Link / Post UUID")
    print("  4. Reverse Trace an Artwork CDN Image URL")
    print("  5. Exit")
    print("=" * 80)

    choice = input("\nEnter choice (1-5): ").strip()
    if choice == "1":
        user_input = input("Enter username or profile URL: ").strip()
        explore_user(user_input, is_overview=True)
    elif choice == "2":
        user_input = input("Enter username or profile URL: ").strip()
        explore_user(user_input, is_overview=False)
    elif choice == "3":
        post_input = input("Enter post URL or UUID: ").strip()
        explore_post(post_input)
    elif choice == "4":
        art_input = input("Enter CDN URL or image filename: ").strip()
        explore_artwork(art_input)
    elif choice == "5" or choice.lower() in ["q", "exit", "quit"]:
        print("Goodbye!")
        sys.exit(0)
    else:
        print("Invalid choice. Exiting.")


def main():
    parser = argparse.ArgumentParser(description="Cara Master Index Dataset CLI Tool")
    parser.add_argument("--user", help="Query creator handle (e.g. --user alex123)")
    parser.add_argument("--overview", action="store_true", help="Clean summary overview (disables showing image links)")
    parser.add_argument("--post", help="Query post metadata, lore & gallery (e.g. --post https://cara.app/post/...)")
    parser.add_argument("--art", help="Reverse trace an image CDN link back to creator & post")

    if len(sys.argv) == 1:
        interactive_menu()
        return

    args = parser.parse_args()

    if args.user:
        explore_user(args.user, is_overview=args.overview)
    elif args.post:
        explore_post(args.post)
    elif args.art:
        explore_artwork(args.art)
    else:
        interactive_menu()


if __name__ == "__main__":
    main()