-
Notifications
You must be signed in to change notification settings - Fork 2
/
ck_pupu_coupon.py
153 lines (129 loc) · 4.88 KB
/
ck_pupu_coupon.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
# -*- coding: utf-8 -*-
"""
cron: 0 7,9,13,15,19 * * *
new Env('朴朴领券');
微信登录朴朴app
找到请求https://cauth.pupuapi.com/clientauth/user/society/wechat/login?user_society_type=11
在json响应里有refresh_token
coupon_center.enabled 是否启用领券(默认true)
"""
import asyncio
import sys
from traceback import format_exc
from pupu_api import Client as PClient
from pupu_types import *
from utils import aio_randomSleep, check, log
import json_codec
assert sys.version_info >= (3, 9)
@dataclass
class PCouponCenterItem:
discount_id: str
discount_group_id: str
condition_amount: int
discount_amount: int
is_finished: bool = True
received: int = 0
received_limit: int = 0
@property
def is_received(self):
"""是否已领取"""
return self.received > 0
@property
def can_received(self):
"""是否可以领取"""
return not self.is_finished and self.received < self.received_limit
class PUPU:
__slots__ = (
"check_item",
"device_id",
"refresh_token",
)
def __init__(self, check_item) -> None:
self.check_item: dict = check_item
async def main(self):
msg: list[str] = []
try:
self.device_id = self.check_item.get("device_id", "")
self.refresh_token = self.check_item.get("refresh_token", "")
if not self.device_id:
raise SystemExit("device_id 配置有误")
if not self.refresh_token:
raise SystemExit("refresh_token 配置有误")
coupon_center = self.check_item.get("coupon_center", {})
if not coupon_center.get("enabled", True):
raise SystemExit("没有启用")
msg += await self.CollectCoupons()
except Exception:
log(f"失败: 请检查接口 {format_exc()}", msg)
return "\n".join(msg)
async def _ReceiveCoupon(self, api: PClient, coupon: PCouponCenterItem):
try:
obj = await api._SendRequest(
HttpMethod.kPost,
"https://j1.pupuapi.com/client/coupon/entity",
ClientType.kWeb,
json={
"discount": coupon.discount_id,
"discount_group": coupon.discount_group_id,
"place_id": api.receiver.place_id,
"store_id": api.receiver.store_id,
"time_type": 1,
},
)
if obj["errcode"] == 0:
# 领取成功
return
else:
return ApiResults.Error(obj)
except:
return ApiResults.Exception()
async def CollectCoupons(self):
msg: list[str] = []
try:
async with PClient(self.device_id, self.refresh_token) as api:
result = await api.InitializeToken(
self.check_item.get("addr_filter"), force_update_receiver=False
)
if isinstance(result, ApiResults.Error):
if api.nickname:
log(f"账号: {api.nickname}", msg)
log(result, msg)
return msg
log(f"账号: {api.nickname}", msg)
obj = await api._SendRequest(
HttpMethod.kGet,
"https://j1.pupuapi.com/client/coupon",
ClientType.kWeb,
params={"store_id": api.receiver.store_id, "type": 1},
)
if obj["errcode"] == 0:
data = obj["data"]
items = json_codec.decode(
data.get("items") or [], list[PCouponCenterItem]
)
if not any(item.can_received for item in items):
log("没有优惠券,领取失败", msg)
exit() # 目前没必要执行后续的操作
return msg
for coupon in items:
result, _ = await asyncio.gather(
self._ReceiveCoupon(api, coupon), aio_randomSleep()
)
if isinstance(result, ApiResults.Error):
log(result, msg)
else:
log(
f"成功领取: 满{coupon.condition_amount/100}减{coupon.discount_amount/100}元",
msg,
)
else:
log(ApiResults.Error(obj), msg)
except Exception:
log(ApiResults.Exception(), msg)
finally:
return msg
@check(run_script_name="朴朴领券", run_script_expression="pupu")
def main(*args, **kwargs):
return asyncio.run(PUPU(check_item=kwargs.get("value")).main())
if __name__ == "__main__":
main()