anilife update 2022.10.23(01.)

This commit is contained in:
2022-10-24 03:59:23 +09:00
parent 6e9b71f4cc
commit 3102ba50ad

View File

@@ -112,7 +112,7 @@ class LogicAniLife(LogicModuleBase):
default_route_socketio(P, self)
@staticmethod
def get_html(url, referer=None, stream=False, timeout=5):
def get_html(url: str, referer: str = None, stream: bool = False, timeout: int = 5) -> str:
data = ""
try:
print("cloudflare protection bypass ==================")
@@ -143,7 +143,7 @@ class LogicAniLife(LogicModuleBase):
return data
@staticmethod
def get_html_requests(url, referer=None, stream=False, timeout=5):
def get_html_requests(url: str, referer: str = None, stream: str = False, timeout: int = 5) -> str:
data = ""
try:
print("get_html_requests ==================")
@@ -173,14 +173,16 @@ class LogicAniLife(LogicModuleBase):
@staticmethod
async def get_html_playwright(
url, headless=False, referer=None, engine="chrome", stealth=False
):
url: str, headless: bool = False, referer: str = None, engine: str = "chrome", stealth: bool = False
) -> str:
try:
from playwright.sync_api import sync_playwright
from playwright.async_api import async_playwright
from playwright_stealth import stealth_sync, stealth_async
import time
cookie = None
browser_args = [
"--window-size=1300,570",
"--window-position=000,000",
@@ -237,8 +239,8 @@ class LogicAniLife(LogicModuleBase):
"AppleWebKit/537.36 (KHTML, like Gecko) "
"Chrome/69.0.3497.100 Safari/537.36"
)
# from playwright_stealth import stealth_sync
cookie = None
def set_cookie(req):
nonlocal cookie
@@ -246,6 +248,7 @@ class LogicAniLife(LogicModuleBase):
cookie = req.headers["cookie"]
async with async_playwright() as p:
try:
if engine == "chrome":
browser = await p.chromium.launch(
channel="chrome", args=browser_args, headless=headless
@@ -264,7 +267,9 @@ class LogicAniLife(LogicModuleBase):
# user_agent=ua,
# )
LogicAniLife.headers["Referer"] = "https://anilife.live/detail/id/471"
LogicAniLife.headers[
"Referer"
] = "https://anilife.live/detail/id/471"
# print(LogicAniLife.headers)
LogicAniLife.headers["Referer"] = LogicAniLife.episode_url
@@ -273,7 +278,9 @@ class LogicAniLife(LogicModuleBase):
LogicAniLife.headers["Referer"] = referer
logger.debug(f"LogicAniLife.headers::: {LogicAniLife.headers}")
context = await browser.new_context(extra_http_headers=LogicAniLife.headers)
context = await browser.new_context(
extra_http_headers=LogicAniLife.headers
)
await context.add_cookies(LogicAniLife.cookies)
# LogicAniLife.headers["Cookie"] = cookie_value
@@ -316,6 +323,18 @@ class LogicAniLife(LogicModuleBase):
print(f"run at {time.time() - start} sec")
return await page.content()
except Exception as e:
logger.error("Exception:%s", e)
logger.error(traceback.format_exc())
finally:
await browser.close()
except Exception as e:
logger.error("Exception:%s", e)
logger.error(traceback.format_exc())
finally:
# browser.close()
pass
@staticmethod
async def get_vod_url_v1(
@@ -421,7 +440,7 @@ class LogicAniLife(LogicModuleBase):
return await page.content()
@staticmethod
async def get_vod_url(url, headless=False):
async def get_vod_url(url: str, headless: bool = False) -> str:
from playwright.sync_api import sync_playwright
from playwright.async_api import async_playwright
from playwright_stealth import stealth_async
@@ -488,6 +507,7 @@ class LogicAniLife(LogicModuleBase):
# from playwright_stealth import stealth_sync
async with async_playwright() as p:
try:
# browser = await p.chromium.launch(headless=headless, args=browser_args)
browser = await p.chromium.launch(headless=headless, args=browser_args)
@@ -568,30 +588,15 @@ class LogicAniLife(LogicModuleBase):
logger.debug(f"run at {time.time() - start} sec")
# html_content = LogicAniLife.get_html_selenium(
# vod_url, "https://anilife.live"
# )
# html_content = LogicAniLife.get_html_playwright(
# vod_url, False, referer="https://anilife.live"
# )
# html_content = LogicAniLife.get_html(
# vod_url, referer="https://anilife.live"
# )
# html_content = LogicAniLife.get_html_requests(
# vod_url, referer="https://anilife.live"
# )
# print(f"html_content:: {html_content}")
# output_json = html_to_json.convert(html_content)
# resolution = output_json["html"][0]["body"][0]["_value"]
# logger.debug(f"output_json:: {resolution}")
return vod_url
except Exception as e:
logger.error("Exception:%s", e)
logger.error(traceback.format_exc())
finally:
await browser.close()
@staticmethod
def get_html_selenium(url, referer):
def get_html_selenium(url: str, referer: str) -> str:
from selenium.webdriver.common.by import By
from selenium import webdriver
from selenium_stealth import stealth
@@ -654,6 +659,8 @@ class LogicAniLife(LogicModuleBase):
elem = driver.find_element(By.XPATH, "//*")
source_code = elem.get_attribute("outerHTML")
driver.close()
return source_code.encode("utf-8")
# Create a request interceptor