-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathjd_unsubscribe.py
173 lines (147 loc) · 4.84 KB
/
jd_unsubscribe.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
#!/usr/local/bin/python
# -*- coding: utf-8 -*-
# @Time : 2021/8/20 11:41 上午
# @File : jd_unsubscribe.py
# @Project : jd_scripts
# @Cron : 50 23 * * *
# @Desc : 取消商品关注和店铺关注
import asyncio
import json
import re
import time
import aiohttp
from urllib.parse import urlencode
from config import USER_AGENT
from utils.process import process_start
from utils.console import println
from utils.jd_init import jd_init
from utils.logger import logger
@jd_init
class JdUnsubscribe:
headers = {
'user-agent': USER_AGENT,
'referer': 'https://wqs.jd.com/',
}
@logger.catch
async def request(self, session, path, params=None, method='GET'):
"""
请求服务器数据
:param method:
:param session:
:param path:
:param params:
:return:
"""
try:
if not params:
params = dict()
params['lastlogintime'] = 0
params['_'] = int(time.time()*1000)
params['sceneval'] = 2
params['g_login_type'] = 1
params['g_ty'] = 'ls'
params['callback'] = 'json'
url = f'https://wq.jd.com/{path}?' + urlencode(params)
if method == 'GET':
response = await session.get(url)
else:
response = await session.post(url)
text = await response.text()
temp = re.search('json\((.*)\);', text, re.S).group(1)
data = json.loads(temp)
return data
except Exception as e:
println('{}, 请求数据失败, {}'.format(self.account, e.args))
return {
'iRet': '999'
}
@logger.catch
async def unsubscribe_goods(self, session):
"""
取关商品
:param session:
:return:
"""
max_page = 10
cur_page = 1
while True:
comm_id_list = []
if cur_page >= max_page:
break
params = {
'cp': 1,
'pageSize': 20,
'category': 0,
'promote': 0,
'cutPrice': 0,
'coupon': 0,
'stock': 0,
}
data = await self.request(session, '/fav/comm/FavCommQueryFilter', params)
if not data or data.get('iRet', '-1') != '0':
break
max_page = int(data.get('totalPage', '1'))
cur_page = int(data.get('cp', '1'))
for item in data.get('data', []):
comm_id_list.append(item.get('commId'))
if not comm_id_list:
break
cur_page += 1
await asyncio.sleep(2)
unfollow_path = '/fav/comm/FavCommBatchDel'
unfollow_params = {
'commId': ','.join(comm_id_list)
}
data = await self.request(session, unfollow_path, unfollow_params)
if data.get('iRet', '-1') == '0':
println('{}, 成功取关{}个商品!'.format(self.account, len(comm_id_list)))
else:
println(data)
await asyncio.sleep(2)
@logger.catch
async def unsubscribe_shops(self, session):
"""
取关店铺
:param session:
:return:
"""
max_page = 10
cur_page = 1
while True:
shop_id_list = []
if cur_page > max_page:
break
params = {
'cp': 1,
'pageSize': 20
}
data = await self.request(session, '/fav/shop/QueryShopFavList', params)
if not data or data.get('iRet', '-1') != '0':
break
max_page = int(data.get('totalPage', '1'))
cur_page = int(data.get('cp', '1'))
for item in data.get('data', []):
shop_id_list.append(item.get('shopId'))
if not shop_id_list:
break
cur_page += 1
await asyncio.sleep(1.5)
unfollow_path = '/fav/shop/batchunfollow'
unfollow_params = {
'shopId': ','.join(shop_id_list)
}
data = await self.request(session, unfollow_path, unfollow_params)
if data.get('iRet', '-1') == '0':
println('{}, 成功取关{}个店铺!'.format(self.account, len(shop_id_list)))
else:
println(data)
await asyncio.sleep(1)
async def run(self):
"""
:return:
"""
async with aiohttp.ClientSession(cookies=self.cookies, headers=self.headers) as session:
await self.unsubscribe_goods(session)
await self.unsubscribe_shops(session)
if __name__ == '__main__':
process_start(JdUnsubscribe, '取消商品关注和店铺关注')