GetQzonehistory/util/RequestUtil.py

108 lines
3.1 KiB
Python
Raw Normal View History

import re
2024-02-14 13:15:17 +00:00
from tqdm import tqdm
2024-02-13 05:45:57 +00:00
import util.LoginUtil as Login
import requests
import json
from fake_useragent import UserAgent
2024-02-14 13:15:17 +00:00
ua = UserAgent()
2024-02-13 05:45:57 +00:00
# 登陆后获取到的cookies
cookies = Login.cookie()
# 获取g_tk
g_tk = Login.bkn(cookies.get('p_skey'))
# 获取uin
uin = re.sub(r'o0*', '', cookies.get('uin'))
2024-02-13 05:45:57 +00:00
# 全局header
headers = {
'authority': 'user.qzone.qq.com',
'accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,'
'application/signed-exchange;v=b3;q=0.7',
'accept-language': 'zh-CN,zh;q=0.9,en;q=0.8,en-GB;q=0.7,en-US;q=0.6',
'cache-control': 'no-cache',
'pragma': 'no-cache',
'sec-ch-ua': '"Not A(Brand";v="99", "Microsoft Edge";v="121", "Chromium";v="121"',
'sec-ch-ua-mobile': '?0',
'sec-ch-ua-platform': '"Windows"',
'sec-fetch-dest': 'document',
'sec-fetch-mode': 'navigate',
'sec-fetch-site': 'none',
'sec-fetch-user': '?1',
'upgrade-insecure-requests': '1',
'user-agent': ua.safari,
2024-02-13 05:45:57 +00:00
}
# 获取历史消息列表
def get_message(start, count):
params = {
'uin': uin,
'begin_time': '0',
'end_time': '0',
'getappnotification': '1',
'getnotifi': '1',
'has_get_key': '0',
'offset': start,
'set': '0',
'count': count,
'useutf8': '1',
'outputhtmlfeed': '1',
'scope': '1',
'format': 'jsonp',
2024-02-13 05:45:57 +00:00
'g_tk': [
g_tk,
g_tk,
],
}
try:
response = requests.get(
'https://user.qzone.qq.com/proxy/domain/ic2.qzone.qq.com/cgi-bin/feeds/feeds2_html_pav_all',
params=params,
cookies=cookies,
headers=headers,
timeout=(5, 10) # 设置连接超时为5秒读取超时为10秒
)
except requests.Timeout:
print("请求超时")
return None
2024-02-13 05:45:57 +00:00
return response
def get_login_user_info():
response = requests.get('https://r.qzone.qq.com/fcg-bin/cgi_get_portrait.fcg?g_tk=' + str(g_tk) + '&uins=' + uin,
headers=headers, cookies=cookies)
info = response.content.decode('GBK')
info = info.strip().lstrip('portraitCallBack(').rstrip(');')
info = json.loads(info)
return info
2024-02-14 13:15:17 +00:00
def get_message_count():
# 初始的总量范围
lower_bound = 0
upper_bound = 10000000 # 假设最大总量为1000000
2024-02-14 13:15:17 +00:00
total = upper_bound // 2 # 初始的总量为上下界的中间值
with tqdm(desc="正在获取消息列表数量...") as pbar:
while lower_bound <= upper_bound:
try:
response = get_message(total, 100)
if response and hasattr(response, 'text'):
if "li" in response.text:
lower_bound = total + 1
else:
upper_bound = total - 1
else:
print(f"无效的响应对象: {response}")
break
except Exception as e:
print(f"请求发生异常: {e}")
break
total = (lower_bound + upper_bound) // 2
2024-02-14 13:15:17 +00:00
pbar.update(1)
2024-02-14 13:15:17 +00:00
return total