# !/usr/bin/env python # -*- coding: utf-8 -*- # 20201120 ## 20201120新添加 七悦 平台接口 ## 七悦api接口地址 https://vip.zhangwenwenhua.com/uploads/attachments/open-api/open-api.pdf ########################## 七悦订单接口,返回数据格式 ''' {'msg': 'ok', 'code': 0, 'data': { 'current_page': 1, 'data': [ {'id': 167100631495102464, 'money': 30, 'type': 1, 'state': 2, 'user_id': 101280121, 'channel_id': 14880, 'create_time': 1605062783, 'finish_time': 1605062790 , 'trade_no': '20201111104622_101280121_Ykhp', 'transaction_no': '4200000845202011115590974617', 'user_open_id': 'oxw0X6zGsL6dSX-Y8lsXMhPG1_cY' , 'user_is_subscribe': 1, 'user_subscribe_time': 1605045625, 'user_create_time': 1605045625 , 'referral_url': 'https://wxaef9ff2c63776f6f.wenhuazw.com/index/book/chapter?book_id=10076083&sid=1007608300004&referral_id=727741' , 'book_name': '我的超级老婆', 'book_keywords': '', 'wechat_app_id': 'wxaef9ff2c63776f6f', 'wechat_app_name': '炎兵文楼', 'channel_name': '炎兵文楼(趣程)' , 'state_desc': '完成', 'type_desc': '书币充值'} ,{...}] ,'first_page_url': 'https://api.zhangwenwenhua.com/v1/orders?page=1' , 'from': 1 , 'last_page': 1 , 'last_page_url': 'https://api.zhangwenwenhua.com/v1/orders?page=1' , 'next_page_url': None , 'path': 'https://api.zhangwenwenhua.com/v1/orders' , 'per_page': '10' , 'prev_page_url': None , 'to': 5 , 'total': 5 } } ''' ######################### import time import requests from concurrent.futures import ProcessPoolExecutor import requests from util import date_util from util import platform_config_util ## 账号配置 from util import robust_util # from apscheduler.schedulers.blocking import BlockingScheduler from util.MySQLConnection import MySQLConnection def get_qiyue_account_list(): """ des cription: 七悦账号列表 return: [['stage','token']] ->list """ return platform_config_util.get_account_list('七悦', 'qiyue_account_config.csv') def get_qiyue_order_task(date, account): order_list = () order_url = "https://api.zhangwenwenhua.com" + "/v1/orders" stage = account[0] token = account[1] page = 1 size = 50 while True: while True: timestamp = int(time.time()) url = order_url + "?" + "token=" + str(token) + "×tamp=" + str(timestamp) + "&page=" + str( page) + "&size=" + str(size) + "&date=" + date r = requests.get(url=url) if r.status_code==200: break else: print(r.text) time.sleep(61) response_result_json = r.json() result_data = response_result_json['data'] total = result_data['total'] if total <= 0: break order_item_list = result_data['data'] for x in order_item_list: if int(x['state']) != 2: continue y = {} y['platform'] = '七悦' y['channel'] = x['wechat_app_name'] ## 公众号名称 y['channel_id'] = x['channel_id'] ## 公众号id y['from_novel'] = x['book_name'] ## 小说名称 y['user_id'] = x['user_id'] ## 付费用户uid y['stage'] = stage ## 期数 createTime = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime(x['create_time'])) y['order_time'] = createTime ## 订单生成时间 y['amount'] = x['money'] ## 原数据单位:元 uid_reg_time = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime(x['user_create_time'])) y['reg_time'] = uid_reg_time ## 用户注册时间 y['order_id'] = x['id'] ## 订单id y['date'] = createTime y = sorted(y.items(), key=lambda item: item[0]) y = dict(y) y = tuple(y.values()) order_list = order_list + ((y),) next_page_url = result_data['next_page_url'] if next_page_url is None: break page += 1 print(f"【{stage}】-【{order_list.__len__()}】条") return order_list @robust_util.catch_exception def get_qiyue_order(dt, account_list): total_order_list = () start_exec_seconds = date_util.getCurrentSecondTime() for account in account_list: order_list= get_qiyue_order_task(dt, account) total_order_list = order_list + total_order_list print('七悦订单数量:', len(total_order_list), '执行时长(秒):', date_util.getCurrentSecondTime() - start_exec_seconds) return total_order_list def batch_save_order(data): if data is None or len(data) == 0: print('数据为空,不执行数据库操作!') else: sql = 'INSERT IGNORE INTO quchen_text.ods_order(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);' connect = MySQLConnection() try: num = connect.batch(sql, data) # 提交 connect.commit() print('订单数据最终入库【{num}】条'.format(num=num)) except Exception as e: print('订单数据入库失败:', e) finally: connect.close() def batch_save_order_new(data): if data is None or len(data) == 0: print('数据为空,不执行数据库操作!') else: sql = 'INSERT IGNORE INTO quchen_text.ods_order(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);' connect = MySQLConnection() try: num = connect.batch(sql, data) # 提交 connect.commit() print('订单数据最终入库【{num}】条'.format(num=num)) except Exception as e: print('订单数据入库失败:', e) finally: connect.close() def start_order_job_qiyue(dt): account_list = get_qiyue_account_list() batch_save_order(get_qiyue_order(dt, account_list)) if __name__ == '__main__': dt=date_util.get_n_day() start_order_job_qiyue(dt)