浏览代码

微信公众后台站内信消息,定时/实时落日志 功能

zhangyong 11 月之前
父节点
当前提交
8e76756737
共有 9 个文件被更改,包括 626 次插入0 次删除
  1. 4 0
      common/__init__.py
  2. 56 0
      common/aliyun_log.py
  3. 48 0
      common/common.py
  4. 330 0
      common/feishu.py
  5. 28 0
      common/material.py
  6. 4 0
      config.ini
  7. 0 0
      wx_gzh/__init__.py
  8. 116 0
      wx_gzh/wx_message_data.py
  9. 40 0
      wx_message_job.py

+ 4 - 0
common/__init__.py

@@ -0,0 +1,4 @@
+from .common import Common
+from .material import Material
+from .feishu import Feishu
+from .aliyun_log import AliyunLogger

+ 56 - 0
common/aliyun_log.py

@@ -0,0 +1,56 @@
+# -*- coding: utf-8 -*-
+# @Time: 2024/05/12
+"""
+公共方法,包含:生成log / 删除log
+"""
+import json
+from datetime import date, timedelta
+from datetime import datetime
+import time
+
+from aliyun.log import PutLogsRequest, LogClient, LogItem
+
+proxies = {"http": None, "https": None}
+
+
+class AliyunLogger:
+    # 统一获取当前时间 <class 'datetime.datetime'>  2022-04-14 20:13:51.244472
+    now = datetime.now()
+    # 昨天 <class 'str'>  2022-04-13
+    yesterday = (date.today() + timedelta(days=-1)).strftime("%Y-%m-%d")
+    # 今天 <class 'datetime.date'>  2022-04-14
+    today = date.today()
+    # 明天 <class 'str'>  2022-04-15
+    tomorrow = (date.today() + timedelta(days=1)).strftime("%Y-%m-%d")
+
+    # 写入阿里云日志
+    @staticmethod
+    def logging(contents):
+        """
+        写入阿里云日志
+        测试库: https://sls.console.aliyun.com/lognext/project/crawler-log-dev/logsearch/crawler-log-dev
+        正式库: https://sls.console.aliyun.com/lognext/project/crawler-log-prod/logsearch/crawler-log-prod
+        """
+        accessKeyId = "LTAIWYUujJAm7CbH"
+        accessKey = "RfSjdiWwED1sGFlsjXv0DlfTnZTG1P"
+
+        project = "crawler-log-prod"
+        logstore = "wxgzh_message_log"
+        endpoint = "cn-hangzhou.log.aliyuncs.com"
+
+        # 创建 LogClient 实例
+        client = LogClient(endpoint, accessKeyId, accessKey)
+        log_group = []
+        log_item = LogItem()
+        log_item.set_contents(contents)
+        log_group.append(log_item)
+        # 写入日志
+        request = PutLogsRequest(
+            project=project,
+            logstore=logstore,
+            topic="",
+            source="",
+            logitems=log_group,
+            compress=False,
+        )
+        client.put_logs(request)

+ 48 - 0
common/common.py

@@ -0,0 +1,48 @@
+# -*- coding: utf-8 -*-
+# @Time: 2023/12/26
+"""
+公共方法,包含:生成log / 删除log / 下载方法 / 删除 weixinzhishu_chlsfiles / 过滤词库 / 保存视频信息至本地 txt / 翻译 / ffmpeg
+"""
+import os
+import sys
+
+sys.path.append(os.getcwd())
+from datetime import date, timedelta
+from datetime import datetime
+from loguru import logger
+
+proxies = {"http": None, "https": None}
+
+
+class Common:
+    # 统一获取当前时间 <class 'datetime.datetime'>  2022-04-14 20:13:51.244472
+    now = datetime.now()
+    # 昨天 <class 'str'>  2022-04-13
+    yesterday = (date.today() + timedelta(days=-1)).strftime("%Y-%m-%d")
+    # 今天 <class 'datetime.date'>  2022-04-14
+    today = date.today()
+    # 明天 <class 'str'>  2022-04-15
+    tomorrow = (date.today() + timedelta(days=1)).strftime("%Y-%m-%d")
+
+    # 使用 logger 模块生成日志
+    @staticmethod
+    def logger(log_type):
+        """
+        使用 logger 模块生成日志
+        """
+        # 日志路径
+        log_dir = f"./{log_type}/logs/"
+        log_path = os.getcwd() + os.sep + log_dir
+        if not os.path.isdir(log_path):
+            os.makedirs(log_path)
+        # 日志文件名
+        log_name = f"{log_type}-{datetime.now().date().strftime('%Y-%m-%d')}.log"
+
+        # 日志不打印到控制台
+        logger.remove(handler_id=None)
+        # 初始化日志
+        logger.add(os.path.join(log_dir, log_name), level="INFO", rotation="00:00", retention="10 days", enqueue=True)
+
+        return logger
+
+

+ 330 - 0
common/feishu.py

@@ -0,0 +1,330 @@
+# -*- coding: utf-8 -*-
+# @Time: 2023/12/26
+"""
+飞书表配置: token 鉴权 / 增删改查 / 机器人报警
+"""
+import json
+import os
+import sys
+import requests
+import urllib3
+
+sys.path.append(os.getcwd())
+from common import Common
+
+proxies = {"http": None, "https": None}
+
+
+class Feishu:
+    """
+    编辑飞书云文档
+    """
+    succinct_url = "https://w42nne6hzg.feishu.cn//sheets/"
+    # 飞书路径token
+    @classmethod
+    def spreadsheettoken(cls):
+        return "ZVT8sBXrshHNiatQUl9cLKwdnvg"
+
+
+
+    # 获取飞书api token
+    @classmethod
+    def get_token(cls):
+        """
+        获取飞书api token
+        :return:
+        """
+        url = "https://open.feishu.cn/open-apis/auth/v3/tenant_access_token/internal/"
+        post_data = {"app_id": "cli_a13ad2afa438d00b",  # 这里账号密码是发布应用的后台账号及密码
+                     "app_secret": "4tK9LY9VbiQlY5umhE42dclBFo6t4p5O"}
+
+        try:
+            urllib3.disable_warnings()
+            response = requests.post(url=url, data=post_data, proxies=proxies, verify=False)
+            tenant_access_token = response.json()["tenant_access_token"]
+            return tenant_access_token
+        except Exception as e:
+            Common.logger("feishu").error("获取飞书 api token 异常:{}", e)
+
+    # 获取表格元数据
+    @classmethod
+    def get_metainfo(cls):
+        """
+        获取表格元数据
+        :return:
+        """
+        try:
+            get_metainfo_url = "https://open.feishu.cn/open-apis/sheets/v2/spreadsheets/" \
+                               + cls.spreadsheettoken() + "/metainfo"
+
+            headers = {
+                "Authorization": "Bearer " + cls.get_token(),
+                "Content-Type": "application/json; charset=utf-8"
+            }
+            params = {
+                "extFields": "protectedRange",  # 额外返回的字段,extFields=protectedRange时返回保护行列信息
+                "user_id_type": "open_id"  # 返回的用户id类型,可选open_id,union_id
+            }
+            urllib3.disable_warnings()
+            r = requests.get(url=get_metainfo_url, headers=headers, params=params, proxies=proxies, verify=False)
+            response = json.loads(r.content.decode("utf8"))
+            return response
+        except Exception as e:
+            Common.logger("feishu").error("获取表格元数据异常:{}", e)
+
+    # 读取工作表中所有数据
+    @classmethod
+    def get_values_batch(cls, sheetid):
+        """
+        读取工作表中所有数据
+        :param sheetid: 哪张表
+        :return: 所有数据
+        """
+        try:
+            get_values_batch_url = "https://open.feishu.cn/open-apis/sheets/v2/spreadsheets/" \
+                                   + cls.spreadsheettoken() + "/values_batch_get"
+            headers = {
+                "Authorization": "Bearer " + cls.get_token(),
+                "Content-Type": "application/json; charset=utf-8"
+            }
+            params = {
+                "ranges": sheetid,
+                "valueRenderOption": "ToString",
+                "dateTimeRenderOption": "",
+                "user_id_type": "open_id"
+            }
+            urllib3.disable_warnings()
+            r = requests.get(url=get_values_batch_url, headers=headers, params=params, proxies=proxies, verify=False)
+            response = json.loads(r.content.decode("utf8"))
+            values = response["data"]["valueRanges"][0]["values"]
+            return values
+        except Exception as e:
+            Common.logger("feishu").error("读取工作表所有数据异常:{}", e)
+
+    # 工作表,插入行或列
+    @classmethod
+    def insert_columns(cls, sheetid, majordimension, startindex, endindex):
+        """
+        工作表插入行或列
+        :param log_type: 日志路径
+        :param sheetid:哪张工作表
+        :param majordimension:行或者列, ROWS、COLUMNS
+        :param startindex:开始位置
+        :param endindex:结束位置
+        """
+        try:
+            insert_columns_url = "https://open.feishu.cn/open-apis/sheets/v2/spreadsheets/" \
+                                 + cls.spreadsheettoken() + "/insert_dimension_range"
+            headers = {
+                "Authorization": "Bearer " + cls.get_token(),
+                "Content-Type": "application/json; charset=utf-8"
+            }
+            body = {
+                "dimension": {
+                    "sheetId": sheetid,
+                    "majorDimension": majordimension,  # 默认 ROWS ,可选 ROWS、COLUMNS
+                    "startIndex": startindex,  # 开始的位置
+                    "endIndex": endindex  # 结束的位置
+                },
+                "inheritStyle": "AFTER"  # BEFORE 或 AFTER,不填为不继承 style
+            }
+
+            urllib3.disable_warnings()
+            r = requests.post(url=insert_columns_url, headers=headers, json=body, proxies=proxies, verify=False)
+            Common.logger("feishu").info("插入行或列:{}", r.json()["msg"])
+        except Exception as e:
+            Common.logger("feishu").error("插入行或列异常:{}", e)
+
+    # 写入数据
+    @classmethod
+    def update_values(cls, sheetid, ranges, values):
+        """
+        写入数据
+        :param log_type: 日志路径
+        :param sheetid:哪张工作表
+        :param ranges:单元格范围
+        :param values:写入的具体数据,list
+        """
+        try:
+            update_values_url = "https://open.feishu.cn/open-apis/sheets/v2/spreadsheets/" \
+                                + cls.spreadsheettoken() + "/values_batch_update"
+            headers = {
+                "Authorization": "Bearer " + cls.get_token(),
+                "Content-Type": "application/json; charset=utf-8"
+            }
+            body = {
+                "valueRanges": [
+                    {
+                        "range": sheetid + "!" + ranges,
+                        "values": values
+                    },
+                ],
+            }
+            urllib3.disable_warnings()
+            r = requests.post(url=update_values_url, headers=headers, json=body, proxies=proxies, verify=False)
+            Common.logger("feishu").info("写入数据:{}", r.json()["msg"])
+        except Exception as e:
+            Common.logger("feishu").error("写入数据异常:{}", e)
+
+
+    # 读取单元格数据
+    @classmethod
+    def get_range_value(cls, sheetid, cell):
+        """
+        读取单元格内容
+        :param log_type: 日志路径
+        :param sheetid: 哪张工作表
+        :param cell: 哪个单元格
+        :return: 单元格内容
+        """
+        try:
+            get_range_value_url = "https://open.feishu.cn/open-apis/sheets/v2/spreadsheets/" \
+                                  + cls.spreadsheettoken() + "/values/" + sheetid + "!" + cell
+            headers = {
+                "Authorization": "Bearer " + cls.get_token(),
+                "Content-Type": "application/json; charset=utf-8"
+            }
+            params = {
+                "valueRenderOption": "FormattedValue",
+
+                # dateTimeRenderOption=FormattedString 计算并对时间日期按照其格式进行格式化,但不会对数字进行格式化,返回格式化后的字符串。
+                "dateTimeRenderOption": "",
+
+                # 返回的用户id类型,可选open_id,union_id
+                "user_id_type": "open_id"
+            }
+            urllib3.disable_warnings()
+            r = requests.get(url=get_range_value_url, headers=headers, params=params, proxies=proxies, verify=False)
+            # print(r.text)
+            return r.json()["data"]["valueRange"]["values"][0]
+        except Exception as e:
+            Common.logger("feishu").error("读取单元格数据异常:{}", e)
+    # 获取表内容
+    @classmethod
+    def get_sheet_content(cls, sheet_id):
+        try:
+            sheet = Feishu.get_values_batch(sheet_id)
+            content_list = []
+            for x in sheet:
+                for y in x:
+                    if y is None:
+                        pass
+                    else:
+                        content_list.append(y)
+            return content_list
+        except Exception as e:
+            Common.logger("feishu").error(f'get_sheet_content:{e}\n')
+
+    # 删除行或列,可选 ROWS、COLUMNS
+    @classmethod
+    def dimension_range(cls, sheetid, major_dimension, startindex, endindex):
+        """
+        删除行或列
+        :param log_type: 日志路径
+        :param sheetid:工作表
+        :param major_dimension:默认 ROWS ,可选 ROWS、COLUMNS
+        :param startindex:开始的位置
+        :param endindex:结束的位置
+        :return:
+        """
+        try:
+            dimension_range_url = "https://open.feishu.cn/open-apis/sheets/v2/spreadsheets/" \
+                                  + cls.spreadsheettoken() + "/dimension_range"
+            headers = {
+                "Authorization": "Bearer " + cls.get_token(),
+                "Content-Type": "application/json; charset=utf-8"
+            }
+            body = {
+                "dimension": {
+                    "sheetId": sheetid,
+                    "majorDimension": major_dimension,
+                    "startIndex": startindex,
+                    "endIndex": endindex
+                }
+            }
+            urllib3.disable_warnings()
+            r = requests.delete(url=dimension_range_url, headers=headers, json=body, proxies=proxies, verify=False)
+            Common.logger("feishu").info("删除视频数据:{}", r.json()["msg"])
+        except Exception as e:
+            Common.logger("feishu").error("删除视频数据异常:{}", e)
+
+    # 获取用户 ID
+    @classmethod
+    def get_userid(cls, username):
+        try:
+            url = "https://open.feishu.cn/open-apis/user/v1/batch_get_id?"
+            headers = {
+                "Authorization": "Bearer " + cls.get_token(),
+                "Content-Type": "application/json; charset=utf-8"
+            }
+            name_phone_dict = {
+                "xinxin": "15546206651",
+                "muxinyi": "13699208058",
+                "wangxueke": "13513479926",
+                "yuzhuoyi": "18624010360",
+                "luojunhui": "18801281360",
+                "fanjun": "15200827642",
+                "zhangyong": "17600025055",
+                "duchongyu": "18500316632"
+            }
+            username = name_phone_dict.get(username)
+
+            data = {"mobiles": [username]}
+            urllib3.disable_warnings()
+            r = requests.get(url=url, headers=headers, params=data, verify=False, proxies=proxies)
+            open_id = r.json()["data"]["mobile_users"][username][0]["open_id"]
+
+            return open_id
+        except Exception as e:
+            Common.logger("feishu").error(f"get_userid异常:{e}\n")
+
+    # 飞书机器人
+    @classmethod
+    def bot(cls, log_type, text):
+        try:
+            url = "https://open.feishu.cn/open-apis/bot/v2/hook/2b317db6-93ed-43b4-bf01-03c35cfa1d59"
+            headers = {'Content-Type': 'application/json'}
+            sheet_url = "https://w42nne6hzg.feishu.cn/sheets/ZVT8sBXrshHNiatQUl9cLKwdnvg?sheet=ffc082"
+            users = "<at id='all'>所有人</at>"
+
+            data = json.dumps({
+                "msg_type": "interactive",
+                "card": {
+                    "config": {
+                        "wide_screen_mode": True,
+                        "enable_forward": True
+                    },
+                    "elements": [{
+                        "tag": "div",
+                        "text": {
+                            "content": users + text,
+                            "tag": "lark_md"
+                        }
+                    }, {
+                        "actions": [{
+                            "tag": "button",
+                            "text": {
+                                "content": text,
+                                "tag": "lark_md"
+                            },
+                            "url": sheet_url,
+                            "type": "default",
+                            "value": {}
+                        }],
+                        "tag": "action"
+                    }],
+                    "header": {
+                        "title": {
+                            "content": f"📣{log_type}",
+                            "tag": "plain_text"
+                        }
+                    }
+                }
+            })
+            urllib3.disable_warnings()
+            r = requests.post(url, headers=headers, data=data, verify=False, proxies=proxies)
+            Common.logger("feishu").info(f'触发机器人消息:{r.status_code}, {text}')
+        except Exception as e:
+            Common.logger("feishu").error(f"bot异常:{e}\n")
+
+

+ 28 - 0
common/material.py

@@ -0,0 +1,28 @@
+# -*- coding: utf-8 -*-
+# @Time: 2023/12/26
+import os
+import random
+import sys
+import datetime
+
+
+sys.path.append(os.getcwd())
+from common.feishu import Feishu
+
+
+class Material():
+    # 获取飞书表格里所有小程序
+    @classmethod
+    def feishu_list(cls):
+        wx_message = Feishu.get_values_batch("ffc082")
+        list = []
+        for row in wx_message[1:]:
+            name = row[0]
+            cookie = row[1]
+            if name:
+                number = {"name": name, "cookie": cookie}
+                list.append(number)
+            else:
+                return list
+        return list
+

+ 4 - 0
config.ini

@@ -0,0 +1,4 @@
+[PATHS]
+TXT_PATH = /Users/tzld/Desktop/wxgzh_backstage_message/txt/
+
+

+ 0 - 0
wx_gzh/__init__.py


+ 116 - 0
wx_gzh/wx_message_data.py

@@ -0,0 +1,116 @@
+import configparser
+import os
+import sys
+import time
+
+import requests
+from datetime import datetime
+sys.path.append(os.getcwd())
+
+from common import Feishu, AliyunLogger, Common
+
+config = configparser.ConfigParser()
+config.read('./config.ini')
+
+class messageData():
+    @classmethod
+    def create_folders(cls, name):
+        v_text_url = config['PATHS']['TXT_PATH']
+        if not os.path.exists(v_text_url):
+            os.makedirs(v_text_url)
+        text_path = v_text_url + f"{name}.text"
+        try:
+            #  尝试打开文件进行读取
+            with open(text_path, 'r') as file:
+                # 读取第一行内容
+                first_data = file.readline().strip()
+                # 判断第一行是否为空
+                if not first_data:
+                    return None, text_path
+                else:
+                    return first_data, text_path
+        except FileNotFoundError:
+            with open(text_path, 'w') as file:
+                print("文件不存在,已创建新文件:", text_path)
+                return None, text_path
+
+
+    @classmethod
+    def wx_data(cls, data_list):
+        name = data_list['name']
+        cookie = data_list['cookie']
+        first_data, text_path = cls.create_folders(name)
+        url = "https://mp.weixin.qq.com/wxopen/wasysnotify?action=get&msg_type=1&lang=zh_CN&f=json&ajax=1&begin=0&count=50"
+        payload = {}
+        headers = {
+            'accept': 'application/json, text/javascript, */*; q=0.01',
+            'accept-language': 'zh-CN,zh;q=0.9',
+            'cache-control': 'no-cache',
+            'cookie': cookie,
+            'pragma': 'no-cache',
+            'sec-fetch-dest': 'empty',
+            'sec-fetch-mode': 'cors',
+            'sec-fetch-site': 'same-origin',
+            'x-requested-with': 'XMLHttpRequest'
+        }
+        response = requests.request("GET", url, headers=headers, data=payload)
+        data = response.json()
+        err_msg = data["base_resp"]["err_msg"]
+        if err_msg == "ok":
+            item_list = data["notify_msg_item_list"]["item"]
+            try:
+                for itme in item_list:
+                    id = itme["id"]
+                    contents = [
+                        (f"aggregate_id", str(itme["aggregate_id"])),
+                        (f"aggregate_num", str(itme["aggregate_num"])),
+                        (f"aggregate_type", str(itme["aggregate_type"])),
+                        (f"client_msg_id", str(itme["client_msg_id"])),
+                        (f"content", str(itme["content"]).replace("\r", " ").replace("\n", " ")),
+                        (f"create_time", str(itme["create_time"])),
+                        (f"id", str(id)),
+                        (f"module", str(itme["module"])),
+                        (f"msg_status", str(itme["msg_status"])),
+                        (f"msg_type", str(itme["msg_type"])),
+                        (f"notify_tmpl_id", str(itme["notify_tmpl_id"])),
+                        (f"openid_list", str(itme["openid_list"])),
+                        (f"process_status", str(itme["process_status"])),
+                        (f"title", str(itme["title"]).replace("\r", " ").replace("\n", " ")),
+                        (f"update_time", str(itme["update_time"])),
+                        (f"url", str(itme["url"])),
+                        (f"channel", name),
+                        ("timestamp", str(int(time.time()))),
+                    ]
+                    if first_data:
+                        if first_data == str(id):
+                            with open(text_path, 'w') as f:
+                                f.write(f"{item_list[0]['id']}")
+                            return
+                        AliyunLogger.logging(contents)
+                    else:
+                        update_time = itme["update_time"]
+                        # 获取当前日期和时间
+                        current_datetime = datetime.now()
+                        now_date = current_datetime.strftime("%Y-%m-%d")
+                        # 将时间戳转换为日期时间
+                        date_time = datetime.fromtimestamp(update_time)
+                        # 将日期时间对象格式化为字符串("%Y-%m-%d"格式)
+                        formatted_date = date_time.strftime("%Y-%m-%d")
+                        if now_date == formatted_date:
+                            AliyunLogger.logging(contents)
+                        else:
+                            with open(text_path, 'w') as f:
+                                f.write(f"{item_list[0]['id']}")
+                            return
+                with open(text_path, 'w') as f:
+                    f.write(f"{item_list[0]['id']}")
+            except Exception as e:
+                Common.logger("wx").error(f"抓取异常:{e}\n")
+                Feishu.bot('微信公众号后台消息通知,请注意查收', f'抓取异常请关注')
+        else:
+            Feishu.bot('微信公众号后台消息通知,请注意查收', f'{name}cookie,请及时更换')
+
+
+if __name__ == '__main__':
+    name = "票圈视频"
+    Feishu.bot('微信公众号后台消息通知,请注意查收', f'{name}cookie,请及时更换')

+ 40 - 0
wx_message_job.py

@@ -0,0 +1,40 @@
+from wx_gzh.wx_message_data import messageData
+from common import Material
+import concurrent.futures
+import schedule
+import time
+
+
+def video_start(user_data):
+    messageData.wx_data(user_data)
+    print(f"执行完成")
+# name_list = Material.feishu_list()
+# video_start(name_list[0])
+# 定义定时任务
+def video_task():
+    print("开始执行")
+    data = Material.feishu_list()
+    if len(data) == 0:
+        print("没有可执行脚本")
+        return
+    # 创建一个线程池
+    with concurrent.futures.ThreadPoolExecutor() as executor:
+        futures = {executor.submit(video_start, user_data): user_data for user_data in data}
+        for future in concurrent.futures.as_completed(futures):
+            try:
+                # 获取每个任务的执行结果
+                result = future.result()
+                print("处理结果:", result)
+            except concurrent.futures.TimeoutError:
+                future.cancel()
+                print("任务超时,已取消.")
+            except Exception as e:
+                print("处理任务时出现异常:", e)
+
+#每10分钟执行次脚本
+schedule.every(10).minutes.do(video_task)
+
+
+while True:
+    schedule.run_pending()
+    time.sleep(1)