Selaa lähdekoodia

MOD:添加七悦有声订单拉取;修复七悦bug

cxyu 4 vuotta sitten
vanhempi
commit
035eb5d7f2

+ 47 - 33
app/api_data/get_order.py

@@ -2,10 +2,12 @@ from app.api_data.order_util import *
 from model.DataBaseUtils import MysqlUtils
 from concurrent.futures import ThreadPoolExecutor
 from model.DingTalkUtils import DingTalkUtils
-db=MysqlUtils()
+from app.api_data.platform_order.audio_qiyue import AudioQiyue
 
+db = MysqlUtils()
 
-def get_account(plactform,id=None):
+
+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 = []
@@ -13,26 +15,28 @@ def get_account(plactform,id=None):
         new_data.append(i[0].replace('\n', '').split(","))
     return new_data
 
+
 def get_new_account(plactform):
-    data = db.quchen_text.getData(f"select text from order_account_text where platform='{plactform}' and create_time>='{du.get_n_days(-1)}'")
+    data = db.quchen_text.getData(
+        f"select text from order_account_text where platform='{plactform}' and create_time>='{du.get_n_days(-1)}'")
     new_data = []
     for i in data:
         new_data.append(i[0].replace('\n', '').split(","))
     return new_data
 
-def yangguang(start=None, end=None,new=None):
 
+def yangguang(start=None, end=None, new=None):
     if start:
-        start = start+' 00:00:00'
-        end = end+' 23:59:59'
+        start = start + ' 00:00:00'
+        end = end + ' 23:59:59'
     else:
         start = du.getTodayOrYestoday() + ' 00:00:00'
         end = du.get_n_hours_ago(0)
 
     client_id = 10008097
     token = '2xa1d55tTPBjeEA8Ho'
-    accounts=get_account("阳光") if new is None else get_new_account('阳光')
-    if accounts.__len__()==0:
+    accounts = get_account("阳光") if new is None else get_new_account('阳光')
+    if accounts.__len__() == 0:
         return
     else:
         print(f"阳光账号数:{accounts.__len__()}")
@@ -43,31 +47,31 @@ def yangguang(start=None, end=None,new=None):
         print(vip_id)
         get_yg_vip_channel(stage, vip_id, client_id, token)
         get_yg_data(stage, vip_id, client_id, token, start, end)
-    x=1
+    x = 1
     while True:
         a = db.quchen_text.getOne("select count(1) from yangguang_path where update_time is null")
         print(f" vip 待处理数量 {a} 正在等待数据回调")
         if a == 0:
             break
         time.sleep(60)
-        x+=1
-        if x>20:
-            DingTalkUtils.send('阳光订单回调延时20min','18860455786')
+        x += 1
+        if x > 20:
+            DingTalkUtils.send('阳光订单回调延时20min', '18860455786')
             break
 
     for i in accounts:
         vip_id = i[1]
-        stage=i[0]
-        parse_yg_data(vip_id,stage)
+        stage = i[0]
+        parse_yg_data(vip_id, stage)
 
 
-def huasheng(start=None,end=None,new=None):
+def huasheng(start=None, end=None, new=None):
     if start is None:
         start = end = du.getTodayOrYestoday()
 
     executor = ThreadPoolExecutor(max_workers=5)
     accounts = get_account("花生") if new is None else get_new_account('花生')
-    if len(accounts)==0:
+    if len(accounts) == 0:
         return
     else:
         print(f'花生有账号{len(accounts)}个')
@@ -79,12 +83,12 @@ def huasheng(start=None,end=None,new=None):
         else:
             print(f"账号:{account[2]} 有channel{len(channel_data)}个")
         for merchant in channel_data:
-            executor.submit(get_huasheng_order_task, start, end, account, merchant,li)
+            executor.submit(get_huasheng_order_task, start, end, account, merchant, li)
     executor.shutdown(True)
     save_hs_data(li)
 
 
-def qiyue(start=None,end=None,new=None):
+def qiyue(start=None, end=None, new=None):
     if start is None:
         start = end = du.getNow()
     accounts = get_account("七悦") if new is None else get_new_account('七悦')
@@ -93,11 +97,10 @@ def qiyue(start=None,end=None,new=None):
     else:
         print(f'七悦有账号{len(accounts)}个')
     for account in accounts:
-            get_qiyue_order_task(start, end, account)
+        get_qiyue_order_task(start, end, account)
 
 
-
-def wending(start=None,end=None,new=None):
+def wending(start=None, end=None, new=None):
     if start is None:
         start = end = du.getNow()
 
@@ -107,10 +110,10 @@ def wending(start=None,end=None,new=None):
     else:
         print(f'文鼎有账号{len(accounts)}个')
     for account in accounts:
-          get_wd_order_task(start, end, account)
+        get_wd_order_task(start, end, account)
 
 
-def zhangdu(start=None,end=None,new=None):
+def zhangdu(start=None, end=None, new=None):
     if start is None:
         start = end = du.getNow()
 
@@ -120,11 +123,10 @@ def zhangdu(start=None,end=None,new=None):
     else:
         print(f'掌读有账号{len(accounts)}个')
     for account in accounts:
-          get_zd_order_task(start, end, account)
-
+        get_zd_order_task(start, end, account)
 
 
-def zhangzhongyun(start=None,end=None,new=None):
+def zhangzhongyun(start=None, end=None, new=None):
     if start is None:
         start = end = du.getNow()
 
@@ -135,11 +137,11 @@ def zhangzhongyun(start=None,end=None,new=None):
     else:
         print(f'掌中云有账号{len(accounts)}个')
     for account in accounts:
-        executor.submit(get_zzy_order_task,start, end, account)
+        executor.submit(get_zzy_order_task, start, end, account)
     executor.shutdown()
 
 
-def yueweng(start=None,end=None,new=None):
+def yueweng(start=None, end=None, new=None):
     if start is None:
         start = end = du.getNow()
 
@@ -150,10 +152,21 @@ def yueweng(start=None,end=None,new=None):
     else:
         print(f'阅文有账号{len(accounts)}个')
     for account in accounts:
-        executor.submit(get_yueweng_order_task,start, end, account)
+        executor.submit(get_yueweng_order_task, start, end, account)
     executor.shutdown()
 
 
+def qiyueyousheng(start=None, end=None, new=None):
+    if start is None:
+        start = end = du.getNow()
+    accounts = get_account("七悦有声") if new is None else get_new_account('七悦有声')
+    if len(accounts) == 0:
+        return
+    else:
+        print(f'七悦有声有账号{len(accounts)}个')
+    for account in accounts:
+        AudioQiyue().get_order(start, end, account)
+
 
 def hourly():
     # try:
@@ -165,25 +178,27 @@ def hourly():
 
     huasheng()
     qiyue()
+    qiyueyousheng()
     wending()
     zhangdu()
-    zhangzhongyun(st,et)
+    zhangzhongyun(st, et)
 
 
 def daily():
     st = du.get_n_days(-10)
-    st2=du.get_n_days(-3)
     et = du.get_n_days(-1)
     # try:
     #     yangguang(st, et)
     # except:
     #     pass
     huasheng(st, et)
-    qiyue(st2, et)    # 有毛病
+    qiyue(st, et)
+    qiyueyousheng(st, et)
     wending(st, et)
     zhangdu(st, et)
     zhangzhongyun(st, et)
 
+
 def daily_yg():
     st = du.get_n_days(-10)
     et = du.get_n_days(-1)
@@ -204,4 +219,3 @@ if __name__ == '__main__':
     # zhangdu('2020-11-01', '2020-12-20')
 
     zhangzhongyun('2021-04-01', '2021-04-21')
-

+ 3 - 3
app/api_data/order_util.py

@@ -209,17 +209,17 @@ def get_qiyue_order_task(start, end, account):
     order_url = "https://api.zhangwenwenhua.com" + "/v1/orders"
     stage = account[0]
     token = account[1]
-    page = 1
     size = 50
     freq=0
     for date in du.getDateLists(start, end):
+        page = 1
 
         while True:
             timestamp = int(time.time())
             url = order_url + "?" + "token=" + str(token) + "&timestamp=" + str(timestamp) + "&page=" + str(
                 page) + "&size=" + str(size) + "&date=" + date
-            response_result_json = requests.get(url=url).json()
-
+            rsp=requests.get(url=url)
+            response_result_json = rsp.json()
 
             freq += 1
             if freq == 59:

+ 96 - 0
app/api_data/platform_order/audio_qiyue.py

@@ -0,0 +1,96 @@
+import time
+import requests
+from model.DataBaseUtils import MysqlUtils
+from model.DateUtils import DateUtils
+from data_processing.sql_tools import save_order
+from queue import Queue
+
+
+class AudioQiyue:
+    def __init__(self):
+        self.db_quchen_text = MysqlUtils().quchen_text
+        self.date_action = DateUtils()
+        self.page_size = 50  # 一次请求的量级
+        # 限制一分钟120次请求,设置为本地限制70s 120次请求
+        self.time_sing_size = 120  # 时间队列长度
+        self.time_sing_ms = 70  # 时间队列,第一对于最后的时间差值.
+
+    def control_speed(self, time_queue):
+        if time_queue.full():
+            time_s = time_queue.get()
+            differ_time_num = time.time() - time_s
+            if differ_time_num < self.time_sing_ms:
+                print('访问速度过快,进行休眠,{}'.format(self.time_sing_ms - differ_time_num))
+                time.sleep(self.time_sing_ms - differ_time_num)
+            else:
+                time_queue.put(time.time())
+        else:
+            time_queue.put(time.time())
+
+    def get_order(self, start, end, account):
+
+        order_list = []
+        # 参数
+        order_url = "https://o-api.qiyuept.com" + "/v1/orders"
+        stage = account[0]
+        token = account[1]
+        time_sign = Queue(self.time_sing_size)
+        for date in self.date_action.getDateLists(start, end):
+            page = 1
+            while True:
+                timestamp = int(time.time())
+                self.control_speed(time_queue=time_sign)
+
+                url = order_url + "?" + "token=" + str(token) + "&timestamp=" + str(timestamp) + "&page=" + str(
+                    page) + "&size=" + str(self.page_size) + "&date=" + date
+                rsp = requests.get(url=url)
+                response_result_json = rsp.json()
+
+                code = response_result_json['code']
+                if code != 0:
+                    print(stage, '七悦充值接口异常:', response_result_json)
+                    break
+
+                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 = ((int(x['create_time']) + 8 * 3600) // 86400 * 86400 - 8 * 3600,
+                         stage,
+                         '七悦有声',
+                         x['wechat_app_name'],  # 公众号名称
+                         x['channel_id'],
+                         x['user_open_id'],
+                         time.strftime("%Y-%m-%d %H:%M:%S", time.localtime(x['create_time'])),
+                         time.strftime("%Y-%m-%d %H:%M:%S", time.localtime(x['user_create_time'])),  # 用户注册时间
+                         x['money'],
+                         x['book_name'],
+                         x['id']  # 订单id
+                         )
+                    order_list.append(y)
+                next_page_url = result_data['next_page_url']
+                if next_page_url is None:
+                    break
+                page += 1
+            # print(len(order_list))
+
+        print(f'{stage} [{start}~{end}] 有订单{order_list.__len__()}')
+        if order_list.__len__() > 0:
+            save_order(db_operation=self.db_quchen_text, order_list=order_list)
+            # print(order_list)
+        return order_list
+
+
+
+if __name__ == "__main__":
+    account = ['趣程15期',
+               'eyJpdiI6Ilc0dmJWTjlHZnpJVVUwM3Q3dlc2aWc9PSIsInZhbHVlIjoiNFFvbXJISzBoTExoa0NJMmtXd0FMUT09IiwibWFjIjoiNTY1YjA3MTVlMzliYzg2MzcxMjZjOTRkYTMyY2FlZmJmNDUyZjYyZGEzM2I4MTMxNDNhMTIwNTIzZWViZjMyMSJ9']
+    start = '2021-04-01'
+    end = '2021-04-22'
+    # AudioQiyue().get_order(start=start, end=end, account=account)
+    AudioQiyue().run(start=start, end=end)

+ 5 - 0
data_processing/sql_tools.py

@@ -0,0 +1,5 @@
+
+
+def save_order(db_operation,order_list):
+    db_operation.executeMany('replace into `order` values (%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s)',order_list)
+    print("入库成功")