fix some stuff

This commit is contained in:
JerryXiao 2023-05-29 22:50:32 +08:00
parent a321dd5284
commit 5792891394
Signed by: Jerry
GPG key ID: 22618F758B5BE2E5
3 changed files with 10 additions and 9 deletions

View file

@ -13,6 +13,7 @@ F_KNOWN_OUTPUT_OVERRIDE = 'known_output_override.py'
LIB_DIR = Path('/var/lib/pacroller') LIB_DIR = Path('/var/lib/pacroller')
DB_FILE = 'db' DB_FILE = 'db'
NEWS_FILE = 'news' NEWS_FILE = 'news'
DEF_HTTP_HDRS = {'User-Agent': 'Mozilla/5.0 (compatible; Pacroller/0.1; +https://github.com/isjerryxiao/pacroller)'}
LOG_DIR = Path('/var/log/pacroller') LOG_DIR = Path('/var/log/pacroller')
PACMAN_CONFIG = '/etc/pacman.conf' PACMAN_CONFIG = '/etc/pacman.conf'
PACMAN_LOG = '/var/log/pacman.log' PACMAN_LOG = '/var/log/pacman.log'

View file

@ -5,7 +5,7 @@ from typing import List
import logging import logging
import urllib.request, urllib.parse import urllib.request, urllib.parse
from platform import node from platform import node
from pacroller.config import NETWORK_RETRY, SMTP_ENABLED, SMTP_SSL, SMTP_HOST, SMTP_PORT, SMTP_FROM, SMTP_TO, SMTP_AUTH, TG_ENABLED, TG_BOT_TOKEN, TG_API_HOST, TG_RECIPIENT from pacroller.config import NETWORK_RETRY, SMTP_ENABLED, SMTP_SSL, SMTP_HOST, SMTP_PORT, SMTP_FROM, SMTP_TO, SMTP_AUTH, TG_ENABLED, TG_BOT_TOKEN, TG_API_HOST, TG_RECIPIENT, DEF_HTTP_HDRS
logger = logging.getLogger() logger = logging.getLogger()
hostname = node() or "unknown-host" hostname = node() or "unknown-host"
@ -42,14 +42,14 @@ def send_tg_message(text: str, subject: str, mailto: List[str]) -> bool:
try: try:
for recipient in mailto: for recipient in mailto:
url = f'https://{TG_API_HOST}/bot{TG_BOT_TOKEN}/sendMessage' url = f'https://{TG_API_HOST}/bot{TG_BOT_TOKEN}/sendMessage'
headers = {'User-Agent': 'Mozilla/5.0 (compatible; Pacroller/0.1; +https://github.com/isjerryxiao/pacroller)'} headers = {'Content-Type': 'application/json'}
data = urllib.parse.urlencode({"chat_id": recipient, "text": f"<b>{subject}</b>\n\n<code>{text}</code>", "parse_mode": "HTML"}) data = json.dumps({"chat_id": recipient, "text": f"<b>{subject}</b>\n\n<code>{text[:4000]}</code>", "parse_mode": "HTML"})
req = urllib.request.Request(url, data=data.encode(), headers=headers) req = urllib.request.Request(url, data=data.encode('utf-8'), headers={**DEF_HTTP_HDRS, **headers})
resp = urllib.request.urlopen(req).read().decode('utf-8') resp = urllib.request.urlopen(req).read().decode('utf-8')
content = json.loads(resp) content = json.loads(resp)
if not content["ok"]: if not content.get("ok"):
all_succeeded = False all_succeeded = False
logger.error(f"unable to send telegram message to {recipient}: %s" % content['description']) logger.error(f"unable to send telegram message to {recipient}: {content.get('description')}")
except Exception: except Exception:
logger.exception("error while send_tg_message") logger.exception("error while send_tg_message")
else: else:
@ -79,4 +79,4 @@ class MailSender:
if __name__ == "__main__": if __name__ == "__main__":
logging.basicConfig(level=logging.DEBUG, format='%(asctime)s - %(module)s - %(funcName)s - %(levelname)s - %(message)s') logging.basicConfig(level=logging.DEBUG, format='%(asctime)s - %(module)s - %(funcName)s - %(levelname)s - %(message)s')
MailSender().send_text_plain("This is a test mail\nIf you see this mail, your notification config is working.") print(MailSender().send_text_plain("This is a test mail\nIf you see this mail, your notification config is working."))

View file

@ -1,11 +1,11 @@
import urllib.request import urllib.request
from xml.etree import ElementTree as etree from xml.etree import ElementTree as etree
from typing import List from typing import List
from pacroller.config import DEF_HTTP_HDRS
def get_news(old_news: str) -> List[str]: def get_news(old_news: str) -> List[str]:
ARCH_RSS_URL = 'https://archlinux.org/feeds/news/' ARCH_RSS_URL = 'https://archlinux.org/feeds/news/'
headers = {'User-Agent': 'Mozilla/5.0 (compatible; Pacroller/0.1; +https://github.com/isjerryxiao/pacroller)'} req = urllib.request.Request(ARCH_RSS_URL, data=None, headers=DEF_HTTP_HDRS)
req = urllib.request.Request(ARCH_RSS_URL, data=None, headers=headers)
rss_text = urllib.request.urlopen(req).read().decode('utf-8') rss_text = urllib.request.urlopen(req).read().decode('utf-8')
xml_root = etree.fromstring(rss_text) xml_root = etree.fromstring(rss_text)