-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathchecker.py
More file actions
525 lines (432 loc) · 18.6 KB
/
Copy pathchecker.py
File metadata and controls
525 lines (432 loc) · 18.6 KB
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
"""
checker.py — Movie Scene Checker backend
Flow:
1. JS panel triggers RunPluginTask via Stash GraphQL mutation
2. Stash calls this script with JSON on stdin containing group_id
3. Script fetches Group data from Stash GraphQL
4. Script scrapes ADVE server-side (bypasses CORS / age gate)
5. Script writes result JSON to a well-known file in the plugin dir
6. JS panel polls for that result file via a plain GET request
"""
import sys
import json
import os
import re
import time
import hashlib
import requests
from typing import Optional
from stashapi.stashapp import StashInterface
try:
from bs4 import BeautifulSoup
except ImportError:
print(json.dumps({"error": "beautifulsoup4 is not installed. Run: pip install beautifulsoup4 requests"}))
sys.exit(1)
# ─────────────────────────────────────────────
# LOGGING
# Stash reads log level from a prefix on each stderr line:
# 2TRACE: 2DEBUG: 2INFO: 2WARNING: 2ERROR: 2PROGRESS:nn.n
# ─────────────────────────────────────────────
class log:
# Stash plugin log protocol (from stash/pkg/plugin/common/log):
# Each line on stderr: SOH () + level letter + STX () + message
# Level letters: t=trace, d=debug, i=info, w=warning, e=error, p=progress
TRACE = "t"
DEBUG = "d"
INFO = "i"
WARNING = "w"
ERROR = "e"
PROGRESS = "p"
@staticmethod
def _emit(level: str, msg: str):
print(f"{level}{msg}", file=sys.stderr, flush=True)
@staticmethod
def trace(msg): log._emit(log.TRACE, msg)
@staticmethod
def debug(msg): log._emit(log.DEBUG, msg)
@staticmethod
def info(msg): log._emit(log.INFO, msg)
@staticmethod
def warning(msg): log._emit(log.WARNING, msg)
@staticmethod
def error(msg): log._emit(log.ERROR, msg)
@staticmethod
def progress(pct: float):
log._emit(log.PROGRESS, f"{pct:.3f}")
# ─────────────────────────────────────────────
# CONFIG
# ─────────────────────────────────────────────
PLUGIN_DIR = os.path.dirname(os.path.abspath(__file__))
CACHE_DIR = os.path.join(PLUGIN_DIR, ".cache")
RESULT_DIR = os.path.join(PLUGIN_DIR, "results")
CACHE_TTL = 60 * 60 * 24 # 24 hours
# Log resolved paths to stderr so they appear in Stash task logs
log.debug(f"PLUGIN_DIR={PLUGIN_DIR}")
log.debug(f"RESULT_DIR={RESULT_DIR}")
# Load config.json — all user-configurable values live here.
# Environment variables take precedence so Docker/systemd overrides still work.
_config_path = os.path.join(PLUGIN_DIR, "config.json")
_config: dict = {}
if os.path.exists(_config_path):
try:
with open(_config_path) as _f:
_config = json.load(_f)
except Exception as _e:
log.warning(f"Could not read config.json: {_e}")
ADVE_SESSION_COOKIE = os.getenv("ADVE_SESSION_COOKIE") or _config.get("adve_session_cookie", "")
HEADERS = {
"User-Agent": (
"Mozilla/5.0 (Windows NT 10.0; Win64; x64) "
"AppleWebKit/537.36 (KHTML, like Gecko) "
"Chrome/122.0.0.0 Safari/537.36"
),
"Referer": "https://www.adultdvdempire.com/",
}
os.makedirs(CACHE_DIR, exist_ok=True)
os.makedirs(RESULT_DIR, exist_ok=True)
# ─────────────────────────────────────────────
# STASH GRAPHQL
# ─────────────────────────────────────────────
def get_group(stash: StashInterface, group_id: str) -> dict:
query = """
query FindGroup($id: ID!) {
findGroup(id: $id) {
id
name
urls
scenes {
id
title
files { duration }
performers { name }
}
}
}
"""
result = stash.call_GQL(query, {"id": group_id})
return result.get("findGroup")
def get_all_groups(stash: StashInterface) -> list:
query = """
query AllGroups($filter: FindFilterType, $group_filter: GroupFilterType) {
findGroups(filter: $filter, group_filter: $group_filter) {
groups { id name urls }
}
}
"""
# per_page: -1 returns all records; group_filter restricts to groups
# whose URL list contains an adultdvdempire.com URL.
result = stash.call_GQL(query, {
"filter": {"per_page": -1},
"group_filter": {
"url": {
"value": "adultdvdempire.com",
"modifier": "INCLUDES",
}
},
})
return result.get("findGroups", {}).get("groups", [])
# ─────────────────────────────────────────────
# URL DETECTION
# ─────────────────────────────────────────────
ADVE_PATTERN = re.compile(
r"https?://(?:www\.)?adultdvdempire\.com/[\w\-]+/?",
re.IGNORECASE,
)
def find_adve_url(urls: list) -> Optional[str]:
for url in (urls or []):
if ADVE_PATTERN.match(url.strip()):
return url.strip()
return None
# ─────────────────────────────────────────────
# CACHING
# ─────────────────────────────────────────────
def _cache_path(url: str) -> str:
key = hashlib.md5(url.encode()).hexdigest()
return os.path.join(CACHE_DIR, f"{key}.json")
def cache_load(url: str) -> Optional[dict]:
path = _cache_path(url)
if not os.path.exists(path):
return None
if time.time() - os.path.getmtime(path) > CACHE_TTL:
return None
with open(path) as f:
return json.load(f)
def cache_save(url: str, data: dict):
with open(_cache_path(url), "w") as f:
json.dump(data, f)
# ─────────────────────────────────────────────
# RESULT FILE (polled by JS)
# ─────────────────────────────────────────────
def result_path(group_id: str) -> str:
return os.path.join(RESULT_DIR, f"group_{group_id}.json")
def write_result(group_id: str, data: dict):
"""Write result JSON where Stash can serve it as a static file."""
path = result_path(group_id)
os.makedirs(os.path.dirname(path), exist_ok=True)
with open(path, "w") as f:
json.dump(data, f)
log.info(f"Result written to: {path}")
# ─────────────────────────────────────────────
# ADVE SCRAPER
# ─────────────────────────────────────────────
def scrape_adve(url: str, force: bool = False) -> dict:
if not force:
cached = cache_load(url)
if cached:
return cached
if not ADVE_SESSION_COOKIE:
raise RuntimeError(
"ADVE_SESSION_COOKIE is not set. "
"Add your ADVE browser session cookie to the plugin settings."
)
session = requests.Session()
session.headers.update(HEADERS)
# Inject the full cookie string from a real logged-in browser session
for cookie_part in ADVE_SESSION_COOKIE.split(";"):
cookie_part = cookie_part.strip()
if "=" in cookie_part:
name, _, value = cookie_part.partition("=")
session.cookies.set(name.strip(), value.strip(), domain="www.adultdvdempire.com")
resp = session.get(url, timeout=20, allow_redirects=True)
resp.raise_for_status()
# Check if we landed on any verification/gate page
if any(x in resp.url for x in ["AgeVerification", "AgeConfirmation", "age_verification", "loginpage"]):
raise RuntimeError(
f"ADVE redirected to a gate page: {resp.url}. "
"Your session cookie may be expired. Please update ADVE_SESSION_COOKIE in plugin settings."
)
# Save raw HTML for selector debugging
debug_path = os.path.join(RESULT_DIR, "debug_last_page.html")
try:
with open(debug_path, "w", encoding="utf-8") as f:
f.write(resp.text)
log.debug(f"Raw HTML saved to: {debug_path}")
except Exception as e:
log.warning(f"Could not save debug HTML: {e}")
soup = BeautifulSoup(resp.text, "html.parser")
log.debug(f"Page title: {soup.title.string if soup.title else None}")
log.debug(f"Final URL after redirects: {resp.url}")
# Movie title
title_tag = soup.select_one("h1")
movie_title = title_tag.get_text(strip=True) if title_tag else "Unknown"
# ADVE scene structure:
# Each scene has an anchor: <a name="scene_XXXXXX" class="anchor">
# Followed by a .row div containing:
# h3 > a[label="Scene Title"] — scene title
# span (sibling of h3 parent col) — duration e.g. "29 min"
# a[href*="pornstars"] — performer name
# img[data-bgsrc] — thumbnail (NOT data-src or src)
scene_anchors = soup.find_all("a", attrs={"name": re.compile(r"^scene_\d+$")})
log.info(f"Found {len(scene_anchors)} scene anchors")
scenes = []
for idx, anchor in enumerate(scene_anchors, start=1):
# The scene content lives in the next sibling .row div
row = anchor.find_next_sibling("div", class_="row")
if not row:
continue
# Title: <a label="Scene Title">
title_el = row.find("a", attrs={"label": "Scene Title"})
scene_title = title_el.get_text(strip=True) if title_el else f"Scene {idx}"
# Duration: <span> sibling of the h3 parent col-sm-6
duration = ""
h3 = row.find("h3")
if h3:
# span is a sibling of h3 inside the same col div
dur_span = h3.find_next_sibling("span")
if dur_span:
duration = dur_span.get_text(strip=True)
# Performers: links to pornstar pages in this row
perf_els = row.find_all("a", href=re.compile(r"/.*-pornstars\.html"))
performers = [p.get_text(strip=True) for p in perf_els]
# Thumbnail: first img with data-bgsrc in the screenshot grid
# (the next .row sibling after the title row)
thumbnail = ""
next_row = row.find_next_sibling("div", class_="row")
if next_row:
img_el = next_row.find("img", attrs={"data-bgsrc": True})
if img_el:
thumbnail = img_el["data-bgsrc"]
if thumbnail.startswith("//"):
thumbnail = "https:" + thumbnail
scenes.append({
"index": idx,
"title": scene_title,
"thumbnail": thumbnail,
"performers": performers,
"duration": duration,
})
result = {
"movie_title": movie_title,
"total_scenes": len(scenes),
"scenes": scenes,
}
cache_save(url, result)
return result
# ─────────────────────────────────────────────
# COMPARISON LOGIC
# ─────────────────────────────────────────────
def normalize(text: str) -> str:
return re.sub(r"[^a-z0-9 ]", "", text.lower()).strip()
def performer_set(performers: list) -> set:
"""Return a normalized set of performer names from a list of name strings."""
return {normalize(p) for p in performers if p}
def parse_duration_secs(dur: str) -> Optional[float]:
"""Parse ADVE duration string to seconds. Handles '29 min' and '29:30'."""
if not dur:
return None
min_match = re.match(r"(\d+)\s*min", dur)
colon_match = re.match(r"(\d+):(\d+)", dur)
if min_match:
return int(min_match.group(1)) * 60
if colon_match:
return int(colon_match.group(1)) * 60 + int(colon_match.group(2))
return None
def match_scene(adve_scene: dict, stash_scenes: list) -> Optional[dict]:
"""
Match an ADVE scene to a Stash scene.
Primary strategy: performer overlap AND duration within tolerance.
- All ADVE performers must be present in the Stash scene.
- Duration must be within ±90 seconds (ADVE only shows whole minutes).
- If ADVE has no duration, performer match alone is accepted.
- If ADVE has no performers, duration match alone is accepted.
Fallback: exact title match (skips generic titles like Scene 1).
"""
adve_performers = performer_set(adve_scene.get("performers") or [])
adve_secs = parse_duration_secs(adve_scene.get("duration", ""))
for s in stash_scenes:
stash_performers = performer_set(
[p.get("name", "") for p in (s.get("performers") or [])]
)
stash_secs_list = [
float(f.get("duration") or 0)
for f in (s.get("files") or [])
if f.get("duration")
]
stash_secs = stash_secs_list[0] if stash_secs_list else None
performers_match = bool(adve_performers) and adve_performers <= stash_performers
# ADVE durations are rounded whole minutes, so tolerance must cover
# up to 59 sec of rounding plus any encode-length variance.
# Scale up slightly for scenes with many performers (longer intros etc).
tolerance = 120 + (len(adve_performers) * 10)
duration_match = (
adve_secs is not None
and stash_secs is not None
and abs(stash_secs - adve_secs) <= tolerance
)
if adve_performers and adve_secs is not None:
# Both available: require both to match
if performers_match and duration_match:
return s
elif adve_performers:
# No duration on ADVE side: performer match alone
if performers_match:
return s
elif adve_secs is not None:
# No performers on ADVE side: duration match alone
if duration_match:
return s
# Fallback: title match (skip generic "Scene N" titles)
adve_norm = normalize(adve_scene.get("title") or "")
if adve_norm and not re.match(r"^scene \d+$", adve_norm):
for s in stash_scenes:
if normalize(s.get("title") or "") == adve_norm:
return s
return None
def build_comparison(stash: StashInterface, group_id: str) -> dict:
group = get_group(stash, group_id)
if not group:
return {"error": f"Group {group_id} not found in Stash."}
adve_url = find_adve_url(group.get("urls", []))
if not adve_url:
return {
"error": "no_adve_url",
"group_name": group["name"],
"message": (
"No AdultDVDEmpire URL found in this Group's URL list. "
"Add the ADVE movie URL to the Group to enable scene checking."
),
}
try:
adve_data = scrape_adve(adve_url)
except Exception as e:
return {"error": f"Failed to scrape ADVE: {e}"}
stash_scenes = group.get("scenes", [])
results = []
for adve_scene in adve_data["scenes"]:
matched = match_scene(adve_scene, stash_scenes)
results.append({
"adve": adve_scene,
"stash_scene": {"id": matched["id"], "title": matched["title"]} if matched else None,
"in_library": matched is not None,
})
return {
"group_id": group_id,
"group_name": group["name"],
"adve_url": adve_url,
"movie_title": adve_data["movie_title"],
"total_adve_scenes": adve_data["total_scenes"],
"total_stash_scenes": len(stash_scenes),
"scenes": results,
}
# ─────────────────────────────────────────────
# PLUGIN ENTRY POINT
# ─────────────────────────────────────────────
def main():
try:
_run()
except Exception as e:
import traceback
print(json.dumps({"error": str(e), "trace": traceback.format_exc()}))
sys.exit(1)
def _run():
json_input = json.loads(sys.stdin.read())
if ADVE_SESSION_COOKIE:
log.info("Session cookie loaded from config.json")
else:
log.warning("No session cookie found in config.json or environment")
server_connection = json_input["server_connection"]
stash = StashInterface(server_connection)
args = json_input.get("args", {})
mode = args.get("mode", "check_group")
if mode == "check_group":
group_id = args.get("group_id")
if not group_id:
result = {"error": "group_id is required"}
else:
result = build_comparison(stash, group_id)
# Write to result file so JS can poll for it
write_result(group_id, result)
print(json.dumps(result))
elif mode == "scrape_all":
groups = get_all_groups(stash)
total = len(groups)
summary = []
log.info(f"Scraping {total} groups with ADVE URLs...")
log.progress(0.0)
start_time = time.time()
for idx, g in enumerate(groups, start=1):
if find_adve_url(g.get("urls", [])):
log.info(f"[{idx}/{total}] {g['name']}")
result = build_comparison(stash, g["id"])
write_result(g["id"], result)
summary.append({
"group_id": g["id"],
"group_name": g["name"],
"status": "error" if "error" in result else "ok",
"total_adve": result.get("total_adve_scenes", 0),
"total_stash": result.get("total_stash_scenes", 0),
})
# Update progress bar and ETA after every group
elapsed = time.time() - start_time
pct = idx / total
eta = int((elapsed / idx) * (total - idx)) if idx > 0 else 0
log.progress(pct)
log.info(f"Progress: {idx}/{total} — ETA: {eta}s")
log.progress(1.0)
log.info(f"Done. Scraped {len(summary)} groups.")
print(json.dumps({"scraped": len(summary), "groups": summary}))
else:
print(json.dumps({"error": f"Unknown mode: {mode}"}))
sys.exit(1)
if __name__ == "__main__":
main()