-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathjd_wonderful_lottery.py
208 lines (187 loc) · 7.54 KB
/
jd_wonderful_lottery.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
#!/usr/local/bin/python
# -*- coding: utf-8 -*-
# @Time : 2021/8/27 10:56 上午
# @File : jd_wonderful_lottery.py
# @Project : jd_scripts
# @Cron : 44 4,5 * * *
# @Desc : 京东APP->签到领豆->边玩边赚->每日抽奖
import asyncio
import json
import random
import re
import aiohttp
from urllib.parse import urlencode
from utils.jd_init import jd_init
from utils.console import println
from config import USER_AGENT
from db.model import Code
from utils.process import process_start, get_code_list
CODE_KEY = 'jd_wonderful_lottery'
def random_uuid():
"""
:return:
"""
s = '01234567890123456789'
return ''.join(random.sample(s, 16)) + '-' + ''.join(random.sample(s, 16))
@jd_init
class JdWonderfulLottery:
"""
京东精彩
"""
headers = {
'origin': 'https://jingcai-h5.jd.com',
'user-agent': 'jdapp;' + USER_AGENT,
'lop-dn': 'jingcai.jd.com',
'accept': 'application/json, text/plain, */*',
'appparams': '{"appid":158,"ticket_type":"m"}',
'content-type': 'application/x-www-form-urlencoded',
'referer': 'https://jingcai-h5.jd.com/index.html'
}
activity_code = "1419494729103441920"
async def request(self, session, path, body=None, method='POST'):
"""
请求服务器数据
:return:
"""
try:
if not body:
body = {}
url = 'https://lop-proxy.jd.com/' + path
if method == 'POST':
response = await session.post(url, json=body)
else:
response = await session.get(url, json=body)
text = await response.text()
data = json.loads(text)
return data
except Exception as e:
println('{}, 请求服务器数据失败, {}'.format(self.account, e.args))
return {
'success': False
}
async def do_tasks(self, session):
"""
做任务
:return:
"""
res = await self.request(session, '/luckdraw/queryMissionList', [{
"userNo": "$cooMrdGatewayUid$",
"activity_code": self.activity_code
}])
if not res.get('success'):
println('{}, 获取任务列表失败!'.format(self.account))
return
task_list = res['content']['missionList']
for task in task_list:
if task['status'] == 10:
println('{}, 今日完成任务:{}!'.format(self.account, task['title']))
continue
if task['status'] == 11:
for no in task['getRewardNos']:
body = [{
"activity_code": self.activity_code,
"userNo": "$cooMrdGatewayUid$",
"getCode": no
}]
res = await self.request(session, '/luckDraw/getDrawChance', body)
if res.get('success'):
println('{}, 成功领取一次抽奖机会!'.format(self.account))
break
continue
if '邀请' in task['title']: # 邀请好友
code = task['missionNo']
println('{}, 助力码:{}'.format(self.account, code))
Code.insert_code(code_val=code, code_key=CODE_KEY, sort=self.sort, account=self.account)
continue
for i in range(task['completeNum'], task['totalNum']):
body = [{
"activity_code": self.activity_code,
"userNo": "$cooMrdGatewayUid$",
"missionNo": task['missionNo'],
"params": task['params']
}]
res = await self.request(session, '/luckDraw/completeMission', body)
if res.get('success'):
println('{}, 完成任务:{}-{}'.format(self.account, task['title'], i + 1))
await asyncio.sleep(1)
async def run_help(self):
"""
助力好友
:return:
"""
async with aiohttp.ClientSession(headers=self.headers, cookies=self.cookies) as session:
item_list = Code.get_code_list(code_key=CODE_KEY)
item_list.extend(get_code_list(code_key=CODE_KEY))
for item in item_list:
account, code = item.get('account'), item.get('code')
if account == self.account:
continue
res = await self.request(session, '/luckdraw/helpFriend', [{
"userNo": "$cooMrdGatewayUid$",
"missionNo": code
}])
if res.get('success'):
println('{}, 成功助力好友:{}'.format(self.account, account))
else:
println('{}, 无法助力好友:{}'.format(self.account, account))
async def lottery(self, session):
"""
抽奖
:return:
"""
while True:
res = await self.request(session, '/luckdraw/draw', [{
"userNo": "$cooMrdGatewayUid$",
"activity_code": self.activity_code
}])
if res.get('success'):
println('{}, 抽奖成功'.format(self.account))
else:
break
await asyncio.sleep(1)
async def get_activate_code(self, session):
"""
:return:
"""
try:
params = {
'functionId': 'qryCompositeMaterials',
'body': json.dumps({"qryParam": "[{\"type\":\"advertGroup\",\"id\":\"03744379\","
"\"mapTo\":\"moreActivity\"},{\"type\":\"advertGroup\","
"\"id\":\"04039687\",\"mapTo\":\"atmosphere\"},"
"{\"type\":\"advertGroup\",\"id\":\"04030152\","
"\"mapTo\":\"promotion\"},{\"type\":\"advertGroup\","
"\"id\":\"04395256\",\"mapTo\":\"temporary\"}]",
"activityId": "00360210", "pageId": "666370", "previewTime": "", "reqSrc": ""}),
'client': 'wh5',
'clientVersion': '1.0.0',
'uuid': random_uuid()
}
url = 'https://api.m.jd.com/client.action?' + urlencode(params)
response = await session.post(url=url)
text = await response.text()
data = json.loads(text)
if data.get('code') != '0':
return None
item_list = data['data']['moreActivity']['list']
for item in item_list:
if '每日抽奖' in item['name']:
code = re.search('activityCode=(.*)&', item['link']).group(1)
return code
return None
except Exception as e:
println('{}, 获取活动ID失败, {}'.format(self.account, e.args))
return None
async def run(self):
"""
:return:
"""
async with aiohttp.ClientSession(headers=self.headers, cookies=self.cookies) as session:
self.activity_code = await self.get_activate_code(session)
if not self.activity_code:
println('{}, 获取活动ID失败, 退出程序!'.format(self.account))
return
await self.do_tasks(session) # 做任务
await self.lottery(session) # 抽奖
if __name__ == '__main__':
process_start(JdWonderfulLottery, '精彩-每日抽奖', code_key=CODE_KEY)