q6 commited on
Commit
b8b8795
·
1 Parent(s): 536bef4
Files changed (1) hide show
  1. backend/app.py +9 -33
backend/app.py CHANGED
@@ -1,5 +1,4 @@
1
  import asyncio
2
- import base64
3
  import io
4
  import json
5
  import os
@@ -18,28 +17,10 @@ TURSO_DB_URL = os.getenv("TURSO_DB_URL", "").strip()
18
  TURSO_AUTH_TOKEN = os.getenv("TURSO_AUTH_TOKEN_WRITE", "").strip()
19
  DISCORD_WEBHOOK_URL = os.getenv("DISCORD_WEBHOOK_URL", "").strip()
20
  PHPSESSID = os.getenv("PHPSESSID", "")
21
- PASS = os.getenv("PASS", "").encode()
22
 
23
  IMG_BASE = "https://i.pximg.net/img-original/img/"
24
 
25
 
26
- def encrypt_query(text):
27
- if not PASS:
28
- return text
29
- raw = bytes(b ^ PASS[i % len(PASS)] for i, b in enumerate(text.encode()))
30
- return base64.b64encode(raw).decode()
31
-
32
-
33
- def decrypt_query(text):
34
- if not PASS:
35
- return text
36
- try:
37
- raw = base64.b64decode(text)
38
- return bytes(b ^ PASS[i % len(PASS)] for i, b in enumerate(raw)).decode()
39
- except Exception:
40
- return text
41
-
42
-
43
  PIXIV_HEADERS = {
44
  "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:126.0) Gecko/20100101 Firefox/126.0",
45
  "referer": "https://www.pixiv.net/",
@@ -117,7 +98,7 @@ async def init_db():
117
  await turso_execute(
118
  [
119
  {
120
- "sql": "CREATE TABLE IF NOT EXISTS searches (id TEXT PRIMARY KEY, query TEXT, post_ids TEXT, created_at INTEGER)"
121
  },
122
  {
123
  "sql": "CREATE TABLE IF NOT EXISTS scans (post_id TEXT PRIMARY KEY, url TEXT, exif_type INTEGER)"
@@ -408,12 +389,11 @@ async def bg_search_task(search_id, url, pages, mode, phpsessid):
408
  }
409
  await discord_notify(f"`{search_id}` started")
410
  try:
411
- post_ids, keywords = await pixiv_search(url, pages, mode, phpsessid)
412
  stmt = {
413
- "sql": "INSERT OR REPLACE INTO searches (id, query, post_ids, created_at) VALUES (?, ?, ?, ?)",
414
  "args": [
415
  {"type": "text", "value": search_id},
416
- {"type": "text", "value": encrypt_query(keywords)},
417
  {"type": "text", "value": json.dumps(post_ids)},
418
  {"type": "integer", "value": str(int(time.time()))},
419
  ],
@@ -440,12 +420,10 @@ async def bg_user_task(search_id, user_ids, phpsessid):
440
  for r in results:
441
  all_post_ids.extend(r["post_ids"])
442
  all_post_ids = list(dict.fromkeys(all_post_ids))
443
- query_label = ",".join(str(u) for u in user_ids)
444
  stmt = {
445
- "sql": "INSERT OR REPLACE INTO searches (id, query, post_ids, created_at) VALUES (?, ?, ?, ?)",
446
  "args": [
447
  {"type": "text", "value": search_id},
448
- {"type": "text", "value": encrypt_query(f"users:{query_label}")},
449
  {"type": "text", "value": json.dumps(all_post_ids)},
450
  {"type": "integer", "value": str(int(time.time()))},
451
  ],
@@ -490,12 +468,11 @@ async def bg_search_and_scan_task(search_id, url, pages, mode, phpsessid):
490
  }
491
  await discord_notify(f"`{search_id}` search+scan started")
492
  try:
493
- post_ids, keywords = await pixiv_search(url, pages, mode, phpsessid)
494
  stmt = {
495
- "sql": "INSERT OR REPLACE INTO searches (id, query, post_ids, created_at) VALUES (?, ?, ?, ?)",
496
  "args": [
497
  {"type": "text", "value": search_id},
498
- {"type": "text", "value": encrypt_query(keywords)},
499
  {"type": "text", "value": json.dumps(post_ids)},
500
  {"type": "integer", "value": str(int(time.time()))},
501
  ],
@@ -628,7 +605,7 @@ async def get_search(search_id: str):
628
  resp = await turso_execute(
629
  [
630
  {
631
- "sql": "SELECT id, query, post_ids, created_at FROM searches WHERE id = ?",
632
  "args": [{"type": "text", "value": search_id}],
633
  }
634
  ]
@@ -640,13 +617,12 @@ async def get_search(search_id: str):
640
  if not rows:
641
  return {"error": "not found"}
642
  row = rows[0]
643
- post_ids = json.loads(row[2].get("value", "[]"))
644
  scanned = await get_scanned_post_ids(post_ids)
645
  return {
646
  "id": row[0].get("value"),
647
- "query": decrypt_query(row[1].get("value", "")),
648
  "post_ids": post_ids,
649
- "created_at": row[3].get("value"),
650
  "scanned": scanned,
651
  }
652
 
 
1
  import asyncio
 
2
  import io
3
  import json
4
  import os
 
17
  TURSO_AUTH_TOKEN = os.getenv("TURSO_AUTH_TOKEN_WRITE", "").strip()
18
  DISCORD_WEBHOOK_URL = os.getenv("DISCORD_WEBHOOK_URL", "").strip()
19
  PHPSESSID = os.getenv("PHPSESSID", "")
 
20
 
21
  IMG_BASE = "https://i.pximg.net/img-original/img/"
22
 
23
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
24
  PIXIV_HEADERS = {
25
  "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:126.0) Gecko/20100101 Firefox/126.0",
26
  "referer": "https://www.pixiv.net/",
 
98
  await turso_execute(
99
  [
100
  {
101
+ "sql": "CREATE TABLE IF NOT EXISTS searches (id TEXT PRIMARY KEY, post_ids TEXT, created_at INTEGER)"
102
  },
103
  {
104
  "sql": "CREATE TABLE IF NOT EXISTS scans (post_id TEXT PRIMARY KEY, url TEXT, exif_type INTEGER)"
 
389
  }
390
  await discord_notify(f"`{search_id}` started")
391
  try:
392
+ post_ids, _ = await pixiv_search(url, pages, mode, phpsessid)
393
  stmt = {
394
+ "sql": "INSERT OR REPLACE INTO searches (id, post_ids, created_at) VALUES (?, ?, ?)",
395
  "args": [
396
  {"type": "text", "value": search_id},
 
397
  {"type": "text", "value": json.dumps(post_ids)},
398
  {"type": "integer", "value": str(int(time.time()))},
399
  ],
 
420
  for r in results:
421
  all_post_ids.extend(r["post_ids"])
422
  all_post_ids = list(dict.fromkeys(all_post_ids))
 
423
  stmt = {
424
+ "sql": "INSERT OR REPLACE INTO searches (id, post_ids, created_at) VALUES (?, ?, ?)",
425
  "args": [
426
  {"type": "text", "value": search_id},
 
427
  {"type": "text", "value": json.dumps(all_post_ids)},
428
  {"type": "integer", "value": str(int(time.time()))},
429
  ],
 
468
  }
469
  await discord_notify(f"`{search_id}` search+scan started")
470
  try:
471
+ post_ids, _ = await pixiv_search(url, pages, mode, phpsessid)
472
  stmt = {
473
+ "sql": "INSERT OR REPLACE INTO searches (id, post_ids, created_at) VALUES (?, ?, ?)",
474
  "args": [
475
  {"type": "text", "value": search_id},
 
476
  {"type": "text", "value": json.dumps(post_ids)},
477
  {"type": "integer", "value": str(int(time.time()))},
478
  ],
 
605
  resp = await turso_execute(
606
  [
607
  {
608
+ "sql": "SELECT id, post_ids, created_at FROM searches WHERE id = ?",
609
  "args": [{"type": "text", "value": search_id}],
610
  }
611
  ]
 
617
  if not rows:
618
  return {"error": "not found"}
619
  row = rows[0]
620
+ post_ids = json.loads(row[1].get("value", "[]"))
621
  scanned = await get_scanned_post_ids(post_ids)
622
  return {
623
  "id": row[0].get("value"),
 
624
  "post_ids": post_ids,
625
+ "created_at": row[2].get("value"),
626
  "scanned": scanned,
627
  }
628