Spaces:
Sleeping
Sleeping
Update src/streamlit_app.py
Browse files- src/streamlit_app.py +59 -38
src/streamlit_app.py
CHANGED
|
@@ -1,41 +1,62 @@
|
|
|
|
|
| 1 |
from playwright.async_api import async_playwright
|
| 2 |
import asyncio
|
|
|
|
| 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 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import streamlit as st
|
| 2 |
from playwright.async_api import async_playwright
|
| 3 |
import asyncio
|
| 4 |
+
from io import BytesIO
|
| 5 |
|
| 6 |
+
st.set_page_config(page_title="HiAnime Screenshot", layout="wide")
|
| 7 |
+
st.title("HiAnime Episode 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.")
|