From 3106e8405532b9b679582596749c6ceb9afd4721 Mon Sep 17 00:00:00 2001 From: LuckyHunter Date: Mon, 17 Oct 2022 00:19:48 +0800 Subject: [PATCH] Add smzdm_bot --- .gitignore | 3 ++ .vscode/launch.json | 14 +++++++ README.md | 35 +++++++++++++++- main.py | 83 +++++++++++++++++++++++++++++++++++++ notifications/pushplus.py | 25 +++++++++++ notifications/serverchan.py | 17 ++++++++ requirements.txt | 7 ++++ 7 files changed, 182 insertions(+), 2 deletions(-) create mode 100644 .vscode/launch.json create mode 100644 main.py create mode 100644 notifications/pushplus.py create mode 100644 notifications/serverchan.py create mode 100644 requirements.txt diff --git a/.gitignore b/.gitignore index b6e4761..8464966 100644 --- a/.gitignore +++ b/.gitignore @@ -127,3 +127,6 @@ dmypy.json # Pyre type checker .pyre/ + +# cookies +cookies.json \ No newline at end of file diff --git a/.vscode/launch.json b/.vscode/launch.json new file mode 100644 index 0000000..1767995 --- /dev/null +++ b/.vscode/launch.json @@ -0,0 +1,14 @@ +{ + "version": "0.2.0", + "configurations": [ + { + "name": "Python: Current File", + "type": "python", + "request": "launch", + "program": "${file}", + "console": "integratedTerminal", + "justMyCode": true, + "envFile": "${workspaceFolder}/.env" + } + ] +} \ No newline at end of file diff --git a/README.md b/README.md index cf849e3..828ccf8 100644 --- a/README.md +++ b/README.md @@ -1,2 +1,33 @@ -# SignBot -Sign Bot +# 什么值得买每日签到脚本 + +

+ + + + +

+ +## 1. 实现功能 + +- `什么值得买`每日签到 +- 通过`pushplus`推送运行结果到微信 + +## 2. 使用方法 + +1. Fork[此仓库项目](https://github.com/chasing66/smzdm_bot)>点击右上角 Fork 按钮即可, 欢迎点`star`~ +2. Secret 新增`COOKIE`, 填入[什么值得买官网](https://www.smzdm.com/)获取的 Cookie 信息, [详见](#31-cookie获取方法) +3. (可选) Secret 新增`PUSH_PLUS_TOKEN`用于推送通知, [详见](https://www.pushplus.plus/) +4. 设置环境变量 + +```bash +export SMZDM_COOKIE=xxxx +export PUSH_PLUS_TOKEN=xxxx +``` + +## 3. 其它 + +### 3.1 Cookie 获取方法 + +- 使用 Chrome 浏览器访问[什么值得买官网](https://www.smzdm.com/), 登录账号 +- 打开开发者工具 (Windows 快捷键`F12`, MacOS 快捷键`option + command + i`) +- 选择 Network, 刷新页面, 选择第一个`www.smzdm.com`, 找到`Requests Headers`里的`Cookie` diff --git a/main.py b/main.py new file mode 100644 index 0000000..a574049 --- /dev/null +++ b/main.py @@ -0,0 +1,83 @@ +import json +import os +import sys +from pprint import pprint + +import requests +from notifications.pushplus import pushplus + + +class SMZDM_Bot(object): + + DEFAULT_HEADERS = { + 'Accept': '*/*', + 'Accept-Encoding': 'gzip, deflate, br', + 'Accept-Language': 'zh-CN,zh;q=0.9', + 'Connection': 'keep-alive', + 'Host': 'zhiyou.smzdm.com', + 'Referer': 'https://www.smzdm.com/', + 'Sec-Fetch-Dest': 'script', + 'Sec-Fetch-Mode': 'no-cors', + 'Sec-Fetch-Site': 'same-site', + 'User-Agent': ('Mozilla/5.0 (Windows NT 10.0; Win64; x64) ' + 'AppleWebKit/537.36 (KHTML, like Gecko) ' + 'Chrome/74.0.3729.131 Safari/537.36'), + } + + def __init__(self): + self.session = requests.Session() + self.session.headers = self.DEFAULT_HEADERS + + def update_cookies(self, cookies): + self.session.cookies.update(cookies) + + def set_cookies(self, cookies): + self.session.headers['Cookie'] = cookies + + def checkin(self): + url = 'https://zhiyou.smzdm.com/user/checkin/jsonp_checkin' + resp = self.session.get(url) + if resp.status_code == 200: + resp_data = resp.json()["data"] + checkin_num = resp_data["checkin_num"] + gold = resp_data["gold"] + point = resp_data["point"] + exp = resp_data["exp"] + rank = resp_data["rank"] + cards = resp_data["cards"] + msg = f'''⭐签到成功{checkin_num}天 + 🏅金币{gold} + 🏅积分{point} + 🏅经验{exp} + 🏅等级{rank} + 🏅补签卡{cards}''' + return msg + else: + pprint("Faile to sign in") + + +if __name__ == '__main__': + smzdm_bot = SMZDM_Bot() + if not os.environ.get("SMZDM_COOKIE", None): + current_dir = os.path.dirname(os.path.realpath(__file__)) + cookies_file_path = os.path.join(current_dir, 'cookies.json') + if not os.path.exists(cookies_file_path): + pprint("Cookies not existed, exit") + sys.exit(1) + with open("cookies.json", "r") as f: + cookies = json.load(f) + smzdm_cookies = {} + for cookie in cookies: + smzdm_cookies.update({cookie["name"]: cookie["value"]}) + smzdm_bot.update_cookies(smzdm_cookies) + else: + smzdm_cookies = os.environ.get( + "SMZDM_COOKIE").encode('UTF-8').decode('latin-1') + smzdm_bot.set_cookies(smzdm_cookies) + resp = smzdm_bot.checkin() + if not os.environ.get('PUSH_PLUS_TOKEN'): + pprint("Skip PushPlus notication") + else: + title = '什么值得买每日签到' + token = os.environ.get('PUSH_PLUS_TOKEN') + pushplus(title=title, content=resp, token=token) diff --git a/notifications/pushplus.py b/notifications/pushplus.py new file mode 100644 index 0000000..38e768a --- /dev/null +++ b/notifications/pushplus.py @@ -0,0 +1,25 @@ +import json +import requests +import os + + +def pushplus(title, content, token, template='html'): + url = 'https://www.pushplus.plus/send' + body = { + 'token': token, + 'title': title, + 'content': content, + 'template': template + } + data = json.dumps(body).encode(encoding='utf-8') + headers = {'Content-Type': 'application/json'} + rsp = requests.post(url, data=data, headers=headers) + return rsp.json() + + +if __name__ == '__main__': + token = os.environ.get('PUSH_PLUS_TOKEN') + res = pushplus(title='Title', + content='Content', + token=token) + print(res) diff --git a/notifications/serverchan.py b/notifications/serverchan.py new file mode 100644 index 0000000..d69cff0 --- /dev/null +++ b/notifications/serverchan.py @@ -0,0 +1,17 @@ +import requests +import os + + +def serverchan(text, desp, secretKey): + url = f'http://sc.ftqq.com/{secretKey}.send' + session = requests.Session() + data = {'text': text, 'desp': desp} + resp = session.post(url, data=data) + return resp.json() + + +if __name__ == '__main__': + token = os.environ.get('PUSH_PLUS_TOKEN') + resp = serverchan(text='test', desp='hi', + secretKey=token) + print(resp) diff --git a/requirements.txt b/requirements.txt new file mode 100644 index 0000000..113256a --- /dev/null +++ b/requirements.txt @@ -0,0 +1,7 @@ +certifi==2022.9.24 +charset-normalizer==2.1.1 +docopt==0.6.2 +idna==3.4 +requests==2.28.1 +urllib3==1.26.12 +yarg==0.1.9 \ No newline at end of file