Skip to content

Commit

Permalink
钉钉报警支持指定用户id,爬虫代码支持提示
Browse files Browse the repository at this point in the history
  • Loading branch information
Boris-code committed Mar 18, 2024
1 parent 0f2f980 commit 033feba
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 14 deletions.
15 changes: 9 additions & 6 deletions feapder/core/base_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@
from feapder.db.mysqldb import MysqlDB
from feapder.network.item import UpdateItem
from feapder.utils.log import log
from feapder.network.request import Request
from feapder.network.response import Response
from feapder.utils.perfect_dict import PerfectDict


class BaseParser(object):
Expand All @@ -26,7 +29,7 @@ def start_requests(self):

pass

def download_midware(self, request):
def download_midware(self, request: Request):
"""
@summary: 下载中间件 可修改请求的一些参数, 或可自定义下载,然后返回 request, response
---------
Expand All @@ -37,7 +40,7 @@ def download_midware(self, request):

pass

def validate(self, request, response):
def validate(self, request: Request, response: Response):
"""
@summary: 校验函数, 可用于校验response是否正确
若函数内抛出异常,则重试请求
Expand All @@ -53,7 +56,7 @@ def validate(self, request, response):

pass

def parse(self, request, response):
def parse(self, request: Request, response: Response):
"""
@summary: 默认的解析函数
---------
Expand All @@ -65,7 +68,7 @@ def parse(self, request, response):

pass

def exception_request(self, request, response, e):
def exception_request(self, request: Request, response: Response, e: Exception):
"""
@summary: 请求或者parser里解析出异常的request
---------
Expand All @@ -78,7 +81,7 @@ def exception_request(self, request, response, e):

pass

def failed_request(self, request, response, e):
def failed_request(self, request: Request, response: Response, e: Exception):
"""
@summary: 超过最大重试次数的request
可返回修改后的request 若不返回request,则将传进来的request直接人redis的failed表。否则将修改后的request入failed表
Expand Down Expand Up @@ -135,7 +138,7 @@ def add_task(self):
@result:
"""

def start_requests(self, task):
def start_requests(self, task: PerfectDict):
"""
@summary:
---------
Expand Down
3 changes: 2 additions & 1 deletion feapder/setting.py
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,8 @@
# 报警 支持钉钉、飞书、企业微信、邮件
# 钉钉报警
DINGDING_WARNING_URL = "" # 钉钉机器人api
DINGDING_WARNING_PHONE = "" # 报警人 支持列表,可指定多个
DINGDING_WARNING_PHONE = "" # 被@的群成员手机号,支持列表,可指定多个。
DINGDING_WARNING_USER_ID = "" # 被@的群成员userId,支持列表,可指定多个
DINGDING_WARNING_ALL = False # 是否提示所有人, 默认为False
DINGDING_WARNING_SECRET = None # 加签密钥
# 飞书报警
Expand Down
3 changes: 2 additions & 1 deletion feapder/templates/project_template/setting.py
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,8 @@
# # 报警 支持钉钉、飞书、企业微信、邮件
# # 钉钉报警
# DINGDING_WARNING_URL = "" # 钉钉机器人api
# DINGDING_WARNING_PHONE = "" # 报警人 支持列表,可指定多个
# DINGDING_WARNING_PHONE = "" # 被@的群成员手机号,支持列表,可指定多个。
# DINGDING_WARNING_USER_ID = "" # 被@的群成员userId,支持列表,可指定多个
# DINGDING_WARNING_ALL = False # 是否提示所有人, 默认为False
# DINGDING_WARNING_SECRET = None # 加签密钥
# # 飞书报警
Expand Down
42 changes: 36 additions & 6 deletions feapder/utils/tools.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
@author: Boris
@email: [email protected]
"""
import hmac
import asyncio
import base64
import calendar
Expand All @@ -16,6 +15,7 @@
import datetime
import functools
import hashlib
import hmac
import html
import importlib
import json
Expand Down Expand Up @@ -2467,18 +2467,41 @@ def reach_freq_limit(rate_limit, *key):


def dingding_warning(
message, message_prefix=None, rate_limit=None, url=None, user_phone=None, secret=None
message,
*,
message_prefix=None,
rate_limit=None,
url=None,
user_phone=None,
user_id=None,
secret=None,
):
"""
钉钉报警,user_phone与user_id 二选一即可
Args:
message:
message_prefix: 消息摘要,用于去重
rate_limit: 包名频率,单位秒,相同的报警内容在rate_limit时间内只会报警一次
url: 钉钉报警url
user_phone: 被@的群成员手机号,支持列表,可指定多个。
user_id: 被@的群成员userId,支持列表,可指定多个
secret: 钉钉报警加签密钥
Returns:
"""
# 为了加载最新的配置
rate_limit = rate_limit if rate_limit is not None else setting.WARNING_INTERVAL
url = url or setting.DINGDING_WARNING_URL
user_phone = user_phone or setting.DINGDING_WARNING_PHONE
user_id = user_id or setting.DINGDING_WARNING_USER_ID
secret = secret or setting.DINGDING_WARNING_SECRET
if secret:
timestamp = str(round(time.time() * 1000))
secret_enc = secret.encode('utf-8')
string_to_sign_enc = f'{timestamp}\n{secret}'.encode('utf-8')
hmac_code = hmac.new(secret_enc, string_to_sign_enc, digestmod=hashlib.sha256).digest()
secret_enc = secret.encode("utf-8")
string_to_sign_enc = f"{timestamp}\n{secret}".encode("utf-8")
hmac_code = hmac.new(
secret_enc, string_to_sign_enc, digestmod=hashlib.sha256
).digest()
sign = urllib.parse.quote_plus(base64.b64encode(hmac_code))
url = f"{url}&timestamp={timestamp}&sign={sign}"

Expand All @@ -2492,10 +2515,17 @@ def dingding_warning(
if isinstance(user_phone, str):
user_phone = [user_phone] if user_phone else []

if isinstance(user_id, str):
user_id = [user_id] if user_id else []

data = {
"msgtype": "text",
"text": {"content": message},
"at": {"atMobiles": user_phone, "isAtAll": setting.DINGDING_WARNING_ALL},
"at": {
"atMobiles": user_phone,
"atUserIds": user_id,
"isAtAll": setting.DINGDING_WARNING_ALL,
},
}

headers = {"Content-Type": "application/json"}
Expand Down

0 comments on commit 033feba

Please sign in to comment.