Spaces:
Sleeping

Neon-AI commited on
Commit
da5cd65
·
verified ·
1 Parent(s): 3cedb1e

Update src/streamlit_app.py

Browse files
Files changed (1) hide show
  1. src/streamlit_app.py +39 -27
src/streamlit_app.py CHANGED
@@ -8,43 +8,55 @@ st.title("HiAnime Episode Screenshot")
8
 
9
  @st.cache_data(show_spinner=True)
10
  def get_screenshot():
11
- """
12
- Runs Playwright headless in a clean context, navigates to the anime episode,
13
- and returns the screenshot as bytes.
14
- """
15
  async def screenshot_async():
16
  async with async_playwright() as p:
17
- browser = await p.chromium.launch(headless=True, args=["--no-sandbox"])
18
-
19
- # Use a clean context (like incognito)
20
- context = await browser.new_context()
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
21
  page = await context.new_page()
22
-
23
- try:
24
- await page.goto(
25
- "https://hianime.to/watch/naruto-677?ep=12352",
26
- wait_until="domcontentloaded",
27
- timeout=60000
28
- )
29
- except Exception as e:
30
- st.warning(f"Goto issue: {e}")
31
-
32
- # Give time for any JS redirect to finish
33
- await page.wait_for_timeout(5000)
34
-
35
- bytes_img = await page.screenshot(full_page=True)
36
-
37
  await context.close()
38
  await browser.close()
39
- return bytes_img
 
40
 
41
  return asyncio.run(screenshot_async())
42
 
 
43
  st.write("Fetching screenshot...")
44
- screenshot_bytes = get_screenshot()
45
 
46
- if screenshot_bytes:
47
- st.image(BytesIO(screenshot_bytes), caption="HiAnime Screenshot", use_column_width=True)
48
  st.success("Screenshot loaded!")
49
  else:
50
  st.error("Failed to capture screenshot.")
 
8
 
9
  @st.cache_data(show_spinner=True)
10
  def get_screenshot():
11
+
 
 
 
12
  async def screenshot_async():
13
  async with async_playwright() as p:
14
+ browser = await p.chromium.launch(
15
+ headless=True,
16
+ args=["--no-sandbox", "--disable-blink-features=AutomationControlled"]
17
+ )
18
+
19
+ context = await browser.new_context(
20
+ user_agent=(
21
+ "Mozilla/5.0 (Windows NT 10.0; Win64; x64) "
22
+ "AppleWebKit/537.36 (KHTML, like Gecko) "
23
+ "Chrome/121.0.0.0 Safari/537.36"
24
+ ),
25
+ viewport={"width": 1280, "height": 800},
26
+ locale="en-US",
27
+ timezone_id="Africa/Lagos",
28
+ extra_http_headers={
29
+ "Accept-Language": "en-US,en;q=0.9",
30
+ "Referer": "https://hianime.to/",
31
+ }
32
+ )
33
+
34
  page = await context.new_page()
35
+
36
+ await page.goto(
37
+ "https://hianime.to/watch/naruto-677?ep=12352",
38
+ wait_until="domcontentloaded",
39
+ timeout=60000
40
+ )
41
+
42
+ # Let JS redirect + player init finish
43
+ await page.wait_for_timeout(8000)
44
+
45
+ screenshot = await page.screenshot(full_page=True)
46
+
 
 
 
47
  await context.close()
48
  await browser.close()
49
+
50
+ return screenshot
51
 
52
  return asyncio.run(screenshot_async())
53
 
54
+
55
  st.write("Fetching screenshot...")
56
+ img = get_screenshot()
57
 
58
+ if img:
59
+ st.image(BytesIO(img), caption="HiAnime Screenshot", use_column_width=True)
60
  st.success("Screenshot loaded!")
61
  else:
62
  st.error("Failed to capture screenshot.")