Publish
This commit is contained in:
commit
38b5ec1058
7 changed files with 145 additions and 0 deletions
3
.gitignore
vendored
Normal file
3
.gitignore
vendored
Normal file
|
@ -0,0 +1,3 @@
|
||||||
|
config.json
|
||||||
|
__pycache__
|
||||||
|
venv
|
13
README.md
Normal file
13
README.md
Normal file
|
@ -0,0 +1,13 @@
|
||||||
|
## 简单的天气预报
|
||||||
|
爬取爷爷那的天气信息,然后手动发短信给他。
|
||||||
|
|
||||||
|
由于每天有用到telegram,这里直接结合一下机器人,用定时任务(每天早上八点)发送消息给本人
|
||||||
|
|
||||||
|
tg相关配置信息 `config.json`
|
||||||
|
```
|
||||||
|
{
|
||||||
|
"api_id": xxxxx,
|
||||||
|
"api_hash": "xxxxx",
|
||||||
|
"bot_token": "xxxxx"
|
||||||
|
}
|
||||||
|
```
|
33
main.py
Normal file
33
main.py
Normal file
|
@ -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()
|
4
requirements.txt
Normal file
4
requirements.txt
Normal file
|
@ -0,0 +1,4 @@
|
||||||
|
requests
|
||||||
|
pytz
|
||||||
|
apscheduler
|
||||||
|
telethon
|
4
utils.py
Normal file
4
utils.py
Normal file
|
@ -0,0 +1,4 @@
|
||||||
|
import time
|
||||||
|
|
||||||
|
def get_unix_time() -> float:
|
||||||
|
return time.time()
|
88
weather.py
Normal file
88
weather.py
Normal file
|
@ -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}")
|
BIN
weather_bot.session
Normal file
BIN
weather_bot.session
Normal file
Binary file not shown.
Loading…
Reference in a new issue