-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathjd_health.py
383 lines (327 loc) · 13.4 KB
/
jd_health.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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
#!/usr/local/bin/python
# -*- coding: utf-8 -*-
# @Time : 2021/8/4 4:12 下午
# @File : jd_health.py
# @Project : jd_scripts
# @Cron : 35 6,16 * * *
# @Desc : 京东APP->我的->签到领豆->边玩边赚->东东健康社区
import asyncio
import aiohttp
import json
from db.model import Code
from urllib.parse import urlencode
from utils.jd_init import jd_init
from utils.console import println
from utils.process import process_start, get_code_list
from config import USER_AGENT
ERRCODE_DEFAULT = 9999
# 东东健康社区
CODE_JD_HEALTH = 'jd_health'
@jd_init
class JdHealth:
"""
东东健康社区
"""
energy = 0 # 当前能量
headers = {
'user-agent': 'jdapp;' + USER_AGENT,
'content-type': 'application/x-www-form-urlencoded',
'origin': 'https://h5.m.jd.com',
'referer': 'https://h5.m.jd.com/',
}
async def request(self, session, function_id, body=None, method='POST'):
"""
请求数据
:param session:
:param function_id:
:param body:
:param method:
:return:
"""
try:
if not body:
body = {}
params = {
'functionId': function_id,
'client': 'wh5',
'body': json.dumps(body),
'clientVersion': '1.0.0',
'uuid': '0'
}
url = 'https://api.m.jd.com/?' + urlencode(params)
if method == 'POST':
response = await session.post(url=url)
else:
response = await session.get(url=url)
text = await response.text()
data = json.loads(text)
if data['code'] != 0:
return {
'bizCode': ERRCODE_DEFAULT,
'bizMsg': data['msg']
}
else:
return data['data']
except Exception as e:
println('{}, 获取服务器数据失败:{}!'.format(self.account, e.args))
return {
'bizCode': ERRCODE_DEFAULT,
'bizMsg': '获取服务器数据失败!'
}
async def get_task_list(self, session):
"""
获取任务列表
:return:
"""
function_id = 'jdhealth_getTaskDetail'
body = {"buildingId": "", "taskId": "", "channelId": 1}
res = await self.request(session, function_id, body)
return res.get('result', dict()).get('taskVos', list())
async def clockIn(self, session, task):
"""
早起打卡
:param session:
:param task:
:return:
"""
function_id = 'jdhealth_collectScore'
task_token = task['threeMealInfoVos'][0]['taskToken']
body = {"taskToken": task_token, "taskId": task['taskId'], "actionType": 0}
res = await self.request(session, function_id, body)
if res.get('bizCode', ERRCODE_DEFAULT) == 0:
println('{}, 打卡成功!'.format(self.account))
else:
println('{}, 打卡失败, {}'.format(self.account, res.get('bizMsg', '原因未知')))
async def receive_task(self, session, task_id, task_token):
"""
领取任务
:param session:
:param task_id:
:param task_token:
:return:
"""
function_id = 'jdhealth_collectScore'
body = {
'taskId': task_id,
'taskToken': task_token,
"actionType": 1,
}
return await self.request(session, function_id, body)
async def receive_task_award(self, session, task_id, task_token):
"""
领取任务奖励
:param session:
:param task_id:
:param task_token:
:return:
"""
function_id = 'jdhealth_collectScore'
body = {
'taskId': task_id,
'taskToken': task_token,
"actionType": 0,
}
return await self.request(session, function_id, body)
async def browser_task(self, session, task, item_list_key='shoppingActivityVos', task_name=None):
"""
浏览商品
:param task_name:
:param item_list_key:
:param session:
:param task:
:return:
"""
if not task_name:
task_name = task.get('taskName')
task_id = task['taskId']
item_list = task[item_list_key]
times, max_times = task['times'], task['maxTimes']
if times == 0:
times += 1
for item in item_list:
if times > max_times:
break
res = await self.receive_task(session, task_id, item['taskToken'])
println('{}, 领取任务《{}》{}/{}, {}!'.format(self.account, task_name, times, max_times,
res.get('bizMsg')))
if res.get('bizCode', ERRCODE_DEFAULT) == 105:
break
times += 1
await asyncio.sleep(1)
timeout = task.get('waitDuration', 1)
if timeout < 1:
timeout = 1
println('{}, {}秒后去领取任务《{}》奖励...'.format(self.account, timeout, task_name))
await asyncio.sleep(timeout)
times, max_times = task['times'], task['maxTimes']
if times == 0:
times += 1
for item in item_list:
if times > max_times:
break
res = await self.receive_task_award(session, task_id, item['taskToken'])
println('{}, 领取任务《{}》{}/{}奖励, {}!'.format(self.account, task_name,
times, max_times, res.get('bizMsg')))
times += 1
if res.get('bizCode', ERRCODE_DEFAULT) == 105:
break
await asyncio.sleep(1)
async def health_a_bit(self, session):
"""
健康一下
:return:
"""
data = await self.request(session, 'jdhealth_getTaskDetail', {"buildingId": "", "taskId": 22, "channelId": 1})
status = data.get('result', dict()).get('taskVos',
dict())[-1].get('status', 0)
if status == 2:
println('{}, 健康一下已完成!'.format(self.account))
return
task_token = data.get('result', dict()).get('taskVos',
dict())[-1].get('shoppingActivityVos')[-1].get('taskToken', None)
if not task_token:
println('{}, 无法进行健康一下!'.format(self.account))
return
println('{}, 正在进行健康一下, 等待6秒!'.format(self.account))
await self.request(session, 'jdhealth_collectScore', {"taskToken": task_token, "taskId": 22, "actionType": 1})
await asyncio.sleep(6)
res = await self.request(session, 'jdhealth_collectScore',
{"taskToken": task_token, "taskId": 22, "actionType": 0})
if res.get('bizCode', ERRCODE_DEFAULT) == 0:
println('{}, 完成健康一下!'.format(self.account))
else:
println('{}, 无法完成健康一下!'.format(self.account))
async def do_task_list(self, session, task_list):
"""
做任务
:param task_list:
:param session:
:return:
"""
for task in task_list:
task_type, task_name = task.get('taskType'), task.get('taskName')
if task['status'] == 2:
println('{}, 任务《{}》已做完!'.format(self.account, task_name))
continue
if task_type == 19: # 下单任务
println('{}, 跳过任务:《{}》!'.format(self.account, task_name))
continue
elif task_type == 10: # 早起打卡
await self.clockIn(session, task)
elif task_type == 9: # 逛商品
await self.browser_task(session, task, 'shoppingActivityVos')
elif task_type == 1: # 关注店铺
await self.browser_task(session, task, 'followShopVo')
elif task_type in [3, 8, 25]: # 浏览产品
if 'shoppingActivityVos' in task:
await self.browser_task(session, task, 'shoppingActivityVos')
else:
await self.browser_task(session, task, 'productInfoVos')
else:
println(task_type, task_name)
async def sign(self, session):
"""
签到
:param session:
:return:
"""
data = await self.request(session, 'jdhealth_getTaskDetail',
{"buildingId": "", "taskId": 16, "channelId": 1})
if data.get('bizCode', ERRCODE_DEFAULT) != 0:
println('{}, 获取签到数据失败!'.format(self.account))
return
status = data['result']['taskVos'][-1]['status']
if status == 2:
println('{}, 今日已签到!'.format(self.account))
return
task_token = data['result']['taskVos'][-1]['simpleRecordInfoVo']['taskToken']
sign_params = {"taskToken": task_token, "taskId": 16, "actionType": "0"}
res = await self.request(session, 'jdhealth_collectScore', sign_params)
if res.get('bizCode', ERRCODE_DEFAULT) == 0:
println('{}, 签到成功!'.format(self.account))
else:
println('{}, 签到失败!'.format(self.account))
async def collect_health_energy(self, session):
res = await self.request(session, 'jdhealth_collectProduceScore')
if res.get('bizCode', ERRCODE_DEFAULT) != 0:
println('{}, 收能量失败!'.format(self.account))
else:
self.energy = int(res.get('result', dict()).get('userScore', '0'))
println('{}, 成功收取能量{}, 当前能量:{}!'.format(self.account,
res.get('result', dict()).get('produceScore'),
self.energy))
async def exchange_bean(self, session):
"""
兑换京豆
:param session:
:return:
"""
res = await self.request(session, 'jdhealth_getCommodities')
if res.get('bizCode', ERRCODE_DEFAULT) != 0:
println('{}, 无法获取可兑换物品列表!'.format(self.account))
return
await asyncio.sleep(1)
item_list = res.get('result', dict()).get('jBeans')
for i in range(len(item_list)):
item = item_list[len(item_list) - 1 - i]
if self.energy < int(item['exchangePoints']):
continue
res = await self.request(session, 'jdhealth_exchange',
{"commodityType": item['type'], "commodityId": item['id']})
if res.get('bizCode', ERRCODE_DEFAULT) == 0:
println('{}, 成功兑换{}京豆!'.format(self.account, item['title']))
else:
println('{}, 无法兑换{}京豆, {}!'.format(self.account, item['title'], res.get('bizMsg', '原因未知')))
if res.get('bizCode', ERRCODE_DEFAULT) == -6055: # 到达今日兑换次数上限,不能再兑换哦~
break
await asyncio.sleep(1)
async def get_share_code(self, session):
"""
获取助力码
:param session:
:return:
"""
res = await self.request(session, 'jdhealth_getTaskDetail', {"buildingId": "", "taskId": 6, "channelId": 1})
if res.get('bizCode', ERRCODE_DEFAULT) != 0:
println('{}, 无法获取助力码!'.format(self.account))
return
code = res['result']['taskVos'][0]['assistTaskDetailVo']['taskToken']
println('{}, 助力码:{}'.format(self.account, code))
Code.insert_code(code_key=CODE_JD_HEALTH, code_val=code, account=self.account, sort=self.sort)
async def help(self, session):
"""
:param session:
:return:
"""
item_list = Code.get_code_list(CODE_JD_HEALTH)
item_list.extend(get_code_list(CODE_JD_HEALTH))
for item in item_list:
account, code = item.get('account'), item.get('code')
res = await self.request(session, 'jdhealth_collectScore',
{"taskToken": code, "taskId": "6", "actionType": 0})
if res.get('bizCode', ERRCODE_DEFAULT) == 0:
println('{}, 成功助力好友{}!'.format(self.account, account))
else:
println('{}, 无法助力好友:{}, {}'.format(self.account, account, res.get('bizMsg', '原因未知')))
async def run(self):
"""
:return:
"""
async with aiohttp.ClientSession(headers=self.headers, cookies=self.cookies) as session:
await self.sign(session)
for i in range(3):
task_list = await self.get_task_list(session)
await self.do_task_list(session, task_list)
await self.get_share_code(session)
await self.health_a_bit(session)
await self.collect_health_energy(session)
await self.exchange_bean(session)
async def run_help(self):
"""
助力入口
:return:
"""
async with aiohttp.ClientSession(headers=self.headers, cookies=self.cookies) as session:
await self.help(session)
if __name__ == '__main__':
process_start(JdHealth, '东东健康社区', code_key=CODE_JD_HEALTH)