From 38b5ec1058e284711a04ea9a74107de8c6aef20b Mon Sep 17 00:00:00 2001 From: hibobmaster <32976627+hibobmaster@users.noreply.github.com> Date: Tue, 20 Feb 2024 11:28:29 +0800 Subject: [PATCH] Publish --- .gitignore | 3 ++ README.md | 13 +++++++ main.py | 33 +++++++++++++++++ requirements.txt | 4 ++ utils.py | 4 ++ weather.py | 88 ++++++++++++++++++++++++++++++++++++++++++++ weather_bot.session | Bin 0 -> 28672 bytes 7 files changed, 145 insertions(+) create mode 100644 .gitignore create mode 100644 README.md create mode 100644 main.py create mode 100644 requirements.txt create mode 100644 utils.py create mode 100644 weather.py create mode 100644 weather_bot.session 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 0000000000000000000000000000000000000000..5d0f0ab1fa9dfd9bc11e34e11272fdd634605809 GIT binary patch literal 28672 zcmeI(e`p(J7zgn8()2RAX4iJrP-ET2GMbE9Y`bX7P}`0=p{-&quE^v_m$%KaQ+~p7YTuiPZ1w;@?y(|+#iulXKU&)fo8^&aZ*Fsu* zv1Spe)P~IFCy2OGAkyHf0rQT0(cH*8C=h@E1Rwwb2tWV=5cn?yA_mc9S-V!)wTZbp zy)+bXyBJk;#^2N1rj2b)4ed>GdqZPOlbrsEY|oQ36xA)O8l&AbCI+i18V zV{Cxw$>7WZJ=p@{NiY2sI_*gYEXctTsWX}^Hk;6QPXa-w8dJJdFV$16+<5@0@kOTZ zb2}Bc+D&yPcY6JunWq&$g`%rZW=Wle16d+c*qKk{ap|7q>@!ECT_bxc_X_!+6f5L- z8jgi_HMyc?lT9G1=BB-RkC#7ADXtLnC;CbXN~ub1D(Cvjs=J+)&RSu(b|Z7nNnj&CRkP26{CFj^P9_jlu{W$*U-*y|1DKKHlml>M37*jV)F znWrZ%ZT{#;u6^|E`1MymyQ}e?;@hXiLg&ny7v5@ZdvWu?2d`YX>F{5UokH~Iq3My| zSe?JJBfS5QLya@fJ+9RM`01BF2cGYLt#I$lqqga(@Ub(eH~ug(eI#ecL49Oj z`-z_(-F=*lwRVR)g!*&iUj;U(TkdcEaFRNF&Y9{5qhB z|D|Q9cp#=7Iv@3aBffq5*5S@|w_SbLCQqKMEjpJ*rFV4bi!Z zP83~{z9$9!)jZHfN{n4c>FNVl-cR)T)Z+c~+Rb10(h2K@LcVQ(cC)^)d#rIVKEy*x zGQ>({@&Et%rhgO&KmY;|fB*y_009U<00Izz00fq?Kwf-qc>Z6;R~S=+00bZa0SG_< z0uX=z1Rwwb2$0$R|I#(W4-^PM00Izz00bZa0SG_<0uX=z1eT3JJ`stq!YDk%_XV7z Fe*