Skip to content

Commit

Permalink
fix: 仿真重试
Browse files Browse the repository at this point in the history
  • Loading branch information
linyuan0213 committed Jul 12, 2024
1 parent f12ee24 commit 01c4711
Show file tree
Hide file tree
Showing 8 changed files with 110 additions and 15 deletions.
13 changes: 12 additions & 1 deletion app/plugins/modules/_autosignin/btschool.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
from time import sleep
from app.helper.drissionpage_helper import DrissionPageHelper
from app.plugins.modules._autosignin._base import _ISiteSigninHandler
from app.utils import StringUtils, RequestUtils
Expand Down Expand Up @@ -100,7 +101,17 @@ def signin(self, site_info: dict):
return True, f'【{site}】签到成功'

def __chrome_visit(self, chrome, url, ua, site_cookie, proxy, site):
html_text = chrome.get_page_html(url=url, ua=ua, cookies=site_cookie, proxies=proxy)
tries = 3
while tries > 0:
try:
html_text = chrome.get_page_html(url=url, ua=ua, cookies=site_cookie, proxies=proxy)
if html_text:
break
except Exception as e:
self.debug(f'获取网页HTML失败: {str(e)} 重试中...')
finally:
tries -= 1
sleep(2)

if not html_text:
self.warn("%s 获取站点源码失败" % site)
Expand Down
12 changes: 11 additions & 1 deletion app/plugins/modules/_autosignin/tjupt.py
Original file line number Diff line number Diff line change
Expand Up @@ -191,7 +191,17 @@ def signin(self, site_info: dict):
# 豆瓣未获取到答案,使用google识图
image_search_url = f"https://lens.google.com/uploadbyurl?url={img_url}"
chrome = DrissionPageHelper()
html_text = chrome.get_page_html(url=image_search_url, ua=ua, proxies=Config().get_proxies() if proxy else None)
tries = 3
while tries > 0:
try:
html_text = chrome.get_page_html(url=image_search_url, ua=ua, proxies=Config().get_proxies() if proxy else None)
if html_text:
break
except Exception as e:
self.debug(f'获取网页HTML失败: {str(e)} 重试中...')
finally:
tries -= 1
time.sleep(2)

search_results = BeautifulSoup(html_text, "lxml").find_all("div", class_="UAiK1e")
if not search_results:
Expand Down
33 changes: 27 additions & 6 deletions app/plugins/modules/autosignin.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
from datetime import datetime, timedelta
from multiprocessing.dummy import Pool as ThreadPool
from threading import Event
from time import sleep
from urllib.parse import urlsplit

import pytz
Expand Down Expand Up @@ -464,7 +465,17 @@ def __signin_base(self, site_info):
home_url = StringUtils.get_base_url(site_url)
if "1ptba" in home_url:
home_url = f"{home_url}/index.php"
html_text = chrome.get_page_html(url=home_url, ua=ua, cookies=site_cookie, proxies=site_info.get("proxy"))
tries = 3
while tries > 0:
try:
html_text = chrome.get_page_html(url=home_url, ua=ua, cookies=site_cookie, proxies=site_info.get("proxy"))
if html_text:
break
except Exception as e:
self.debug(f'获取网页HTML失败: {str(e)} 重试中...')
finally:
tries -= 1
sleep(2)
if not html_text:
self.warn("%s 无法打开网站" % site)
return f"【{site}】仿真签到失败,无法打开网站!"
Expand All @@ -488,11 +499,21 @@ def __signin_base(self, site_info):
return f"【{site}】模拟登录失败!"
# 开始仿真
try:
html_text = chrome.get_page_html(url=home_url,
ua=ua,
cookies=site_cookie,
proxies=site_info.get("proxy"),
callback=lambda page: page(f'x:{xpath_str}').click(by_js=True))
tries = 3
while tries > 0:
try:
html_text = chrome.get_page_html(url=home_url,
ua=ua,
cookies=site_cookie,
proxies=site_info.get("proxy"),
callback=lambda page: page(f'x:{xpath_str}').click(by_js=True))
if html_text:
break
except Exception as e:
self.debug(f'获取网页HTML失败: {str(e)} 重试中...')
finally:
tries -= 1
sleep(2)
if not html_text:
self.info("%s 仿真签到失败,无法通过Cloudflare" % site)
return f"【{site}】仿真签到失败,无法通过Cloudflare!"
Expand Down
13 changes: 12 additions & 1 deletion app/plugins/modules/opensubtitles.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import os
import shutil
from functools import lru_cache
from time import sleep
from urllib.parse import quote

from pyquery import PyQuery
Expand Down Expand Up @@ -213,7 +214,17 @@ def __parse_opensubtitles_results(cls, url):
if not chrome.visit(url):
return []
# 源码
html_text = chrome.get_page_html(url=url)
tries = 3
while tries > 0:
try:
html_text = chrome.get_page_html(url=url)
if html_text:
break
except Exception as e:
self.debug(f'获取网页HTML失败: {str(e)} 重试中...')
finally:
tries -= 1
sleep(2)
# 解析列表
ret_subtitles = []
html_doc = PyQuery(html_text)
Expand Down
13 changes: 12 additions & 1 deletion app/sites/site_userinfo.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
from datetime import datetime
from multiprocessing.dummy import Pool as ThreadPool
from threading import Lock
from time import sleep

import requests

Expand Down Expand Up @@ -69,7 +70,17 @@ def build(self, url, site_id, site_name,
# 检测环境,有浏览器内核的优先使用仿真签到
chrome = DrissionPageHelper()
if emulate:
html_text = chrome.get_page_html(url=url, ua=ua, cookies=site_cookie, proxies=Config().get_proxies() if proxy else None)
tries = 3
while tries > 0:
try:
html_text = chrome.get_page_html(url=url, ua=ua, cookies=site_cookie, proxies=Config().get_proxies() if proxy else None)
if html_text:
break
except Exception as e:
self.debug(f'获取网页HTML失败: {str(e)} 重试中...')
finally:
tries -= 1
sleep(2)
# 循环检测是否过cf
if not html_text:
log.error("【Sites】%s 跳转站点失败" % site_name)
Expand Down
15 changes: 12 additions & 3 deletions app/sites/siteconf.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
from app.utils import ExceptionUtils, StringUtils, RequestUtils, JsonUtils
from app.utils.commons import singleton
from config import MT_URL, Config
import log


@singleton
Expand Down Expand Up @@ -263,9 +264,17 @@ def __get_site_page_html(url, cookie, ua, headers=None, render=False, proxy=Fals
chrome = DrissionPageHelper()
if render and chrome.get_status():
# 开渲染
html_text = chrome.get_page_html(url=url, cookies=cookie, ua=ua, proxies=proxy)
if html_text:
return html_text
tries = 3
while tries > 0:
try:
html_text = chrome.get_page_html(url=url, cookies=cookie, ua=ua, proxies=proxy)
if html_text:
return html_text
except Exception as e:
log.debug(f'获取网页HTML失败: {str(e)} 重试中...')
finally:
tries -= 1
time.sleep(2)
elif 'm-team' in url:
param = {'id': param}
headers.update({
Expand Down
13 changes: 12 additions & 1 deletion app/sites/sites.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import json
from datetime import datetime
from time import sleep

import log
from app.helper import SiteHelper, DbHelper, DrissionPageHelper
Expand Down Expand Up @@ -305,7 +306,17 @@ def test_connection(self, site_id):
# 计时
chrome = DrissionPageHelper()
start_time = datetime.now()
html_text = chrome.get_page_html(url=site_url, ua=ua, cookies=site_cookie, proxies=Config().get_proxies() if site_info.get("proxy") else None)
tries = 3
while tries > 0:
try:
html_text = chrome.get_page_html(url=site_url, ua=ua, cookies=site_cookie, proxies=Config().get_proxies() if site_info.get("proxy") else None)
if html_text:
break
except Exception as e:
log.debug(f'获取网页HTML失败: {str(e)} 重试中...')
finally:
tries -= 1
sleep(2)
seconds = int((datetime.now() - start_time).microseconds / 1000)
# 判断是否已签到
if not html_text:
Expand Down
13 changes: 12 additions & 1 deletion app/sites/siteuserinfo/_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import json
import re
from abc import ABCMeta, abstractmethod
from time import sleep
from urllib.parse import urljoin, urlsplit

import requests
Expand Down Expand Up @@ -258,7 +259,17 @@ def _get_page_content(self, url, params=None, headers=None):
log.debug(f"【Sites】{self.site_name} 检测到Cloudflare,需要浏览器仿真")
chrome = DrissionPageHelper()
if self._emulate and chrome.get_status():
html_text = chrome.get_page_html(url=url, ua=self._ua, cookies=self._site_cookie, proxies=proxies)
tries = 3
while tries > 0:
try:
html_text = chrome.get_page_html(url=url, ua=self._ua, cookies=self._site_cookie, proxies=proxies)
if html_text:
break
except Exception as e:
log.debug(f'获取网页HTML失败: {str(e)} 重试中...')
finally:
tries -= 1
sleep(2)
if not html_text:
log.error(f"【Sites】{self.site_name} 无法打开网站")
return None
Expand Down

0 comments on commit 01c4711

Please sign in to comment.