From 2014345a31d101267c82006fd9f611568d25d902 Mon Sep 17 00:00:00 2001 From: LibraHp_0928 <1941163264@qq.com> Date: Tue, 13 Feb 2024 13:45:57 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=E5=B7=A5=E5=85=B7=E7=B1=BB?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- main.py | 13 +--- resource/config/config.ini | 3 + test/test.py | 81 +++++++++++++++++++ test/test1.py | 155 +++++++++++++++++++++++++++++++++++++ util/ConfigUtil.py | 7 ++ util/LoginUtil.py | 97 +++++++++++++++++++++++ util/RequestUtil.py | 53 +++++++++++++ 7 files changed, 399 insertions(+), 10 deletions(-) create mode 100644 resource/config/config.ini create mode 100644 test/test.py create mode 100644 test/test1.py create mode 100644 util/ConfigUtil.py create mode 100644 util/LoginUtil.py create mode 100644 util/RequestUtil.py diff --git a/main.py b/main.py index 90ffb6f..8327f01 100644 --- a/main.py +++ b/main.py @@ -1,16 +1,9 @@ -# 这是一个示例 Python 脚本。 - -# 按 Shift+F10 执行或将其替换为您的代码。 -# 按 双击 Shift 在所有地方搜索类、文件、工具窗口、操作和设置。 - - -def print_hi(name): - # 在下面的代码行中使用断点来调试脚本。 - print(f'Hi, {name}') # 按 Ctrl+F8 切换断点。 +import util.RequestUtil as Request # 按间距中的绿色按钮以运行脚本。 if __name__ == '__main__': - print_hi('PyCharm') + message = Request.get_message(0, 10) + print(message.text) # 访问 https://www.jetbrains.com/help/pycharm/ 获取 PyCharm 帮助 diff --git a/resource/config/config.ini b/resource/config/config.ini new file mode 100644 index 0000000..11bc5c3 --- /dev/null +++ b/resource/config/config.ini @@ -0,0 +1,3 @@ +[File] +temp = ./resource/temp/ +user = ./resource/user/ \ No newline at end of file diff --git a/test/test.py b/test/test.py new file mode 100644 index 0000000..d524df2 --- /dev/null +++ b/test/test.py @@ -0,0 +1,81 @@ +import re +import requests +import json + + +def qzone_login(): + cookies = { + 'ptui_loginuin': '1941163264', + 'RK': 'nzPEtRXmWI', + 'ptcz': '4b478c47ddfaf994cad9c3eeb6099336c6302fdde27eb31949fd0d8502e7aed6', + 'pgv_pvid': '249131564', + 'qz_screen': '1707x960', + 'QZ_FE_WEBP_SUPPORT': '1', + 'cpu_performance_v8': '1', + '_qpsvr_localtk': '0.0676035804419064', + 'pgv_info': 'ssid=s3673858535', + 'uin': 'o1941163264', + 'p_uin': 'o1941163264', + 'pt4_token': 'QUuDx3v5A1fbS9y1V-bgUZvp4AXSGeEt2897lTJUpgc_', + 'p_skey': '-RBLfzmoS*kIv9TQZMXaVjOEPuHcxMPsAQdhB6uSOdg_', + 'Loading': 'Yes', + '1941163264_todaycount': '0', + '1941163264_totalcount': '20087', + '__Q_w_s__QZN_TodoMsgCnt': '1', + '__layoutStat': '12', + } + + 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', + # Requests sorts cookies= alphabetically + # 'cookie': 'ptui_loginuin=1941163264; RK=nzPEtRXmWI; ptcz=4b478c47ddfaf994cad9c3eeb6099336c6302fdde27eb31949fd0d8502e7aed6; pgv_pvid=249131564; qz_screen=1707x960; QZ_FE_WEBP_SUPPORT=1; cpu_performance_v8=1; _qpsvr_localtk=0.0676035804419064; pgv_info=ssid=s3673858535; uin=o1941163264; p_uin=o1941163264; pt4_token=QUuDx3v5A1fbS9y1V-bgUZvp4AXSGeEt2897lTJUpgc_; p_skey=-RBLfzmoS*kIv9TQZMXaVjOEPuHcxMPsAQdhB6uSOdg_; Loading=Yes; 1941163264_todaycount=0; 1941163264_totalcount=20087; __Q_w_s__QZN_TodoMsgCnt=1; __layoutStat=12', + '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': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/121.0.0.0 Safari/537.36 Edg/121.0.0.0', + } + + params = { + 'uin': '1941163264', + 'begin_time': '0', + 'end_time': '0', + 'getappnotification': '1', + 'getnotifi': '1', + 'has_get_key': '0', + 'offset': '15731', + 'set': '0', + 'count': '10', + 'useutf8': '1', + 'outputhtmlfeed': '1', + 'grz': '0.791498607065434', + 'scope': '1', + 'g_tk': [ + '1928892726', + '1928892726', + ], + } + + 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) + return response + + +response = qzone_login() +json_str = response.text.strip().lstrip('_Callback(').rstrip(');') +# 将属性值的单引号替换为双引号 +output_str = re.sub(r"'([^']*)'", r'"\1"', json_str) +print(output_str) +output_str = re.sub(r'(\w+)(:)', r'"\1"\2', output_str) +# 移除多余的换行和制表符 +output_str = output_str.replace('\n', '').replace('\t', '').replace(' ', '') +print(output_str) + diff --git a/test/test1.py b/test/test1.py new file mode 100644 index 0000000..dfc2bcb --- /dev/null +++ b/test/test1.py @@ -0,0 +1,155 @@ +import requests +from PIL import Image +import time +import re +import os + +def bkn(pSkey): + # 计算bkn + t, n, o = 5381, 0, len(pSkey) + + while n < o: + t += (t << 5) + ord(pSkey[n]) + n += 1 + + return t & 2147483647 + + +def ptqrToken(qrsig): + # 计算ptqrtoken + n, i, e = len(qrsig), 0, 0 + + while n > i: + e += (e << 5) + ord(qrsig[i]) + i += 1 + + return 2147483647 & e + + +def QR(): + # 获取 qq空间 二维码 + url = 'https://ssl.ptlogin2.qq.com/ptqrshow?appid=549000912&e=2&l=M&s=3&d=72&v=4&t=0.8692955245720428&daid=5&pt_3rd_aid=0' + + try: + r = requests.get(url) + qrsig = requests.utils.dict_from_cookiejar(r.cookies).get('qrsig') + + with open(r'.\QR.png', 'wb') as f: + f.write(r.content) + + im = Image.open(r'.\QR.png') + im = im.resize((350, 350)) + print(time.strftime('%H:%M:%S'), '登录二维码获取成功') + im.show() + + return qrsig + + except Exception as e: + print(e) + + +def cookie(): + if os.path.exists('cookies.txt'): + with open('cookies.txt', 'r') as file: + cookies = file.read() + return eval(cookies) + # 获取 QQ空间 cookie + qrsig = QR() + ptqrtoken = ptqrToken(qrsig) + + while True: + url = 'https://ssl.ptlogin2.qq.com/ptqrlogin?u1=https%3A%2F%2Fqzs.qq.com%2Fqzone%2Fv5%2Floginsucc.html%3Fpara%3Dizone&ptqrtoken=' + str( + ptqrtoken) + '&ptredirect=0&h=1&t=1&g=1&from_ui=1&ptlang=2052&action=0-0-' + str( + time.time()) + '&js_ver=20032614&js_type=1&login_sig=&pt_uistyle=40&aid=549000912&daid=5&' + cookies = {'qrsig': qrsig} + + try: + r = requests.get(url, cookies=cookies) + + if '二维码未失效' in r.text: + print(time.strftime('%H:%M:%S'), '二维码未失效') + + elif '二维码认证中' in r.text: + print(time.strftime('%H:%M:%S'), '二维码认证中') + + elif '二维码已失效' in r.text: + print(time.strftime('%H:%M:%S'), '二维码已失效') + + else: + print(time.strftime('%H:%M:%S'), '登录成功') + + cookies = requests.utils.dict_from_cookiejar(r.cookies) + uin = requests.utils.dict_from_cookiejar(r.cookies).get('uin') + regex = re.compile(r'ptsigx=(.*?)&') + sigx = re.findall(regex, r.text)[0] + url = 'https://ptlogin2.qzone.qq.com/check_sig?pttype=1&uin=' + uin + '&service=ptqrlogin&nodirect=0&ptsigx=' + sigx + '&s_url=https%3A%2F%2Fqzs.qq.com%2Fqzone%2Fv5%2Floginsucc.html%3Fpara%3Dizone&f_url=&ptlang=2052&ptredirect=100&aid=549000912&daid=5&j_later=0&low_login_hour=0®master=0&pt_login_type=3&pt_aid=0&pt_aaid=16&pt_light=0&pt_3rd_aid=0' + + try: + r = requests.get(url, cookies=cookies, allow_redirects=False) + targetCookies = requests.utils.dict_from_cookiejar(r.cookies) + # 存储cookies到本地 + with open(r'.\cookies.txt', 'w') as f: + f.write(str(targetCookies)) + pSkey = requests.utils.dict_from_cookiejar(r.cookies).get('p_skey') + break + + except Exception as e: + print(e) + + except Exception as e: + print(e) + + time.sleep(3) + + return targetCookies + + +def get_message(): + cookies = cookie() + g_tk = bkn(cookies.get('p_skey')) + # 去掉uin首位的o + uin = cookies.get('uin')[1:] + print(cookies) + 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': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/121.0.0.0 Safari/537.36 Edg/121.0.0.0', + } + params = { + 'uin': uin, + 'begin_time': '0', + 'end_time': '0', + 'getappnotification': '1', + 'getnotifi': '1', + 'has_get_key': '0', + 'offset': '0', + 'set': '0', + 'count': '10', + 'useutf8': '1', + 'outputhtmlfeed': '1', + 'scope': '1', + 'g_tk': [ + g_tk, + g_tk, + ], + } + 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) + return response + + +if __name__ == '__main__': + message = get_message().text + json_str = message.strip().lstrip('_Callback(').rstrip(');') + print(json_str) diff --git a/util/ConfigUtil.py b/util/ConfigUtil.py new file mode 100644 index 0000000..12bde94 --- /dev/null +++ b/util/ConfigUtil.py @@ -0,0 +1,7 @@ +import configparser + +config = configparser.ConfigParser() +config.read('./resource/config/config.ini') + +temp_path = config.get('File', 'temp') +user_path = config.get('File', 'user') \ No newline at end of file diff --git a/util/LoginUtil.py b/util/LoginUtil.py new file mode 100644 index 0000000..8265ab7 --- /dev/null +++ b/util/LoginUtil.py @@ -0,0 +1,97 @@ +import requests +from PIL import Image +import time +import re +import os +import util.ConfigUtil as Config + +def bkn(pSkey): + # 计算bkn + t, n, o = 5381, 0, len(pSkey) + + while n < o: + t += (t << 5) + ord(pSkey[n]) + n += 1 + + return t & 2147483647 + + +def ptqrToken(qrsig): + # 计算ptqrtoken + n, i, e = len(qrsig), 0, 0 + + while n > i: + e += (e << 5) + ord(qrsig[i]) + i += 1 + + return 2147483647 & e + + +def QR(): + # 获取 qq空间 二维码 + url = 'https://ssl.ptlogin2.qq.com/ptqrshow?appid=549000912&e=2&l=M&s=3&d=72&v=4&t=0.8692955245720428&daid=5&pt_3rd_aid=0' + + try: + r = requests.get(url) + qrsig = requests.utils.dict_from_cookiejar(r.cookies).get('qrsig') + + with open(Config.temp_path + 'QR.png', 'wb') as f: + f.write(r.content) + + im = Image.open(Config.temp_path + 'QR.png') + im = im.resize((350, 350)) + print(time.strftime('%H:%M:%S'), '登录二维码获取成功') + im.show() + + return qrsig + + except Exception as e: + print(e) + + +def cookie(): + # 获取 QQ空间 cookie + qrsig = QR() + ptqrtoken = ptqrToken(qrsig) + + while True: + url = 'https://ssl.ptlogin2.qq.com/ptqrlogin?u1=https%3A%2F%2Fqzs.qq.com%2Fqzone%2Fv5%2Floginsucc.html%3Fpara' \ + '%3Dizone&ptqrtoken=' + str(ptqrtoken) + '&ptredirect=0&h=1&t=1&g=1&from_ui=1&ptlang=2052&action=0-0-' \ + + str(time.time()) + '&js_ver=20032614&js_type=1&login_sig=&pt_uistyle=40&aid=549000912&daid=5&' + cookies = {'qrsig': qrsig} + try: + r = requests.get(url, cookies=cookies) + if '二维码未失效' in r.text: + print(time.strftime('%H:%M:%S'), '二维码未失效') + elif '二维码认证中' in r.text: + print(time.strftime('%H:%M:%S'), '二维码认证中') + elif '二维码已失效' in r.text: + print(time.strftime('%H:%M:%S'), '二维码已失效') + else: + print(time.strftime('%H:%M:%S'), '登录成功') + cookies = requests.utils.dict_from_cookiejar(r.cookies) + uin = requests.utils.dict_from_cookiejar(r.cookies).get('uin') + regex = re.compile(r'ptsigx=(.*?)&') + sigx = re.findall(regex, r.text)[0] + url = 'https://ptlogin2.qzone.qq.com/check_sig?pttype=1&uin=' + uin + '&service=ptqrlogin&nodirect=0' \ + '&ptsigx=' + sigx + \ + '&s_url=https%3A%2F%2Fqzs.qq.com%2Fqzone%2Fv5%2Floginsucc.html%3Fpara%3Dizone&f_url=&ptlang' \ + '=2052&ptredirect=100&aid=549000912&daid=5&j_later=0&low_login_hour=0®master=0&pt_login_type' \ + '=3&pt_aid=0&pt_aaid=16&pt_light=0&pt_3rd_aid=0' + try: + r = requests.get(url, cookies=cookies, allow_redirects=False) + target_cookies = requests.utils.dict_from_cookiejar(r.cookies) + p_skey = requests.utils.dict_from_cookiejar(r.cookies).get('p_skey') + # 删除二维码 + os.remove(Config.temp_path + 'QR.png') + break + + except Exception as e: + print(e) + + except Exception as e: + print(e) + + time.sleep(3) + + return target_cookies diff --git a/util/RequestUtil.py b/util/RequestUtil.py new file mode 100644 index 0000000..24e300d --- /dev/null +++ b/util/RequestUtil.py @@ -0,0 +1,53 @@ +import util.LoginUtil as Login +import requests + +# 登陆后获取到的cookies +cookies = Login.cookie() +# 获取g_tk +g_tk = Login.bkn(cookies.get('p_skey')) +# 获取uin +uin = cookies.get('uin')[1:] +# 全局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': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/121.0.0.0 ' + 'Safari/537.36 Edg/121.0.0.0', +} + + +# 获取历史消息列表 +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', + 'g_tk': [ + g_tk, + g_tk, + ], + } + 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) + return response