File size: 6,138 Bytes
65d3d39 6ed2c3e 65d3d39 6ed2c3e 65d3d39 6ed2c3e 65d3d39 917acfe 65d3d39 |
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 |
# coding:utf-8
import os
import sys
import socket
import asyncio
import psutil
import requests
from os.path import join
from tld import get_tld
from loguru import logger
from playwright.async_api import async_playwright, Page
from playwright_stealth import stealth_async
from option_mysql import OptionMysql
from __init__ import (
gotify_host,
gotify_port,
gotify_token,
gotify_img,
mysql_option,
DIR_PATH,
DEBUG,
browser_headless,
browser_proxy,
ignore_https_errors,
user_agent,
)
# webdriver
async def init_page(storage_state=None, browser_type: str = "chromium") -> Page:
if browser_type not in ("chromium", "firefox", "webkit"):
raise TypeError("unspported browser")
async with async_playwright() as p:
if browser_type == "chromium":
browser = await p.chromium.launch(headless=browser_headless)
elif browser_type == "firefox":
browser = await p.firefox.launch(headless=browser_headless)
else:
logger.error("初始化浏览器失败")
exit(-1)
context = await browser.new_context(
proxy=browser_proxy,
ignore_https_errors=ignore_https_errors,
user_agent=user_agent,
)
# 从文件读取 Cookie
context = await browser.new_context(storage_state=storage_state)
page = await context.new_page()
# 隐藏 webdriver 特征
await stealth_async(page)
# await page.pause() # async 跳出上下文就会 playwright._impl._api_types.Error: Connection closed
await goto_github(page)
await dump_cookie(page) # 创建函数:一个 browser 对应一个 context、page,在 with 内完成多个任务
await browser.close()
async def goto_github(page):
await page.goto("https://github.com")
async def dump_cookie(page):
# url = "https://www.52pojie.cn/home.php"
# url = "https://v2ex.com"
# url = "https://www.bilibili.com"
# url = "https://www.tsdm39.com/forum.php?mobile=no"
# url = "https://www.aliyundrive.com/drive"
url = "https://huggingface.co/login"
save_cookie = join(DIR_PATH, f"{get_domain(url)}.json")
await page.goto(url)
logger.debug(DIR_PATH)
await page.screenshot(
path=join(DIR_PATH, "screenshot.png"),
full_page=True,
) # 截图
while input("input c to continue: ") != "c": # 阻塞等待填写登录信息
continue
await page.context.storage_state(path=save_cookie) # 保存 Cookie 到文件
# 获取容器内 IP
def get_local_ip():
net_card_info = []
info = psutil.net_if_addrs()
for k, v in info.items():
for item in v:
if item[0] == 2 and not item[1] == "127.0.0.1":
net_card_info.append(item[1])
local_ip = net_card_info[0]
return local_ip
# 从 url 获取域名
def get_domain(url: str) -> str:
result = get_tld(url, as_object=True)
domain = result.domain
return domain
# 从域名获取 IP
def get_domain_ip(host: str) -> str:
domain_ip = socket.gethostbyname(host)
return domain_ip
# gotify 推送
def push_msg(title: str = "无标题", message: str = "无内容", img_url: str = gotify_img):
gotify_ip = get_domain_ip(gotify_host)
url = f"http://{gotify_ip}:{gotify_port}/message?token={gotify_token}"
resp = requests.post(
url,
json={
"title": title,
"message": message,
"priority": 10,
"extras": {
"client::display": {"contentType": "text/markdown"},
"client::notification": {"bigImageUrl": img_url},
},
},
)
return resp.json()
def get_bilibili_live_rooms_from_pi() -> dict:
logger.info("getting bilibili_live_rooms from pi")
bilibili_live_rooms = {}
mysql = OptionMysql(mysql_option)
sql = f"SELECT room_id, username FROM live_rooms"
data = mysql.get_dict_data(sql)
if data:
for d in data:
bilibili_live_rooms[d["room_id"]] = d["username"]
return bilibili_live_rooms
def get_cookie_from_pi() -> dict:
logger.info("getting cookie from pi")
mysql = OptionMysql(mysql_option)
sql = f"SELECT cookie_name, cookie_value FROM cookie"
data = mysql.get_dict_data(sql)
for d in data:
log_file = join(DIR_PATH, f"cookie/{d['cookie_name']}.json")
with open(log_file, "w") as lf:
lf.write(d["cookie_value"])
return data[0]["cookie_name"]
def get_aliyundrive_refresh_token():
mysql = OptionMysql(mysql_option)
sql = f"SELECT token FROM aliyundrive WHERE id=1"
data = mysql.get_dict_data(sql)
if data:
logger.info("获取阿里云盘 refresh_token 成功")
return data[0]["token"]
return False
def update_aliyundrive_access_token(access_token):
mysql = OptionMysql(mysql_option)
sql = f"UPDATE aliyundrive SET token=%s WHERE id=2"
data = mysql.update_data(sql, [access_token])
if data:
logger.info("update aliyundrive access token")
return True
return False
def get_log(log_file: str) -> str:
with open(log_file, "r", encoding="utf-8") as f:
data = f.read()
return data
if __name__ == "__main__":
logger.debug(f"DEBUG Mode: {DEBUG}")
logger.debug(f"DIR_PATH: {DIR_PATH}")
# asyncio.run(init_page())
# logger.debug(f"get_domain_ip: {get_domain_ip(gotify_host)}")
# logger.debug(f'get_domain: {get_domain("https://github.com")}')
# logger.debug(f"list_path: {list_path()}")
logger.debug(f'push_msg: {push_msg("utils.py", "hello world")}')
# logger.debug(f"get_aliyun_token_from_pi(): {get_aliyun_token_from_pi()}")
# logger.debug(f"get_bilibili_live_rooms_from_pi(): {get_bilibili_live_rooms_from_pi()}")
# logger.debug(f"get_cookie_from_pi(): {get_cookie_from_pi()}")
# logger.debug(f'get_cron_log(): {get_cron_log("log/tsdm.log")}')
# logger.debug(f"get_aliyundrive_access_token: {get_aliyundrive_access_token()}")
# logger.debug(f"update_aliyundrive_access_token: {update_aliyundrive_access_token('123')}")
|