-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
80 lines (76 loc) · 2.51 KB
/
main.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
#导入依赖库
import json
import sys
import requests
import time
import hashlib
import base64
import urllib
import hmac
import os
import datetime
from bs4 import BeautifulSoup
def encrypt(secret):
timestamp = str(round(time.time() * 1000))
secret_enc = secret.encode('utf-8')
string_to_sign = '{}\n{}'.format(timestamp, secret)
string_to_sign_enc = string_to_sign.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))
return timestamp,sign
def fetch_electricity_fee(api_url):
# 发送请求获取HTML内容
response = requests.get(api_url)
if response.status_code == 200:
# 使用BeautifulSoup解析HTML
soup = BeautifulSoup(response.text, 'html.parser')
# 找到包含电费信息的元素
price_element = soup.find('span', class_='price')
if price_element:
price_text = price_element.text.strip()
price_text = price_text.replace("元", "")
# 提取电费信息并返回
return price_text
else:
return "null"
else:
return "error"
if __name__=='__main__':
#定义数据类型
headers={
"Content-Type": "application/json",
"Charset": "UTF-8"
}
secret = os.getenv('SECRET')
access_token = os.getenv('TOKEN')
timestamp,sign=encrypt(secret)
#定义webhook,从钉钉群机器人设置页面复制获得
webhook = 'https://oapi.dingtalk.com/robot/send?access_token='+access_token+'×tamp='+timestamp+'&sign='+sign
# API URL
api_url = os.getenv('URL')
text="好好好~~~牛魔*zc*洗澡!————hby\n"+"**剩余电费**"+fetch_electricity_fee(api_url)+"\n"
data = {
"msgtype": "markdown",
"markdown": {
"title": "牛魔们好哇",
"text": text
},
"at": {
"isAtAll": True # @全体成员
}
}
requests.post(webhook, data=json.dumps(data), headers=headers)
current_date = datetime.date.today()
# if current_date.weekday()==6:
text2="牛魔hby提醒大家***记单词***啦~\n"+"牛魔zc提醒大家少撸管,多休息\n"
data2 = {
"msgtype": "markdown",
"markdown": {
"title": "牛魔们好哇",
"text": text2
},
"at": {
"isAtAll": True # @全体成员
}
}
requests.post(webhook, data=json.dumps(data2), headers=headers)