This repository has been archived by the owner on Aug 4, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 28
/
modian.py
216 lines (192 loc) · 7.72 KB
/
modian.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
# -*- coding: utf-8 -*-
import setting
import requests
# import json
# 消去https请求的不安全warning
# import urllib3
# urllib3.disable_warnings(urllib3.exceptions.InsecureRequestWarning)
import urllib
import hashlib
import time
from CQLog import INFO, WARN
# -------------------------------
# init
header = {'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_6) Appl\
eWebKit/537.36 (KHTML, like Gecko) Chrome/60.0.3112.90 Safari/537.36'}
# 计算签名
def getSign(ret):
# 将字典按键升序排列,返回一个元组tuple
tuple = sorted(ret.items(), key=lambda e: e[0], reverse=False)
md5_string = urllib.parse.urlencode(tuple).encode(encoding='utf_8', errors='strict')
md5_string += b'&p=das41aq6'
# md5计算 & 十六进制转化 & 根据规则从第6位开始取16位
sign = hashlib.md5(md5_string).hexdigest()[5: 21]
return sign
# page从1开始,每页最多20条数据
# 项目订单查询
def getOrders(pro_id, page):
url = 'https://wds.modian.com/api/project/orders'
form = {
'page': page,
'pro_id': pro_id
}
sign = getSign(form)
form['sign'] = sign
response = requests.post(url, form, headers=header, timeout=5).json()
return response
# 20180613API更新,增加自定义订单排序
# sort_by = 1 按支付时间倒序
# sort_by = 0 按下单时间倒序
def sorted_orders(pro_id, page, sort_by=1):
url = 'https://wds.modian.com/api/project/sorted_orders'
form = {
'page': page,
'pro_id': pro_id,
'sort_by': sort_by
}
sign = getSign(form)
form['sign'] = sign
response = requests.post(url, form, headers=header, timeout=5).json()
return response
# 项目聚聚榜查询
# type = 1 聚聚榜
# type = 2 打卡榜
def getRankings(pro_id, type, page):
url = 'https://wds.modian.com/api/project/rankings'
form = {
'page': page,
'pro_id': pro_id,
'type': type
}
sign = getSign(form)
form['sign'] = sign
response = requests.post(url, form, headers=header, timeout=5).json()
return response
# 项目筹款结果查询
# 查询多个项目用逗号分隔,如getDetail(10250,10280)
def getDetail(*pro_id):
# 将pro_id(一个元组)中int元素转为str元素,用逗号拼接成字符串
pro_id_str = ','.join(map(str, pro_id))
url = 'https://wds.modian.com/api/project/detail'
form = {
'pro_id': pro_id_str
}
sign = getSign(form)
form['sign'] = sign
response = requests.post(url, form, headers=header, timeout=5).json()
return response
# init end
# ---------------------------------
# func
def md_init(proid_array):
array = []
for id in proid_array:
dict = {}
dict['pro_id'] = int(id)
id_url = 'https://zhongchou.modian.com/item/%d' % int(id)
try:
dict['url_short'] = setting.get_short_url(id_url)
except Exception as e:
dict['url_short'] = 'https://m.modian.com/project/%d' % int(id)
dict['url_long'] = 'https://m.modian.com/project/%d' % int(id)
detail = getDetail(id)
dict['name'] = detail['data'][0]['pro_name']
array.append(dict)
return array
# rank
def rank(type):
msg_array = []
err = False
err_msg = '返回rank错误\n'
pro_id_array = md_init(setting.pro_id())
for pro_id_dict in pro_id_array:
msg = ''
detail = getDetail(pro_id_dict['pro_id'])
# type=1:总额榜
if type == 1:
msg = msg + pro_id_dict['name'] + '·聚聚榜TOP20\n' + '------------\n'
dic = getRankings(pro_id_dict['pro_id'], 1, 1)
if int(dic['status']) == 0:
for data in dic['data']:
msg = msg + '【第' + str(data['rank']) + '名】: ' +data['nickname'] + '支持了' + str(data['backer_money']) + '元\n'
elif int(dic['status']) == 2:
err = True
err_msg += dic['message']
elif type == 2:
msg = msg + pro_id_dict['name'] + '·打卡榜TOP20\n' + '------------\n'
dic = getRankings(pro_id_dict['pro_id'], 2, 1)
if int(dic['status']) == 0:
for data in dic['data']:
msg = msg + '【第' + str(data['rank']) + '名】: ' +data['nickname'] + '已打卡' + str(data['support_days']) + '天\n'
elif int(dic['status']) == 2:
err = True
err_msg += dic['message']
msg = msg + '------------\n【摩点】:' + pro_id_dict['url_short'] + '\n目前集资进度:¥' +\
str(detail['data'][0]['already_raised']) + '\n目标:¥' +\
detail['data'][0]['goal']
msg_array.append(msg)
if err is True:
WARN("modian rank error", err_msg)
return err_msg
elif err is False:
return msg_array
def result(pro_id_array):
msg_array = []
for pro_id in pro_id_array:
response = getDetail(pro_id)
msg = ''
msg += response['data'][0]['pro_name'] + '\n' + '项目进度:' + str(response['data'][0]['already_raised']) + '/' + response['data'][0]['goal'] + '\n结束时间:' + response['data'][0]['end_time']
msg_array.append(msg)
return msg_array
def newOrder(stamp10, secondsDelay):
msgDict_array = []
pro_id_array = md_init(setting.pro_id())
for pro_id_dict in pro_id_array:
newOrders = []
# 获取一次订单信息,返回一个dictionary
orderDict = sorted_orders(pro_id_dict['pro_id'], 1)
# 查询失败则返回错误信息
if int(orderDict['status']) == 2:
WARN("modian newOrder error", "获取订单错误", orderDict['message'])
return orderDict['message']
# 20180319,防止空订单,循环查询五次
retry = 0
while not len(orderDict['data']):
INFO("modian newOrder info", "疑似空订单")
orderDict = sorted_orders(pro_id_dict['pro_id'], 1)
retry += 1
if retry > 5:
INFO("modian newOrder info", "订单5次为空,判断为无人集资")
break
# 查询成功,遍历data
for data in orderDict['data']:
pay_success_time = data['pay_success_time']
# 将字符串时间转换为unix时间戳
data['pay_success_time'] = int(time.mktime(time.strptime(pay_success_time, '%Y-%m-%d %H:%M:%S')))
# 筛选订单时间在查询时间前的设定时间段之间的订单
if data['pay_success_time'] >= stamp10 - secondsDelay and data['pay_success_time'] < stamp10:
newOrders.append(data)
msgDict = {}
# 有新订单
if newOrders:
# 获取项目信息
detail = getDetail(pro_id_dict['pro_id'])
# 查询失败则返回错误信息
if int(detail['status']) == 2:
WARN("modian newOrder error", "获取项目信息错误", detail['message'])
return detail['message']
# 查询成功,初始化消息
msgDict['msg'] = []
msg = ''
for newOrder in newOrders:
msg = "ID: " + newOrder['nickname'] +\
" 的聚聚刚刚在【" + pro_id_dict['name'] + "】中支持了 ¥" +\
str(newOrder['backer_money']) + '\n' + "感谢这位聚聚对" +\
setting.idol_name() + "的支持" + '\n'
msgDict['msg'].append(msg)
msgDict['end'] = '【摩点】:' + pro_id_dict['url_short'] + '\n目前集资进度:¥' +\
str(detail['data'][0]['already_raised']) + '\n目标:¥' +\
str(detail['data'][0]['goal'])
msgDict_array.append(msgDict)
msgDict_array.append(orderDict)
return msgDict_array