forked from TRHX/CSDNSignIn
-
Notifications
You must be signed in to change notification settings - Fork 0
/
CSDN.py
177 lines (157 loc) · 6.94 KB
/
CSDN.py
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
# ====================================
# --*-- coding: utf-8 --*--
# @Time : 2021-05-29
# @FileName: CSDN.py
# @Software: PyCharm
# ====================================
import requests
import json
import os
import time
import hmac
import hashlib
import base64
import urllib.parse
# ============== 1.CSDN 个人信息 ============== #
CSDN_ID = os.environ["CSDN_ID"] # 必填!CSDN 的 ID
COOKIE = os.environ["COOKIE"] # 必填!已登录的 cookie
# ============== 2.功能开关配置项 ============== #
# 填 on 则开启,开启的同时也需要配置3中的选项,不填或填其他则关闭
IF_LUCK_DRAW = os.environ["IF_LUCK_DRAW"] # 选填!是否开启抽奖
IF_SEVER = os.environ["IF_SEVER"] # 选填!是否开启 server 酱通知
IF_WECHAT = os.environ["IF_WECHAT"] # 选填!是否开启企业微信通知
IF_DING = os.environ["IF_DING"] # 选填!是否开启钉钉通知
# ============== 3.消息通知配置项 ============== #
SEVER_SCKEY = os.environ["SEVER_SCKEY"] # 选填!server 酱的 SCKEY
WECHAT_URL = os.environ["WECHAT_URL"] # 选填!企业微信机器人地址
DING_URL = os.environ["DING_URL"] # 选填!钉钉机器人地址
DING_SECRET = os.environ["DING_SECRET"] # 选填!钉钉机器人加签 SECRET
# ============== 4.准备发送的消息 ============== #
TEXT = ''
DESP = ''
class CSDN:
def __init__(self) -> None:
self.DRAW_TIMES = 0 # 可抽奖次数
self.UUID = COOKIE.split(';', 1)[0].split('=', 1)[1]
self.USER_AGENT = 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/90.0.4430.72 Safari/537.36'
self.SIGN_IN_URL = 'https://me.csdn.net/api/LuckyDraw_v2/signIn'
self.LUCKY_DRAW_URL = 'https://me.csdn.net/api/LuckyDraw_v2/goodLuck'
self.HEADERS = {
'accept': 'application/json, text/plain, */*',
'accept-encoding': 'gzip, deflate, br',
'accept-language': 'zh-CN,zh;q=0.9,en-US;q=0.8,en;q=0.7',
'content-length': '243',
'content-type': 'application/json;charset=UTF-8',
'cookie': COOKIE,
'origin': 'https://i.csdn.net',
'referer': 'https://i.csdn.net/',
'sec-fetch-dest': 'empty',
'sec-fetch-mode': 'cors',
'sec-fetch-site': 'same-site',
'user-agent': self.USER_AGENT
}
self.DATA = {
'ip': '',
'platform': 'pc-my',
'product': 'pc',
'user_agent': self.USER_AGENT,
'username': CSDN_ID,
'uuid': self.UUID
}
def csdn_sign_in(self) -> None:
global TEXT, DESP
response = requests.post(url=self.SIGN_IN_URL, headers=self.HEADERS, data=self.DATA)
result = response.json()
if result['code'] == 200:
data = result['data']
if data['isSigned']:
TEXT = 'CSDN 签到失败!'
DESP = 'CSDN 签到失败!你今天已经签到过了哟!'
elif data['signed']:
self.DRAW_TIMES = data['drawTimes']
TEXT = 'CSDN 签到成功!'
DESP = f"签到成功!你已连续签到 {data['keepCount']} 天,累计签到 {data['totalCount']} 天,当前已有 {data['totalSignedCount']} 人签到。\n{data['msg']}"
else:
TEXT = 'CSDN 签到失败!'
DESP = ''
print('签到失败!')
elif result['code'] == 400102:
TEXT = 'CSDN 签到失败!'
DESP = 'CSDN 签到失败!{} 用户不存在或者 cookie 错误!请检查 CSDN ID 或尝试重置 cookie!'.format(CSDN_ID)
else:
TEXT = 'CSDN 签到失败!'
DESP = ''
print(TEXT, DESP)
def csdn_luck_draw(self) -> None:
if self.DRAW_TIMES != 0:
global TEXT, DESP
response = requests.post(url=self.LUCKY_DRAW_URL, headers=self.HEADERS, data=self.DATA)
result = response.json()
if result['code'] == 200:
if result['data']['can_draw']:
prize_title = result['data']['prize_title']
TEXT += 'CSDN 抽奖成功!'
DESP += '抽奖成功!恭喜你获得{}'.format(prize_title)
print('抽奖成功!恭喜你获得{}'.format(prize_title))
elif not result['data']['can_draw']:
TEXT += 'CSDN 抽奖失败!'
DESP += 'CSDN 抽奖失败!抽奖机会已经用完了哟!'
print('抽奖机会已经用完了哟!')
else:
TEXT += 'CSDN 抽奖失败!'
print('抽奖失败!')
elif result['code'] == 400102:
TEXT = 'CSDN 抽奖失败!'
DESP = 'CSDN 抽奖失败!{} 用户不存在或者 cookie 错误!请检查 CSDN ID 或尝试重置 cookie!'.format(CSDN_ID)
print('抽奖失败!{} 用户不存在或者 cookie 错误!请检查 CSDN ID 或尝试重置 cookie!'.format(CSDN_ID))
else:
TEXT += 'CSDN 抽奖失败!'
print('抽奖失败!')
class Notice:
@staticmethod
def sever() -> None:
r = requests.get(f'https://sctapi.ftqq.com/{SEVER_SCKEY}.send?title={TEXT}&desp={DESP}')
print(r.json())
@staticmethod
def wechat() -> None:
data = {
'msgtype': 'text',
'text': {
'content': DESP
}
}
headers = {'content-type': 'application/json'}
requests.post(url=WECHAT_URL, headers=headers, data=json.dumps(data))
@staticmethod
def ding() -> None:
timestamp = str(round(time.time() * 1000))
secret = DING_SECRET
secret_enc = secret.encode('utf-8')
string_to_sign = '{}\n{}'.format(timestamp, secret)
string_to_sign_enc = string_to_sign.encode('utf-8')
hmac_code = hmac.new(secret_enc, string_to_sign_enc, digestmod=hashlib.sha256).digest()
sign = urllib.parse.quote_plus(base64.b64encode(hmac_code))
headers = {'Content-Type': 'application/json'}
complete_url = DING_URL + '×tamp=' + timestamp + "&sign=" + sign
data = {
"text": {
"content": DESP
},
"msgtype": "text"
}
r = requests.post(url=complete_url, data=json.dumps(data), headers=headers)
print(r.json())
def run() -> None:
c = CSDN()
n = Notice()
c.csdn_sign_in()
if IF_LUCK_DRAW == 'on':
c.csdn_luck_draw()
if IF_SEVER == 'on':
n.sever()
if IF_WECHAT == 'on':
n.wechat()
if IF_DING == 'on':
n.ding()
if __name__ == '__main__':
run()