Procházet zdrojové kódy

增加七悦订单拉取

ck před 4 roky
rodič
revize
154eaf734c
2 změnil soubory, kde provedl 89 přidání a 5 odebrání
  1. 17 2
      app/api_data/get_order.py
  2. 72 3
      app/api_data/order_util.py

+ 17 - 2
app/api_data/get_order.py

@@ -83,10 +83,24 @@ def huasheng(start=None,end=None,new=None):
     save_hs_data(li)
 
 
+def qiyue(start=None,end=None,new=None):
+    if start is None:
+        start = end = du.getNow()
+
+    executor = ThreadPoolExecutor(max_workers=1)
+    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:
+            executor.submit(get_qiyue_order_task, start, end, account)
+    executor.shutdown(True)
 
 def hourly():
     yangguang()
     huasheng()
+    qiyue()
 
 
 def daily():
@@ -103,8 +117,9 @@ def daily():
 
 
 if __name__ == '__main__':
-    yangguang()
-    huasheng()
+    # yangguang()
+    # huasheng('2021-02-01','2021-02-19',new=1)
+    qiyue()
 
 
     """要是只跑一个账号 把 get_yg_acccount() 里面的sql where 条件加上 id=xxx"""

+ 72 - 3
app/api_data/order_util.py

@@ -193,6 +193,7 @@ def get_huasheng_order_task(start,end, account, merchant,li):
             page = page + 1
     print(f"[{merchant_name}] 订单数: {count}")
 
+
 def save_hs_data(data):
     sql = 'replace INTO quchen_text.`order` ' \
           '(amount,channel,channel_id,date,from_novel,order_id,order_time,platform,reg_time,stage,user_id)' \
@@ -200,7 +201,75 @@ def save_hs_data(data):
     db.quchen_text.executeMany(sql,data)
 
 
+
+
+def get_qiyue_order_task(start, end, account):
+    """一分钟请求60次的限制"""
+    order_list = []
+    # 参数
+    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):
+        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()
+
+            freq += 1
+            if freq == 59:
+                print("一分钟请求60次的限制 等待中")
+                time.sleep(61)
+                freq = 0
+
+            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_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(f'{stage} [{start}~{end}] 有订单{order_list.__len__()}')
+    if order_list.__len__()>0:
+        save_qiyue_order(order_list)
+    return order_list
+
+def save_qiyue_order(order_list):
+    db.quchen_text.executeMany('replace into `order` values (%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s)',order_list)
+    print("入库成功")
+
+
+
+
 if __name__ == '__main__':
-    print(du.str_to_stamp('2021-01-18'))
-    a = time.strftime("%Y-%m-%d", time.localtime(1610899200))
-    print(a)
+    get_qiyue_order_task('2021-02-01','2021-02-19',['趣程15期','eyJpdiI6ImluVWxoRUl3VTR6QU5hamlYOFBvXC9BPT0iLCJ2YWx1ZSI6Ik5IZ0N4dm5GcmJ0Zklsd0tNZ1JVSVE9PSIsIm1hYyI6IjJjODUzMjdlZTc2ODI2ZjFmY2QyYmU5MGViYTkzOGU4MDEwZTIyODIxOTE4NzgzYTNhOGQ1YWM4OGJkMDAzMmIifQ=='])