commit 38b5ec1058e284711a04ea9a74107de8c6aef20b Author: hibobmaster <32976627+hibobmaster@users.noreply.github.com> Date: Tue Feb 20 11:28:29 2024 +0800 Publish diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..e22c576 --- /dev/null +++ b/.gitignore @@ -0,0 +1,3 @@ +config.json +__pycache__ +venv \ No newline at end of file diff --git a/README.md b/README.md new file mode 100644 index 0000000..2f60b15 --- /dev/null +++ b/README.md @@ -0,0 +1,13 @@ +## 简单的天气预报 +爬取爷爷那的天气信息,然后手动发短信给他。 + +由于每天有用到telegram,这里直接结合一下机器人,用定时任务(每天早上八点)发送消息给本人 + +tg相关配置信息 `config.json` +``` +{ + "api_id": xxxxx, + "api_hash": "xxxxx", + "bot_token": "xxxxx" +} +``` \ No newline at end of file diff --git a/main.py b/main.py new file mode 100644 index 0000000..5fd8854 --- /dev/null +++ b/main.py @@ -0,0 +1,33 @@ +import json +from telethon import TelegramClient +from apscheduler.schedulers.asyncio import AsyncIOScheduler +from datetime import datetime +from pytz import timezone +from weather import get_weather_report + +with open('config.json') as f: + config = json.loads(f.read()) + +api_id = config["api_id"] +api_hash = config["api_hash"] +bot_token = config["bot_token"] +bot = TelegramClient('weather_bot', api_id, api_hash).start(bot_token=bot_token) + +async def send_weather_report(): + weather_report = get_weather_report() + print(weather_report) + await bot.send_message("hiqianxue", weather_report) + + + +# Define your target time in UTC+8 +target_time = datetime.now(timezone('Asia/Shanghai')).replace(hour=8, minute=0, second=0) +target_time_utc = target_time.astimezone(timezone('UTC')) +scheduler = AsyncIOScheduler() +scheduler.add_job(send_weather_report, 'interval', days=1, next_run_time=target_time_utc) +scheduler.start() + + + +with bot: + bot.loop.run_forever() \ No newline at end of file diff --git a/requirements.txt b/requirements.txt new file mode 100644 index 0000000..e365815 --- /dev/null +++ b/requirements.txt @@ -0,0 +1,4 @@ +requests +pytz +apscheduler +telethon \ No newline at end of file diff --git a/utils.py b/utils.py new file mode 100644 index 0000000..d9ede08 --- /dev/null +++ b/utils.py @@ -0,0 +1,4 @@ +import time + +def get_unix_time() -> float: + return time.time() \ No newline at end of file diff --git a/weather.py b/weather.py new file mode 100644 index 0000000..0ef3a6d --- /dev/null +++ b/weather.py @@ -0,0 +1,88 @@ +# 桂林市天气数据 +# 数据来源: http://www.nmc.cn +# API: http://www.nmc.cn/rest/weather?stationid=57957 +# Response: json + +import requests +from datetime import datetime + + +API_URL = "http://www.nmc.cn/rest/weather" +CITY = "桂林市" + +headers = { + 'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:122.0) Gecko/20100101 Firefox/122.0', + 'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,*/*;q=0.8', + 'Accept-Language': 'en-US,en;q=0.5', + # 'Accept-Encoding': 'gzip, deflate', + 'DNT': '1', + 'Sec-GPC': '1', + 'Connection': 'keep-alive', + 'Upgrade-Insecure-Requests': '1', +} + +params = { + 'stationid': '57957', +} + +def make_get_request(): + response = requests.get(API_URL, params=params, headers=headers) + response.raise_for_status() + return response.json() + +def get_weather_report() -> str: + try: + response = make_get_request() + if "success" != response["msg"]: + raise ValueError + + current_date = response['data']['predict']['detail'][0]['date'] + current_date_object = datetime.strptime(current_date, "%Y-%m-%d") + year = current_date_object.year + month = current_date_object.month + day = current_date_object.day + # 当天白天天气信息 + current_date_day_weather_info = response['data']['predict']['detail'][0]['day']['weather']['info'] + current_date_day_weather_temperature = response['data']['predict']['detail'][0]['day']['weather']['temperature'] + current_date_day_wind_direct = response['data']['predict']['detail'][0]['day']['wind']['direct'] + current_date_day_wind_power = response['data']['predict']['detail'][0]['day']['wind']['power'] + # 当天夜晚天气信息 + current_date_night_weather_info = response['data']['predict']['detail'][0]['night']['weather']['info'] + current_date_night_weather_temperature = response['data']['predict']['detail'][0]['night']['weather']['temperature'] + current_date_night_wind_direct = response['data']['predict']['detail'][0]['night']['wind']['direct'] + current_date_night_wind_power = response['data']['predict']['detail'][0]['night']['wind']['power'] + + # 明天白天天气信息 + next_date_day_weather_info = response['data']['predict']['detail'][1]['day']['weather']['info'] + next_date_day_weather_temperature = response['data']['predict']['detail'][1]['day']['weather']['temperature'] + next_date_day_wind_direct = response['data']['predict']['detail'][1]['day']['wind']['direct'] + next_date_day_wind_power = response['data']['predict']['detail'][1]['day']['wind']['power'] + # 明天夜晚天气信息 + next_date_night_weather_info = response['data']['predict']['detail'][1]['night']['weather']['info'] + next_date_night_weather_temperature = response['data']['predict']['detail'][1]['night']['weather']['temperature'] + next_date_night_wind_direct = response['data']['predict']['detail'][1]['night']['wind']['direct'] + next_date_night_wind_power = response['data']['predict']['detail'][1]['night']['wind']['power'] + + # 后天白天天气信息 + next_next_date_day_weather_info = response['data']['predict']['detail'][2]['day']['weather']['info'] + next_next_date_day_weather_temperature = response['data']['predict']['detail'][2]['day']['weather']['temperature'] + next_next_date_day_wind_direct = response['data']['predict']['detail'][2]['day']['wind']['direct'] + next_next_date_day_wind_power = response['data']['predict']['detail'][2]['day']['wind']['power'] + # 后天夜晚天气信息 + next_next_date_night_weather_info = response['data']['predict']['detail'][2]['night']['weather']['info'] + next_next_date_night_weather_temperature = response['data']['predict']['detail'][2]['night']['weather']['temperature'] + next_next_date_night_wind_direct = response['data']['predict']['detail'][2]['night']['wind']['direct'] + next_next_date_night_wind_power = response['data']['predict']['detail'][2]['night']['wind']['power'] + + weather_report_text = f"{CITY}{month}月{day}号天气预报,今天{current_date_night_weather_temperature}到{current_date_day_weather_temperature}度," + \ + f"白天{current_date_day_weather_info},{current_date_day_wind_direct}{current_date_day_wind_power}," + \ + f"晚上{current_date_night_weather_info},{current_date_night_wind_direct}{current_date_night_wind_power}," + \ + f"预计明天{next_date_night_weather_temperature}到{next_date_day_weather_temperature}度,{next_date_day_weather_info},{next_date_day_wind_direct}{next_date_day_wind_power}," + \ + f"后天{next_next_date_night_weather_temperature}到{next_next_date_day_weather_temperature}度,{next_next_date_day_weather_info},{next_next_date_day_wind_direct}{next_next_date_day_wind_power}" + + return weather_report_text + + except requests.HTTPError as e: + print(f"网络错误: {e}") + except ValueError as e: + print(f"出错了: {e}") diff --git a/weather_bot.session b/weather_bot.session new file mode 100644 index 0000000..5d0f0ab Binary files /dev/null and b/weather_bot.session differ