Explorar el Código

增加了飞书通知---v0.2

罗俊辉 hace 1 año
padre
commit
0a557518c4
Se han modificado 2 ficheros con 93 adiciones y 5 borrados
  1. 84 0
      feishu/feishu.py
  2. 9 5
      main_v2.py

+ 84 - 0
feishu/feishu.py

@@ -0,0 +1,84 @@
+"""
+feishu python方法
+"""
+import json
+import requests
+import datetime
+
+
+class Feishu(object):
+    """
+    feishu Python Object
+    """
+
+    def __init__(self):
+        self.document_url = (
+            "https://w42nne6hzg.feishu.cn/sheets/C1Qrsa4HWh6bzEtv7aocrFlAnad"
+        )
+        self.robot_url = "https://open.feishu.cn/open-apis/bot/v2/hook/34e2fdbc-0649-44d3-b5ce-b28b38cca1db"
+        self.headers = {"Content-Type": "application/json"}
+
+    def insert(self):
+        """
+        写入飞书表格
+        :return:
+        """
+        return self.document_url
+
+    def bot(self, platform_name, flag=1):
+        """
+        飞书机器人,在群里报警用
+        :param platform_name: 小程序的名称
+        :param flag: 通知 or 报警, 默认为1, 通知
+        :return:
+        """
+        if flag == 1:
+            payload = {
+                "msg_type": "interactive",
+                "card": {
+                    "elements": [
+                        {
+                            "tag": "div",
+                            "text": {
+                                "content": "**{}**\n数据自动导出完成;\n完成时间是:{}。".format(
+                                    platform_name, datetime.datetime.now().__str__()
+                                ),
+                                "tag": "lark_md",
+                            },
+                        },
+                    ],
+                    "header": {"title": {"content": "We分析: 通知 ✅", "tag": "plain_text"}},
+                },
+            }
+        else:
+            payload = {
+                "msg_type": "interactive",
+                "card": {
+                    "elements": [
+                        {
+                            "tag": "div",
+                            "text": {
+                                "content": "**{}**\n自动导出失败 !!!".format(
+                                    platform_name
+                                ),
+                                "tag": "lark_md",
+                            },
+                        },
+                    ],
+                    "header": {"title": {"content": "We分析: 报警  ❌", "tag": "plain_text"}},
+                },
+            }
+        w = requests.request(
+            "POST", url=self.robot_url, headers=self.headers, data=json.dumps(payload)
+        )
+        print(w.json())
+        # if response.status_code == 200:
+        #     return True
+        # else:
+        #     print("飞书通知失败")
+        #     return
+
+
+if __name__ == "__main__":
+    F = Feishu()
+    F.bot("票圈视频", 2)

+ 9 - 5
main_v2.py

@@ -12,6 +12,7 @@ from playwright.async_api import async_playwright
 from applications import check_account,  Mysql
 from applications.config import platform_map
 from applications.functions import navigate
+from feishu.feishu import Feishu
 
 platform_name = None
 data_frame = []
@@ -49,10 +50,10 @@ async def handle_download(download):
         w = {temp: data_line[index] for index, temp in enumerate(title_row)}
         print(platform_name, "\t", task_name, "\t", task_index)
         print(json.dumps(w, ensure_ascii=False, indent=4))
-        # while len(temp) < 29:
-        #     temp.append("NULL")
-        # Mysql().insert_line(data=temp)
-        # data_frame.append(temp)
+        while len(temp) < 29:
+            temp.append("NULL")
+        Mysql().insert_line(data=temp)
+        data_frame.append(temp)
         os.remove(new_file_path)
 
 
@@ -91,17 +92,20 @@ async def run(playwright):
     await page.goto("https://wedata.weixin.qq.com/mp2/basic-data/core-data?source=0")
 
     done_dict = {}
+    F = Feishu()
     platform_name = '票圈内容精选'
     await navigate(page, platform_name)
     done_dict[platform_name] = 1
+    F.bot(platform_name)
 
     for platform_name in platform_map.keys():
         if done_dict.get(platform_name):
             print(f"{platform_name} has already been exported")
         else:
             await check_account(page, platform_name)
-            # await page.wait_until_loaded()
             await navigate(page, platform_name)
+            done_dict[platform_name] = 1
+            F.bot(platform_name)
 
     # 关闭浏览器上下文和浏览器
     await context.close()