Bladeren bron

飞书 insert, 监测表格

罗俊辉 1 jaar geleden
bovenliggende
commit
ceec3615a2

+ 1 - 1
application/common/__init__.py

@@ -1,4 +1,4 @@
-from .feishu import Feishu
+from .feishu import Feishu, FeishuInsert
 from .log import *
 from .messageQueue import *
 from .mysql import *

+ 2 - 1
application/common/feishu/__init__.py

@@ -1 +1,2 @@
-from .feishu import Feishu
+from .feishu import Feishu
+from .feishu_insert import FeishuInsert

+ 56 - 0
application/common/feishu/feishu_insert.py

@@ -0,0 +1,56 @@
+"""
+feishu python方法
+"""
+
+import requests
+
+
+def get_app_token():
+    """
+    获取飞书api token
+    :return:
+    """
+    url = "https://open.feishu.cn/open-apis/auth/v3/tenant_access_token/internal/"
+    post_data = {
+        "app_id": "cli_a51114cf8bf8d00c",  # 这里账号密码是发布应用的后台账号及密码
+        "app_secret": "cNoTAqMpsAm7mPBcpCAXFfvOzCNL27fe",
+    }
+    response = requests.request("POST", url=url, data=post_data)
+    tenant_access_token = response.json()["tenant_access_token"]
+    return tenant_access_token
+
+
+class FeishuInsert(object):
+    """
+    feishu Python Object
+    """
+
+    def __init__(self, document_token):
+        self.headers = {"Content-Type": "application/json"}
+        self.document_token = document_token
+
+    def insert_value(self, sheet_id, ranges, values):
+        """
+        在表的某一个sheet的ranges中插入数据,若该地方存在数据,会自动把已有的数据往下移动,再写如数据
+        :param sheet_id: 飞书表的唯一ID
+        :param ranges: 单元格位置的range, 从左上角到右下角, 两边都是闭区间
+        :param values: 二维数组, 用于填充ranges的空格数组
+        """
+        insert_value_url = "https://open.feishu.cn/open-apis/sheets/v2/spreadsheets/{}/values_prepend".format(
+            self.document_token)
+        # print(get_app_token())
+        headers = {
+            "Authorization": "Bearer " + get_app_token(),
+            'contentType': 'application/json; charset=utf-8'
+        }
+        body = {
+            "valueRange": {
+                "range": "{}!{}".format(sheet_id, ranges),
+                "values": values
+            }
+        }
+        response = requests.request("POST", url=insert_value_url, headers=headers, json=body)
+        print(response.json())
+
+
+

+ 10 - 2
spider/ad_click/piaoquan_vlog.py

@@ -13,6 +13,7 @@ sys.path.append(os.getcwd())
 
 from application.common.proxies import tunnel_proxies
 from application.common.log import AliyunLogger
+from application.common.feishu import FeishuInsert
 
 
 class PiaoQuanVlog(object):
@@ -37,6 +38,8 @@ class PiaoQuanVlog(object):
             'accept-language': 'en-US,en;q=0.9'
         }
         self.aliyun_log = AliyunLogger(platform="piaoquanVlog", mode="recommend")
+        self.feishu = FeishuInsert(document_token="PN9usqN4ehDIFxtdsuqcH546nwe")
+        self.sheet_id = "cc9ace"
 
     def send_request(self, page_num):
         """
@@ -88,7 +91,7 @@ class PiaoQuanVlog(object):
         :return: None
         """
         for video_obj in video_list:
-            print(json.dumps(video_obj, ensure_ascii=False, indent=4))
+            # print(json.dumps(video_obj, ensure_ascii=False, indent=4))
             video_item = {
                 "id": video_obj['id'],
                 "status": video_obj['status'],
@@ -101,12 +104,17 @@ class PiaoQuanVlog(object):
                 "publish_date": video_obj['gmtCreateDescr']
             }
             time.sleep(12)
-            # print(json.dumps(video_item, ensure_ascii=False, indent=4))
             self.aliyun_log.logging(
                 code="7001",
                 message="监控到一条视频",
                 data=video_item
             )
+            line = [video_item["id"], video_item["status"], video_item["uid"], video_item["title"], video_item["titleId"],video_item['playCount'], video_item["shareCount"], video_item["favorCount"], datetime.datetime.now().__str__(), video_item['publish_date']]
+            self.feishu.insert_value(
+                sheet_id=self.sheet_id,
+                values=[line],
+                ranges="A2:K2"
+            )
 
     def run(self):
         """