123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554 |
- import time
- from model import ComUtils
- import requests
- import json
- from model.DataBaseUtils import MysqlUtils
- from model.DateUtils import DateUtils
- from model.ComUtils import *
- import math
- from model.DateUtils import DateUtils
- import logging
- from urllib import parse
- from model.DingTalkUtils import DingTalkUtils
- # logging.getLogger().setLevel(logging.WARNING)
- db=MysqlUtils()
- du=DateUtils()
- def get_hs_order_task(start, end, account):
- url = 'https://vip.rlcps.cn/api/getMerchants'
- apiKey = str(account[0])
- apiSecurity = account[1]
- timestamp = str(int(time.time()))
- sign = md5(apiKey + timestamp + apiSecurity).upper()
- params = {
- 'apiKey': apiKey,
- 'apiSecurity': apiSecurity,
- 'timestamp': timestamp,
- 'sign': sign
- }
- response_result_json = requests.post(url, params).json()
- if 'data' not in response_result_json.keys():
- print('花生账号【{apiKey}】本次请求数据异常,响应报文【{result}】'.format(apiKey=apiKey, result=response_result_json))
- channel_data = response_result_json['data']
- # print(f"{account[2]} 有channel{len(channel_data)}个:{str([i['merchant_name'] for i in channel_data])}")
- li = []
- for merchant in channel_data:
- orders = get_huasheng_order(start, end, account, merchant)
- li.extend(orders)
- if len(li) > 0:
- print(f"花生账号:{account[2]} 有order{len(li)}个")
- # print(li)
- save_order(li)
- def get_huasheng_order(start,end, account, merchant):
- li =[]
- apiKey = str(account[0])
- apiSecurity = account[1]
- stage = account[2]
- timestamp = str(int(time.time()))
- order_url = 'https://vip.rlcps.cn/api/orderList'
- merchant_id = merchant['merchant_id']
- merchant_name = merchant['merchant_name']
- limit = 500
- for date in du.getDateLists(start,end):
- page = 1
- while True:
- sign = md5(apiKey + date + str(merchant_id) + timestamp + apiSecurity).upper()
- order_params = {
- 'apiKey': apiKey,
- 'apiSecurity': apiSecurity,
- 'timestamp': timestamp,
- 'date': date,
- 'merchant_id': merchant_id,
- 'sign': sign,
- 'page': page,
- 'limit': limit
- }
- r = requests.post(order_url, order_params)
- response_result_json = r.json()
- if response_result_json['code']!=0:
- print(response_result_json)
- DingTalkUtils().send('花生订单接口异常'+r.text)
- if 'data' not in response_result_json.keys():
- print('花生账号【{key}】, 查询时间【{date}】, 渠道【{merchant_id}:{merchant_name}】本次请求数据异常,响应报文【{result}】'
- .format(key=apiKey, date=date, merchant_id=merchant_id, merchant_name=merchant_name,
- result=response_result_json))
- break
- if len(response_result_json['data']) == 0:
- break
- order_item_list = response_result_json['data']
- if len(order_item_list) == 0:
- break
- for i in order_item_list:
- li.append(
- (i['request_at'][:10],
- stage,
- '花生',
- merchant_name,
- merchant_id,
- i['openid'],
- i['request_at'],
- i['join_at'],
- i['amount'],
- i['book_name'],
- i['trans_id'] if i['trans_id'] != '' else i['order_num'],
- 2 if i['order_status'] == 1 else 1
- # i['user_id']
- )
- )
- if len(order_item_list) < limit:
- break
- else:
- page = page + 1
- return li
- def save_hs_data(data):
- sql = 'replace INTO quchen_text.ods_order ' \
- ' VALUES (%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s);'
- db.quchen_text.executeMany(sql,data)
- # print(order_list)
- def save_order(order_list):
- db.quchen_text.executeMany("""replace into ods_order(date,stage,platform,channel,channel_id,user_id,
- order_time,reg_time,amount,from_novel,order_id,status)
- values (%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s)""", order_list)
- print("入库成功")
- def save_order2(order_list):
- db.quchen_text.executeMany("""replace into ods_order(date,stage,platform,channel,channel_id,user_id,
- order_time,reg_time,amount,from_novel,order_id,status,
- platform_user_id,wechat_app_id,book_tags,order_type,trade_no,transaction_no)
- values (%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s)""", order_list)
- print("入库成功")
- def get_wd_account_siteid_list(account):
- url = 'https://bi.reading.163.com/dist-api/siteList'
- consumerkey = account[0]
- secretkey = account[1]
- stage = account[3]
- timestamp = int(time.time() * 1000)
- siteid_params = {
- "consumerkey": consumerkey,
- 'secretkey': secretkey,
- 'timestamp': timestamp,
- }
- sorted_data = sorted(siteid_params.items(), reverse=False)
- s = ""
- for k, v in sorted_data:
- s = s + str(k) + "=" + str(v)
- sign = md5(s).lower()
- siteid_params['sign'] = sign
- consumerkey = siteid_params['consumerkey']
- timestamp = siteid_params['timestamp']
- parameter = 'consumerkey=' + str(consumerkey) + '×tamp=' + str(timestamp) + '&sign=' + str(sign)
- get_url = url + "?" + parameter
- while True:
- r = requests.get(url=get_url)
- if r.status_code == 200:
- break
- try:
- id_key_list = r.json()['data']
- except:
- return []
- mpid_list = []
- try:
- for id_key_val in id_key_list:
- mpid = dict(id_key_val)["mpId"]
- mpid_list.append(mpid)
- except Exception as e:
- print(stage, '站点查询返回结果:', r.json())
- return mpid_list
- def get_wending_json_object(url,params):
- params['timestamp'] = int(time.time()*1000)
- sorted_data = sorted(params.items(),reverse = False)
- s=""
- for k,v in sorted_data:
- s = s+str(k)+"="+str(v)
- sign = md5(s).lower()
- params['sign'] = sign
- consumerkey = params['consumerkey']
- secretkey = params['secretkey']
- timestamp = params['timestamp']
- siteid = params['siteid']
- pageSize = params['pageSize']
- starttime = params['starttime']
- endtime = params['endtime']
- page = params['page']
- ## +'&secretkey='+str(secretkey)
- parameter = 'consumerkey='+str(consumerkey)+'×tamp='+str(timestamp)+'&siteid='+str(siteid)+'&pageSize='+str(pageSize)\
- +'&starttime='+str(starttime)+'&endtime='+str(endtime)+'&page='+str(page)+'&sign='+str(sign)
- global get_url
- get_url = url+"?"+parameter
- while True:
- r= requests.get(url=get_url)
- if r.status_code==200:
- break
- else:
- time.sleep(1)
- print("请求连接出错,等待1s...")
- response_result_json=r.json()
- del params['sign']
- return response_result_json
- def get_wd_order_task(start,end,account):
- order_list = []
- url = 'https://bi.reading.163.com/dist-api/rechargeList'
- consumerkey = account[0]
- secretkey = account[1]
- siteid = account[2]
- stage = account[3]
- siteid_list = get_wd_account_siteid_list(account)
- # print(siteid_list)
- if len(siteid_list) == 0:
- siteid_list.append(siteid)
- starttime = du.date_str_to_str(start)+'0000'
- endtime = du.date_str_to_str(end)+'2359'
- for siteid in siteid_list:
- page = 1
- while True:
- params = {
- 'consumerkey': consumerkey,
- 'secretkey': secretkey,
- 'timestamp': int(1601481600),
- 'siteid': siteid,
- 'pageSize': 1000,
- 'starttime': starttime,
- 'endtime': endtime,
- 'page': page}
- response_result_json = get_wending_json_object(url, params)
- # print(response_result_json)
- order_item_list = response_result_json['data']['rechargeList']
- for x in order_item_list:
- order_time = DateUtils.stamp_to_str(x['createTime'])
- reg_time = DateUtils.stamp_to_str(x['userRegisterTime'])
- order_list.append(
- (order_time[:10],
- stage,
- '文鼎',
- x['wx_mpName'],
- x['wx_originalId'],
- x['wx_user_openId'],
- order_time,
- reg_time,
- x['money'] / 100,
- x['bookTitle'] if x['bookTitle'] else '',
- x['ewTradeId'] if x.get('ewTradeId') else x['rechargeUuid'],
- 2 if x['payStatus'] == 1 else 1
- # ,x['userId']
- )
- )
- if len(order_item_list) < 1000:
- break
- else:
- page += 1
- print(f"{stage} [{start}~{end}] 有订单 {order_list.__len__()}")
- if order_list.__len__()>0:
- # print(order_list)
- save_order(order_list)
- def get_gf_order_task(start,end,account):
- order_list = []
- url = 'https://bi.reading.163.com/dist-api/rechargeList'
- consumerkey = account[0]
- secretkey = account[1]
- siteid = account[2]
- stage = account[3]
- siteid_list = get_wd_account_siteid_list(account)
- # print(siteid_list)
- if len(siteid_list) == 0:
- siteid_list.append(siteid)
- starttime = du.date_str_to_str(start)+'0000'
- endtime = du.date_str_to_str(end)+'2359'
- for siteid in siteid_list:
- page = 1
- while True:
- params = {
- 'consumerkey': consumerkey,
- 'secretkey': secretkey,
- 'timestamp': int(1601481600),
- 'siteid': siteid,
- 'pageSize': 1000,
- 'starttime': starttime,
- 'endtime': endtime,
- 'page': page}
- response_result_json = get_wending_json_object(url, params)
- # print(response_result_json)
- order_item_list = response_result_json['data']['rechargeList']
- for x in order_item_list:
- order_time = DateUtils.stamp_to_str(x['createTime'])
- reg_time = DateUtils.stamp_to_str(x['userRegisterTime'])
- order_list.append(
- (order_time[:10],
- stage,
- '国风',
- x['wx_mpName'],
- x['wx_originalId'],
- x['wx_user_openId'],
- order_time,
- reg_time,
- x['money'] / 100,
- x['bookTitle'] if x['bookTitle'] else '',
- x['ewTradeId'] if x.get('ewTradeId') else x['rechargeUuid'],
- 2 if x['payStatus'] == 1 else 1
- # ,x['userId']
- )
- )
- if len(order_item_list) < 1000:
- break
- else:
- page += 1
- print(f"{stage} [{start}~{end}] 有订单 {order_list.__len__()}")
- if order_list.__len__()>0:
- print(order_list)
- save_order(order_list)
- def get_zd_order_task(start,end,account):
- """开始到结束最多90天"""
- order_list = []
- url = 'https://api.zhangdu520.com/channel/getorder'
- uid = account[0]
- appsecert = account[1]
- channel = account[2]
- stage = account[3]
- timestamp = int(time.time())
- sign = md5(str(uid) + '&' + appsecert + '&' + str(timestamp))
- for i in du.split_date2(start, end, 90):
- starttime = DateUtils.str_to_stamp(i[0]+' 00:00:00','%Y-%m-%d %H:%M:%S')
- endtime = DateUtils.str_to_stamp(i[1]+' 23:59:59','%Y-%m-%d %H:%M:%S')
- page = 1
- while True:
- params = {
- 'uid': uid,
- 'timestamp': timestamp,
- 'sign': sign,
- 'starttime': starttime,
- 'endtime': endtime,
- 'page': page
- }
- response_result_json = requests.get(url=url, params=params).json()
- # print(response_result_json)
- if 'data' not in response_result_json.keys():
- print(f'掌读账号【{uid}】, 查询时间【{i[0]} - {i[1]}】,本次请求数据异常,响应报文【{response_result_json}】')
- break
- result_data = response_result_json['data']
- page_count = result_data['pageCount']
- if page_count == 0:
- break
- order_item_list = result_data['list']
- for i in order_item_list:
- order_time = DateUtils.stamp_to_str(i['ctime'])
- reg_time = DateUtils.stamp_to_str(i['regtime'])
- order_list.append((
- order_time[:10],
- stage,
- '掌读',
- channel,
- uid,
- i['openid'],
- order_time,
- reg_time,
- i['amount'],
- i['book_entry'],
- i['orderno'],
- 2 if i['status'] == '1' else 1
- # ,i['userid']
- ))
- if page == page_count: # 是最后一页
- break
- page = page + 1
- print(f"{channel} [{start}]~[{end}] 有订单 {order_list.__len__()}")
- if len(order_list) > 0:
- # print(order_list)
- save_order(order_list)
- def get_zzy_order_task(start, end, account):
- url = 'https://inovel.818tu.com/partners/channel/channels/list?'
- key = account[0]
- secert = account[1]
- stage = account[2]
- sign = md5(secert + 'key=' + key)
- params = 'key=' + key + '&sign=' + sign
- response_result_json = requests.get(url + params).json() # 获取子渠道列表
- if 'data' not in response_result_json.keys():
- print('掌中云账号【{key}】本次请求数据异常,响应报文【{result}】'.format(key=key, result=response_result_json))
- return
- items = response_result_json['data']['items']
- print(f'VIP{account[0]} 有公众号{len(items)} ')
- total_order_list = []
- for channel in items:
- # 获取channel_id 后逐个拉取历史orders
- order_list = get_zzy_channel_order(start, end, account, channel)
- total_order_list.extend(order_list)
- print(f"{stage} [{start}]~[{end}] 有订单{total_order_list.__len__()}")
- if len(total_order_list) > 0:
- save_order(total_order_list)
- def get_zzy_channel_order(start, end, account, channel):
- get_time=DateUtils.str_to_date_str(start, f2="%Y-%m-%dT%H:%M:%S+08:00")
- limit_time=DateUtils.str_to_date_str(end, f2="%Y-%m-%dT%H:%M:%S+08:00")
- order_list = []
- key = account[0]
- secert = account[1]
- stage = account[2]
- order_url = 'https://openapi.818tu.com/partners/channel/orders/list?'
- channel_id = channel['id']
- channel_name = channel['nickname']
- status = str(1)
- page = str(1)
- per_page = str(1000)
- gte = parse.urlencode({'created_at[gte]': get_time}) # gte就是ge 大于等于开始时间
- lt = parse.urlencode({'created_at[lt]': limit_time}) # 小于 结束时间
- while True:
- sign = md5(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)
- params = 'channel_id=' + str(channel_id) + '&' + gte + '&' + lt + '&page=' + str(
- page) + '&per_page=' + per_page + '&status=' + status + '&key=' + key + '&sign=' + sign
- while True:
- r = requests.get(order_url + params)
- if r.status_code == 200:
- response_result_json = r.json()
- break
- else:
- time.sleep(61)
- print("掌中云接口调用sleep 61s...")
- # print(response_result_json)
- if 'data' not in response_result_json.keys():
- print(f'掌中云账号【{key}】,查询时间【{start} - {end}】,渠道【{channel_name}】本次请求数据异常,响应报文【{r.text}】')
- break
- total_count = response_result_json['data']['count'] # 总数量
- order_item_list = response_result_json['data']['items'] # 订单列表
- for i in order_item_list:
- order_time = DateUtils.str_to_date_str(i['created_at'], "%Y-%m-%dT%H:%M:%S+08:00", "%Y-%m-%d %H:%M:%S")
- reg_time = DateUtils.str_to_date_str(i['member']['created_at'], "%Y-%m-%dT%H:%M:%S+08:00", "%Y-%m-%d %H:%M:%S")
- order_list.append((
- order_time[:10],
- stage,
- '掌中云',
- channel_name,
- channel_id,
- i['member']['openid'],
- order_time,
- reg_time,
- round(i['price'] / 100, 2),
- i['from_novel']['title'] if str(i['from_novel_id']) != 'None' else '',
- str(i['id']),
- 2 if i['status'] == 1 else 1
- # ,i['id']
- ))
- if int(page) >= math.ceil(total_count / int(per_page)):
- break
- page = int(page) + 1
- # print(f"{channel_name}获取订单:{order_list.__len__()}")
- # print(order_list)
- return order_list
- def get_account(plactform, id=None):
- op = f" and id={id} " if id else ''
- data = db.quchen_text.getData(f"select text from order_account_text where platform='{plactform}' {op}")
- new_data = []
- for i in data:
- new_data.append(i[0].replace('\n', '').split(","))
- return new_data
- if __name__ == '__main__':
- # account = "347347942,e0c361b54a35a55c2b6296b5a80867ce,趣程小程序"
- # get_hs_order_task('2021-05-01','2021-05-07',account.split(","))
- # print(DateUtils.stamp_to_str(1612155476,'%Y-%m-%d %H:%M:%S')[:10])
- # exit(0)
- st= et = '2021-05-07'
- account = "62140324,KUUxPIokqtIrtvHQ,1025010,趣程19期,qucheng19qi@163.com"
- get_wd_order_task(st,et,account.split(','))
|