check_order_new.py 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780
  1. #!/usr/bin/env python
  2. # -*- coding: utf-8 -*-
  3. """
  4. __title__ = '每日凌晨空闲时检查本地数据库中的订单数据是否和平台昨天总订单一致'
  5. @Time : 2020/9/26 19:44
  6. @Author : Kenny-PC
  7. @Software: PyCharm
  8. # code is far away from bugs with the god animal protecting
  9. I love animals. They taste delicious.
  10. ┏┓ ┏┓
  11. ┏┛┻━━━┛┻┓
  12. ┃ ☃ ┃
  13. ┃ ┳┛ ┗┳ ┃
  14. ┃ ┻ ┃
  15. ┗━┓ ┏━┛
  16. ┃ ┗━━━┓
  17. ┃ 神兽保佑 ┣┓
  18. ┃ 永无BUG! ┏┛
  19. ┗┓┓┏━┳┓┏┛
  20. ┃┫┫ ┃┫┫
  21. ┗┻┛ ┗┻┛
  22. """
  23. import datetime
  24. import hashlib
  25. import math
  26. import time
  27. from concurrent.futures import ProcessPoolExecutor
  28. from urllib import parse
  29. import requests
  30. import random
  31. import traceback
  32. from apscheduler.schedulers.blocking import BlockingScheduler
  33. import account_list_zwg as al
  34. from MySQLConnection import MySQLConnection
  35. from util import date_util
  36. from util import platform_util
  37. def md5value(s):
  38. md5 = hashlib.md5()
  39. md5.update(s.encode("utf-8"))
  40. return md5.hexdigest()
  41. ##《1》阅文
  42. def get_yuewen_order(st, et):
  43. start_exec_seconds = date_util.getCurrentSecondTime()
  44. total_order_list = ()
  45. account_list = platform_util.get_yuewen_account_list()
  46. executor = ProcessPoolExecutor(max_workers=5)
  47. futures = []
  48. for account in account_list:
  49. future = executor.submit(get_yuewen_order_task, st, et, account)
  50. futures.append(future)
  51. executor.shutdown(True)
  52. for future in futures:
  53. order_list = future.result()
  54. if len(order_list) > 0:
  55. total_order_list = order_list + total_order_list
  56. print('阅文订单数量:', len(total_order_list), '执行时长(秒):', date_util.getCurrentSecondTime() - start_exec_seconds)
  57. return total_order_list
  58. def get_yuewen_order_task(st, et, account):
  59. order_list = ()
  60. url = 'https://open.yuewen.com/cpapi/wxRecharge/querychargelog'
  61. version = 1
  62. start_time = st
  63. email = account[0]
  64. appsecert = account[1]
  65. for i in range((et - st) // 86400 + 1):
  66. end_time = min(start_time + 86400, et)
  67. timestamp = int(time.time())
  68. s = ''
  69. page = 1
  70. order_status = 2
  71. data = {
  72. 'email': email,
  73. 'version': version,
  74. 'timestamp': timestamp,
  75. 'start_time': start_time,
  76. 'end_time': end_time,
  77. 'page': page,
  78. 'order_status': order_status
  79. # 'last_min_id':last_min_id,
  80. # 'last_max_id':last_max_id,
  81. # 'total_count':total_count,
  82. # 'last_page':last_page
  83. }
  84. sorted_data = sorted(data.items())
  85. for k, v in sorted_data:
  86. s = s + str(k) + str(v)
  87. sign = md5value(appsecert + s).upper()
  88. data1 = {
  89. 'email': email,
  90. 'version': version,
  91. 'timestamp': timestamp,
  92. 'start_time': start_time,
  93. 'end_time': end_time,
  94. 'page': page,
  95. 'order_status': order_status,
  96. 'sign': sign
  97. }
  98. list1 = requests.get(url=url, params=data1)
  99. ## 此接口有调用频率限制,相同查询条件每分钟仅能请求一次
  100. ## exception: list1.json() {'code': 10408, 'msg': '调用频率超限'}
  101. if list1.json()['code'] != 0:
  102. print('阅文查询充值接口异常:', list1.json())
  103. break
  104. total_count = list1.json()['data']['total_count']
  105. last_min_id = list1.json()['data']['min_id']
  106. last_max_id = list1.json()['data']['max_id']
  107. last_page = list1.json()['data']['page']
  108. if total_count > 0:
  109. for x in list1.json()['data']['list']:
  110. y = {}
  111. dtime = datetime.datetime.strptime(x['order_time'], "%Y-%m-%d %H:%M:%S")
  112. y['date'] = ((int(time.mktime(dtime.timetuple())) + 8 * 3600) // 86400) * 86400 - 8 * 3600
  113. y['platform'] = '阅文'
  114. y['channel'] = x['app_name']
  115. y['from_novel'] = x['book_name']
  116. y['user_id'] = x['openid']
  117. y['stage'] = ''
  118. y['channel_id'] = 0
  119. y['order_time'] = x['order_time']
  120. y['amount'] = x['amount']
  121. y['reg_time'] = x['reg_time']
  122. y['order_id'] = x['order_id']
  123. y = sorted(y.items(), key=lambda item: item[0])
  124. y = dict(y)
  125. y = tuple(y.values())
  126. order_list = order_list + ((y),)
  127. if total_count > 100:
  128. page_while_count = math.ceil(total_count / 100) + 1
  129. if page_while_count > 2:
  130. sleep_seconds = random.randint(60, 70)
  131. print('阅文获取订单数据线程休眠', sleep_seconds,'秒,因为该接口有一分钟的限制')
  132. time.sleep(sleep_seconds)
  133. for page in range(2, page_while_count):
  134. timestamp = int(time.time())
  135. data = {
  136. 'email': email,
  137. 'version': version,
  138. 'timestamp': timestamp,
  139. 'start_time': start_time,
  140. 'end_time': end_time,
  141. 'page': page,
  142. 'last_min_id': last_min_id,
  143. 'last_max_id': last_max_id,
  144. 'total_count': total_count,
  145. 'last_page': last_page,
  146. 'order_status': order_status
  147. }
  148. sorted_data = sorted(data.items())
  149. s1 = ''
  150. for k, v in sorted_data:
  151. s1 = s1 + str(k) + str(v)
  152. sign = md5value(appsecert + s1).upper()
  153. data2 = {
  154. 'email': email,
  155. 'version': version,
  156. 'timestamp': timestamp,
  157. 'start_time': start_time,
  158. 'end_time': end_time,
  159. 'page': page,
  160. 'last_min_id': last_min_id,
  161. 'last_max_id': last_max_id,
  162. 'total_count': total_count,
  163. 'last_page': last_page,
  164. 'order_status': order_status,
  165. 'sign': sign
  166. }
  167. list2 = requests.get(url=url, params=data2)
  168. if list2.json()['code'] != 0:
  169. print('阅文查询充值接口异常:', list2.json(), timestamp, int(time.time()))
  170. break
  171. for x in list2.json()['data']['list']:
  172. y = {}
  173. dtime = datetime.datetime.strptime(x['order_time'], "%Y-%m-%d %H:%M:%S")
  174. y['date'] = ((int(time.mktime(dtime.timetuple())) + 8 * 3600) // 86400) * 86400 - 8 * 3600
  175. y['platform'] = '阅文'
  176. y['channel'] = x['app_name']
  177. y['from_novel'] = x['book_name']
  178. y['user_id'] = x['openid']
  179. y['stage'] = ''
  180. y['channel_id'] = 0
  181. y['order_time'] = x['order_time']
  182. y['amount'] = x['amount']
  183. y['reg_time'] = x['reg_time']
  184. y['order_id'] = x['order_id']
  185. y = sorted(y.items(), key=lambda item: item[0])
  186. y = dict(y)
  187. y = tuple(y.values())
  188. order_list = order_list + ((y),)
  189. total_count = list2.json()['data']['total_count']
  190. last_min_id = list2.json()['data']['min_id']
  191. last_max_id = list2.json()['data']['max_id']
  192. last_page = list2.json()['data']['page']
  193. start_time = start_time + 86400
  194. return order_list
  195. ##《2》掌读
  196. def get_zhangdu_order(st, et):
  197. start_exec_seconds = date_util.getCurrentSecondTime()
  198. total_order_list = ()
  199. account_list = platform_util.get_zhangdu_account_list()
  200. executor = ProcessPoolExecutor(max_workers=5)
  201. futures = []
  202. for account in account_list:
  203. future = executor.submit(get_zhangdu_order_task, st, et, account)
  204. futures.append(future)
  205. executor.shutdown(True)
  206. for future in futures:
  207. order_list = future.result()
  208. if len(order_list) > 0:
  209. total_order_list = order_list + total_order_list
  210. print('掌读订单数量:', len(total_order_list), '执行时长(秒):', date_util.getCurrentSecondTime() - start_exec_seconds)
  211. return total_order_list
  212. def get_zhangdu_order_task(st, et, account):
  213. order_list = ()
  214. url = 'https://api.zhangdu520.com/channel/getorder'
  215. uid = account[0]
  216. appsecert = account[1]
  217. channel = account[2]
  218. timestamp = int(time.time())
  219. sign = md5value(str(uid) + '&' + appsecert + '&' + str(timestamp))
  220. starttime = st
  221. timespace = 90 * 3600 * 24
  222. endtime = min(et, st + timespace)
  223. for x in range((et - st) // timespace + 1): # 分时段
  224. if x > 0:
  225. print('掌读跨天数查询:', x)
  226. params = {
  227. 'uid': uid,
  228. 'timestamp': timestamp,
  229. 'sign': sign,
  230. 'starttime': starttime,
  231. 'endtime': endtime
  232. }
  233. list1 = requests.get(url=url, params=params)
  234. pageCount = list1.json()['data']['pageCount']
  235. if pageCount == 0:
  236. continue
  237. for page in range(1, pageCount + 1): # 分页
  238. params = {
  239. 'uid': uid,
  240. 'timestamp': timestamp,
  241. 'sign': sign,
  242. 'starttime': starttime,
  243. 'endtime': endtime,
  244. 'page': page
  245. }
  246. list2 = requests.get(url=url, params=params).json()
  247. if 'data' in list2.keys():
  248. for b in list2['data']['list']:
  249. if b['status'] == '1':
  250. c = {}
  251. c['amount'] = b['amount']
  252. c['channel_id'] = uid
  253. c['order_id'] = str(b['orderno'])
  254. c['order_time'] = b['ctime']
  255. c['user_id'] = b['openid']
  256. c['platform'] = '掌读'
  257. c['channel'] = channel
  258. c['reg_time'] = b['regtime']
  259. c['from_novel'] = ''
  260. c['stage'] = ''
  261. c['date'] = ((int(b['ctime']) + 8 * 3600) // 86400) * 86400 - 8 * 3600
  262. x = sorted(c.items(), key=lambda item: item[0])
  263. x = dict(x)
  264. x = tuple(x.values())
  265. order_list = order_list + ((x),)
  266. starttime = starttime + timespace
  267. endtime = min(et, starttime + timespace)
  268. return order_list
  269. ##《3》花生
  270. def get_huasheng_order(st, et):
  271. start_exec_seconds = date_util.getCurrentSecondTime()
  272. total_order_list = ()
  273. account_list = platform_util.get_huasheng_account_list()
  274. executor = ProcessPoolExecutor(max_workers=10)
  275. futures = []
  276. for account in account_list:
  277. url = 'https://vip.rlcps.cn/api/getMerchants'
  278. apiKey = str(account[0])
  279. apiSecurity = account[1]
  280. timestamp = str(int(time.time()))
  281. sign = md5value(apiKey + timestamp + apiSecurity).upper()
  282. params = {
  283. 'apiKey': apiKey,
  284. 'apiSecurity': apiSecurity,
  285. 'timestamp': timestamp,
  286. 'sign': sign
  287. }
  288. response_result_json = requests.post(url, params).json()
  289. if 'data' not in response_result_json.keys():
  290. print('花生账号【{apiKey}】本次请求数据为空,响应报文【{result}】'.format(apiKey=apiKey, result=response_result_json))
  291. continue
  292. for merchant in response_result_json['data']:
  293. future = executor.submit(get_huasheng_order_task, st, et, account, merchant)
  294. futures.append(future)
  295. executor.shutdown(True)
  296. for future in futures:
  297. order_list = future.result()
  298. if len(order_list) > 0:
  299. total_order_list = order_list + total_order_list
  300. print('花生订单数量:', len(total_order_list), '执行时长(秒):', date_util.getCurrentSecondTime() - start_exec_seconds)
  301. return total_order_list
  302. def get_huasheng_order_task(st, et, account, merchant):
  303. order_list = ()
  304. apiKey = str(account[0])
  305. apiSecurity = account[1]
  306. stage = account[2]
  307. timestamp = str(int(time.time()))
  308. order_url = 'https://vip.rlcps.cn/api/orderList'
  309. merchant_id = merchant['merchant_id']
  310. merchant_name = merchant['merchant_name']
  311. start_time = st
  312. for i in range((et - st) // 86400 + 1):
  313. page = 1
  314. limit = 500
  315. while True:
  316. date = time.strftime("%Y-%m-%d", time.localtime(start_time))
  317. sign = md5value(apiKey + date + str(merchant_id) + timestamp + apiSecurity).upper()
  318. order_params = {
  319. 'apiKey': apiKey,
  320. 'apiSecurity': apiSecurity,
  321. 'timestamp': timestamp,
  322. 'date': date,
  323. 'merchant_id': merchant_id,
  324. 'sign': sign,
  325. 'page': page,
  326. 'limit': limit
  327. }
  328. response_result_json = requests.post(order_url, order_params).json()
  329. if 'data' not in response_result_json.keys() or len(response_result_json['data']) == 0:
  330. print('花生账号【{key}】, 渠道【{merchant_id}:{merchant_name}】本次请求数据为空,响应报文【{result}】'
  331. .format(key=apiKey, merchant_id=merchant_id, merchant_name=merchant_name, result=response_result_json))
  332. total_count = response_result_json['count']
  333. order_item_list = response_result_json['data']
  334. for order_item in order_item_list:
  335. if order_item['order_status'] == 1:#1为已支付
  336. order = {}
  337. ##dtime = datetime.datetime.strptime(order_item['pay_at'],"%Y-%m-%d")
  338. ##order['date']= ((int(time.mktime(dtime.timetuple()))+8*3600)//86400)*86400-8*3600
  339. order['user_id'] = order_item['openid']
  340. order['order_id'] = order_item['trans_id']
  341. order['order_time'] = order_item['pay_at']
  342. order['reg_time'] = order_item['join_at']
  343. #TODO 花生的时间需要统一
  344. order['date'] = (start_time + 8 * 3600) // 86400 * 86400 - 8 * 3600
  345. order['channel'] = merchant_name
  346. order['channel_id'] = merchant_id
  347. order['platform'] = '花生'
  348. order['stage'] = stage
  349. order['from_novel'] = order_item['book_name']
  350. order['amount'] = order_item['amount']
  351. order = sorted(order.items(), key=lambda item: item[0])
  352. order = dict(order)
  353. order = tuple(order.values())
  354. order_list = order_list + ((order),)
  355. if int(page) >= math.ceil(total_count / int(limit)):
  356. break
  357. # print('花生账号【{key}】, 渠道【{merchant_id}:{merchant_name}】当前页【{page}】,本次查询订单数【{total_count}】,即将查询下一页'
  358. # .format(key=apiKey, merchant_id=merchant_id, merchant_name=merchant_name, page=page, total_count=total_count))
  359. page = page + 1
  360. start_time = start_time + 86400 #天数加1
  361. return order_list
  362. ##《4》掌中云
  363. def get_zzy_order(st, et):
  364. start_exec_seconds = date_util.getCurrentSecondTime()
  365. total_order_list = ()
  366. account_list = al.zzy_account_list
  367. # account_list = platform_util.get_zhangzhongyun_account_list()
  368. # account_list = [['1108701f1d6','0f9c0f8429d1a16a8a78c2306e7a4db3','清勇7月']]
  369. # account_list = [['1109295d56c','9bb955186597882ac473e86ba4576158','趣程20期']]
  370. executor = ProcessPoolExecutor(max_workers=5)
  371. futures = []
  372. for account in account_list:
  373. url = 'https://openapi.818tu.com/partners/channel/channels/list?'
  374. key = account[0]
  375. secert = account[1]
  376. sign = md5value(secert + 'key=' + key)
  377. params = 'key=' + key + '&sign=' + sign
  378. response_result_json = requests.get(url + params).json()# 获取子渠道列表
  379. if 'data' not in response_result_json.keys():
  380. print('掌中云账号【{key}】本次请求数据为空,响应报文【{result}】'.format(key=key, result=response_result_json))
  381. continue
  382. items = response_result_json['data']['items']
  383. for channel in items:
  384. # 获取channel_id 后逐个拉取历史orders
  385. future = executor.submit(get_zzy_order_task, st, et, account, channel)
  386. futures.append(future)
  387. executor.shutdown(True)
  388. for future in futures:
  389. order_list = future.result()
  390. if len(order_list) > 0:
  391. total_order_list = order_list + total_order_list
  392. print('掌中云订单数量:', len(total_order_list), '执行时长(秒):', date_util.getCurrentSecondTime() - start_exec_seconds)
  393. return total_order_list
  394. def get_zzy_order_task(st, et, account, channel):
  395. # 掌中云的时间格式比较特殊,转换下
  396. st = platform_util.get_zhangzhongyun_format_time(st)
  397. et = platform_util.get_zhangzhongyun_format_time(et)
  398. order_list = ()
  399. key = account[0]
  400. secert = account[1]
  401. stage = account[2]
  402. order_url = 'https://openapi.818tu.com/partners/channel/orders/list?'
  403. channel_id = channel['id']
  404. channel_name = channel['nickname']
  405. status = str(1)
  406. page = str(1)
  407. per_page = str(1000)
  408. get_time = st
  409. limit_time = et
  410. gte = parse.urlencode({'created_at[gte]': get_time}) #gte就是ge 大于等于开始时间
  411. lt = parse.urlencode({'created_at[lt]': limit_time}) #小于 结束时间
  412. while True:
  413. sign = md5value(secert + 'channel_id=' + str(channel_id) + '&created_at[gte]=' + get_time + '&created_at[lt]=' + limit_time + '&key=' + key + '&page=' + str(page) + '&per_page=' + per_page + '&status=' + status)
  414. params = 'channel_id=' + str(channel_id) + '&' + gte + '&' + lt + '&page=' + str(page) + '&per_page=' + per_page + '&status=' + status + '&key=' + key + '&sign=' + sign
  415. response_result_json = requests.get(order_url + params).json()
  416. if 'data' not in response_result_json.keys():
  417. print('掌中云账号【{key}】, 渠道【{channel_id}:{channel_name}】本次请求数据为空,响应报文【{result}】'
  418. .format(key=key, channel_id=channel_id, channel_name=channel_name, result=response_result_json))
  419. break
  420. total_count = response_result_json['data']['count'] #总数量
  421. order_item_list = response_result_json['data']['items'] #订单列表
  422. for order_item in order_item_list:
  423. order = {}
  424. order['user_id'] = str(order_item['member']['openid'])
  425. order['channel'] = channel_name
  426. order['reg_time'] = order_item['member']['created_at']
  427. order['channel_id'] = channel_id
  428. order['amount'] = round(order_item['price'] / 100, 2)
  429. order['order_id'] = str(order_item['id'])
  430. order['order_time'] = order_item['created_at']
  431. order['platform'] = '掌中云'
  432. order['stage'] = stage
  433. dtime = datetime.datetime.strptime(order_item['created_at'][0:10], "%Y-%m-%d")
  434. order['date'] = ((int(time.mktime(dtime.timetuple())) + 8 * 3600) // 86400) * 86400 - 8 * 3600
  435. if str(order_item['from_novel_id']) != 'None':
  436. order['from_novel'] = order_item['from_novel']['title']
  437. else:
  438. order['from_novel'] = 'None'
  439. x = sorted(order.items(), key=lambda item: item[0])
  440. x = dict(x)
  441. x = tuple(x.values())
  442. order_list = order_list + ((x),)
  443. if int(page) >= math.ceil(total_count / int(per_page)):
  444. break
  445. # print('掌中云账号【{key}】, 渠道【{channel_id}:{channel_name}】当前页【{page}】,本次查询订单数【{total_count}】,即将查询下一页'
  446. # .format(key=key, channel_id=channel_id, channel_name=channel_name, page=page, total_count=total_count))
  447. page = int(page) + 1
  448. return order_list
  449. ##《5》 悠书阁
  450. def get_ysg_order(st, et):
  451. start_exec_seconds = date_util.getCurrentSecondTime()
  452. total_order_list = ()
  453. account_list = platform_util.get_youshuge_account_list()
  454. executor = ProcessPoolExecutor(max_workers=5)
  455. futures = []
  456. for account in account_list:
  457. future = executor.submit(get_ysg_order_task, st, et, account)
  458. futures.append(future)
  459. executor.shutdown(True)
  460. for future in futures:
  461. order_list = future.result()
  462. if len(order_list) > 0:
  463. total_order_list = order_list + total_order_list
  464. print('悠书阁订单数量:', len(total_order_list), '执行时长(秒):', date_util.getCurrentSecondTime() - start_exec_seconds)
  465. return total_order_list
  466. def get_ysg_order_task(st, et, account):
  467. order_list = ()
  468. url = 'https://novel.youshuge.com/v2/open/orders'
  469. # 超过100条就需要分页,别问我为什么知道,看代码看出来的
  470. max_page_size = 100
  471. host_name = account[0]
  472. channel_id = int(account[1])
  473. secert_key = account[2]
  474. channel = account[3]
  475. stage = account[4]
  476. timestamp = int(time.time())
  477. start_date = time.strftime("%Y-%m-%d", time.localtime(st))
  478. end_date = time.strftime("%Y-%m-%d", time.localtime(et))
  479. page = 1
  480. str1 = 'channel_id=' + str(channel_id) + '&end_date=' + end_date + '&host_name=' + host_name + '&page=' + str(
  481. page) + '&pay_status=1' + '&start_date=' + start_date + '&time=' + str(timestamp) + '&key=' + secert_key
  482. sign = md5value(str1).upper()
  483. data = {
  484. 'sign': sign,
  485. 'host_name': host_name,
  486. 'time': timestamp,
  487. 'channel_id': channel_id,
  488. 'page': page,
  489. 'pay_status': 1,
  490. 'start_date': start_date,
  491. 'end_date': end_date
  492. }
  493. respone = requests.post(url, data)
  494. if respone.status_code == 400:
  495. print('respone', respone)
  496. result_json = respone.json()
  497. first_page_order = build_ysg_order_data(channel, channel_id, result_json, stage)
  498. order_list = order_list + first_page_order
  499. if len(first_page_order) == 0:
  500. return order_list
  501. total_count = result_json['data'][0]['count']
  502. if total_count > max_page_size:
  503. for i in range((total_count - 1) // max_page_size + 1):
  504. timestamp = int(time.time())
  505. str1 = 'channel_id=' + str(
  506. channel_id) + '&end_date=' + end_date + '&host_name=' + host_name + '&page=' + str(
  507. page) + '&pay_status=1' + '&start_date=' + start_date + '&time=' + str(timestamp) + '&key=' + secert_key
  508. sign = md5value(str1).upper()
  509. data2 = {
  510. 'sign': sign,
  511. 'host_name': host_name,
  512. 'time': timestamp,
  513. 'channel_id': channel_id,
  514. 'page': page,
  515. 'pay_status': 1,
  516. 'start_date': start_date,
  517. 'end_date': end_date
  518. }
  519. r2 = requests.post(url, data2).json()
  520. order_list = order_list + build_ysg_order_data(channel, channel_id, r2, stage)
  521. page = page + 1
  522. return order_list
  523. def build_ysg_order_data(channel, channel_id, result_json, stage):
  524. order_list = ()
  525. if 'data' in result_json.keys():
  526. data = result_json['data']
  527. if len(data) > 0:
  528. for x in data:
  529. y = {}
  530. dtime = datetime.datetime.strptime(x['create_time'][0:10], "%Y-%m-%d")
  531. y['date'] = ((int(
  532. time.mktime(dtime.timetuple())) + 8 * 3600) // 86400) * 86400 - 8 * 3600
  533. y['order_id'] = x['order_num']
  534. y['amount'] = round(int(x['price']) / 100, 2)
  535. y['order_time'] = x['create_time']
  536. y['channel'] = channel
  537. y['from_novel'] = x['book_name']
  538. y['stage'] = stage
  539. y['user_id'] = x['openid']
  540. y['channel_id'] = channel_id
  541. y['platform'] = '悠书阁'
  542. y['reg_time'] = x['reg_time']
  543. y = sorted(y.items(), key=lambda item: item[0])
  544. y = dict(y)
  545. y = tuple(y.values())
  546. order_list = order_list + ((y),)
  547. return order_list
  548. # 数据导入表采用replace替换主键orderid的方法
  549. def mysql_insert_order(data):
  550. if data is None or len(data) == 0:
  551. print('数据为空,不执行数据库操作!')
  552. else:
  553. sql = 'replace into quchen_text.`order_zwg` (amount,channel,channel_id,date,from_novel,order_id,order_time,platform,reg_time,stage,user_id) values (%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s);'
  554. connect = MySQLConnection()
  555. try:
  556. num = connect.batch(sql, data)
  557. # 提交
  558. connect.commit()
  559. print(num, '条订单数据入库成功')
  560. except Exception as e:
  561. traceback.print_exc()
  562. print('订单数据入库失败:', e)
  563. finally:
  564. connect.close()
  565. # 获取各平台的订单数量
  566. def mysql_select_platform_order_count(date):
  567. sql = 'SELECT platform, COUNT(1) AS num FROM quchen_text.`order_zwg` WHERE date = %s GROUP BY platform'
  568. connect = MySQLConnection()
  569. platform_order_count = []
  570. try:
  571. platform_order_count = connect.query(sql, date)
  572. return platform_order_count
  573. except Exception as e:
  574. traceback.print_exc()
  575. print('各平台的订单数据查询失败:', e)
  576. finally:
  577. connect.close()
  578. return platform_order_count
  579. def start_all_job():
  580. start_exec_seconds = date_util.getCurrentSecondTime()
  581. st_unix = date_util.getYesterdayStartTime()
  582. et_unix = date_util.getTodayStartTime()
  583. st_unix = 1601136000 #2020/9/27 0:0:0
  584. et_unix = 1601308800 #2020/9/29 0:0:0
  585. print('查询开始时间:', st_unix, date_util.getSecondsToDatetime(st_unix))
  586. print('查询结束时间:', et_unix, date_util.getSecondsToDatetime(et_unix))
  587. order_list = get_yuewen_order(st_unix, et_unix)
  588. mysql_insert_order(order_list)
  589. # platform_order_num_list = mysql_select_platform_order_count(date_util.getYesterdayStartTime())
  590. # if len(platform_order_num_list) == 0:
  591. # print('本地库中没有任何数据,现在全平台补全')
  592. # mysql_insert_order(get_zzy_order(st_unix, et_unix))
  593. # mysql_insert_order(get_yuewen_order(st_unix, et_unix))
  594. # mysql_insert_order(get_huasheng_order(st_unix, et_unix))
  595. # mysql_insert_order(get_ysg_order(st_unix, et_unix))
  596. # mysql_insert_order(get_zhangdu_order(st_unix, et_unix))
  597. # else:
  598. # platform_list = ['阅文','悠书阁','掌读','掌中云','花生']
  599. # for platform_order_num in platform_order_num_list:
  600. # platform = str(platform_order_num['platform'])
  601. # num = int(platform_order_num['num'])
  602. # platform_list.remove(platform)
  603. #
  604. # if platform == '阅文':
  605. # order_list = get_yuewen_order(st_unix, et_unix)
  606. # if len(order_list) != num:
  607. # print('阅文数据实际订单和已经入库数据差异:', len(order_list) - num)
  608. # mysql_insert_order(order_list)
  609. # elif platform == '悠书阁':
  610. # order_list = get_ysg_order(st_unix, et_unix)
  611. # if len(order_list) != num:
  612. # print('悠书阁数据实际订单和已经入库数据差异:', len(order_list) - num)
  613. # mysql_insert_order(order_list)
  614. # elif platform == '掌读':
  615. # order_list = get_zhangdu_order(st_unix, et_unix)
  616. # if len(order_list) != num:
  617. # print('掌读数据实际订单和已经入库数据差异:', len(order_list) - num)
  618. # mysql_insert_order(order_list)
  619. # elif platform == '掌中云':
  620. # order_list = get_zzy_order(st_unix, et_unix)
  621. # if len(order_list) != num:
  622. # print('掌中云数据实际订单和已经入库数据差异:', len(order_list) - num)
  623. # mysql_insert_order(order_list)
  624. # elif platform == '花生':
  625. # order_list = get_huasheng_order(st_unix, et_unix)
  626. # if len(order_list) != num:
  627. # print('花生数据实际订单和已经入库数据差异:', len(order_list) - num)
  628. # mysql_insert_order(order_list)
  629. # else:
  630. # print('发现未知平台数据!', platform_order_num)
  631. #
  632. # for platform in platform_list:
  633. # if platform == '阅文':
  634. # print('阅文没有数据')
  635. # mysql_insert_order(get_yuewen_order(st_unix, et_unix))
  636. # elif platform == '悠书阁':
  637. # print('悠书阁没有数据')
  638. # mysql_insert_order(get_ysg_order(st_unix, et_unix))
  639. # elif platform == '掌读':
  640. # print('掌读没有数据')
  641. # mysql_insert_order(get_zhangdu_order(st_unix, et_unix))
  642. # elif platform == '掌中云':
  643. # print('掌中云没有数据')
  644. # mysql_insert_order(get_zzy_order(st_unix, et_unix))
  645. # elif platform == '花生':
  646. # print('花生没有数据')
  647. # mysql_insert_order(get_huasheng_order(st_unix, et_unix))
  648. # else:
  649. # print('什么鬼平台:', platform)
  650. print('订单检查执行时间(秒):', date_util.getCurrentSecondTime() - start_exec_seconds)
  651. if __name__ == '__main__':
  652. start_all_job()
  653. # scheduler = BlockingScheduler()
  654. # #每天凌晨3点到4点的30分钟都执行一次
  655. # scheduler.add_job(start_all_job, 'cron', hour='3-4', minute='35')
  656. # scheduler.start()