36 Commits

Author SHA1 Message Date
RTAkland
cc8bed1890 feat: update README.md 2022-01-01 15:33:20 +08:00
RTAkland
9174e11ecd feat: fix bug 2022-01-01 15:32:38 +08:00
RTAkland
31ea638770 feat: update version 2021-12-26 14:13:52 +08:00
RTAkland
11244734fd feat: update version 2021-12-26 14:13:31 +08:00
RTAkland
9cbd3b5063 feat: fix a bug 2021-12-26 14:13:09 +08:00
RTAkland
3622b094fd feat: add 404 page 2021-12-26 14:12:40 +08:00
RTAkland
4c73240fe9 feat: update language files 2021-12-26 14:12:24 +08:00
RTAkland
451aed59d0 feat: del 2021-12-26 10:34:10 +08:00
RTAkland
0b41c9c1f5 feat: update README.md 2021-12-26 10:00:07 +08:00
RTAkland
b205533ae7 feat: del 2021-12-26 09:39:03 +08:00
RTAkland
b493c06387 feat: update .gitignore 2021-12-26 09:36:33 +08:00
RTAkland
936dd14a87 feat: updated 2021-12-25 12:18:22 +08:00
RTAkland
21a7f9140d feat: updated 2021-12-25 12:15:13 +08:00
RTAkland
23f44c3f78 feat: fixed a bug 2021-12-25 12:14:53 +08:00
RTAkland
9477c33952 feat: update 2021-12-25 12:09:30 +08:00
RTAkland
7fdd0d56a1 feat: fixed a bug 2021-12-25 12:08:52 +08:00
RTAkland
2117771518 feat: update README.md 2021-12-24 19:52:11 +08:00
RTAkland
ea4d04a71c feat: add hitokoto 2021-12-24 19:51:49 +08:00
RTAkland
235e252bab feat: update README.md 2021-12-24 18:58:26 +08:00
RTAkland
278913b2c3 update language files 2021-12-24 18:57:25 +08:00
RTAkland
3cc8afa1f4 feat: optimized output information 2021-12-24 18:56:16 +08:00
RTAkland
3034346c73 feat: removed some text 2021-12-24 18:55:11 +08:00
RTAkland
7b4a0dd033 feat: update 2021-12-23 21:09:19 +08:00
RTAkland
c9ec788cd3 feat: update version 2021-12-21 21:37:53 +08:00
RTAkland
30240033cf feat: update language files 2021-12-21 21:37:33 +08:00
RTAkland
f986a037a8 feat: added SSL 2021-12-21 21:37:13 +08:00
RTAkland
1b96caf719 feat: fixed a bug 2021-12-21 21:36:29 +08:00
RTAkland
df1d5a13b4 feat: added a small info 2021-12-21 20:59:05 +08:00
RTAkland
2c111536c7 feat: update version 2021-12-21 20:57:56 +08:00
RTAkland
1bedb05ac1 feat: rename webserver -> webservice 2021-12-21 20:56:14 +08:00
RTAkland
fd10791baa feat: update language files 2021-12-21 20:55:39 +08:00
RTAkland
98b4b9305b feat: rename file 2021-12-21 20:55:09 +08:00
RTAkland
62d08096b3 feat: added get_host_ip.py 2021-12-21 20:54:38 +08:00
RTAkland
c32efe4b44 feat: added webservice host ip info 2021-12-21 20:54:02 +08:00
RTAkland
9e3f866fb8 feat: fixed a small bug 2021-12-21 20:53:35 +08:00
RTAkland
e8b1a910ae feat: fixed a bug 2021-12-20 22:03:49 +08:00
12 changed files with 165 additions and 81 deletions

1
.gitignore vendored
View File

@@ -1,3 +1,4 @@
test.py
*.yml
!config.yml
*.log

View File

@@ -1,14 +1,14 @@
<p align="center">
<a href="https://github.com/MarkusJoe/QWeather">
<img src="https://img.shields.io/badge/Python-3.10.x-blue.svg" alt="PythonVersion">
<img src="https://img.shields.io/badge/release-3.1.1b-green.svg" alt="QWeatherVersion">
<img src="https://img.shields.io/badge/release-3.2.3-green.svg" alt="QWeatherVersion">
<img src="https://img.shields.io/badge/LINCESE-Apache2.0-orange.svg" alt="LICENSE">
</a>
</p>
<div align="center">
## *QWeather*
## *QWeatherReporter*
<i style="text-align: center;"><a href="https://www.qweather.com/">QWeather Official Website</a></i>\
<i style="text-align: center;"><a href="https://dev.qweather.com/">QWeather Development Platform</a></i>
@@ -20,32 +20,33 @@
<b><i><a href="https://markusjoe.github.io/" target="_blank">点击跳转到帮助文档</a></i></b>
</div>
## 声明
- > 程序作者: **RTAkland (markushammered@gmail.com)**
- > 和风天气开发者平台https://dev.qweather.com
- > 和风天气官网: https://qweather.com
### 开源
- 本项目以[Apache-2.0](./LICENSE)许可开源, 即:
- 你可以直接使用该项目提供的功能, 无需任何授权
- 你可以在**注明来源版权信息**的情况下对源代码进行任意分发和修改以及衍生
### 已实现功能
- [x] 发送免费版&开发板右键
- [x] 发送免费版&开发版天气预报功能
- [x] 间隔10分钟请求一次自然灾害预警信息
- [x] 在网页上快速查看天气
- [x] 在邮件html内附带一句一言
- [ ] ~~推送到QQ(咕咕咕)~~
### 问题汇总
#### Python 版本:
> 程序使用了Python3.10中的match-case语句
> 请使用Python3.10版本运行
> 程序使用了*Python3.10.x*中的match-case语句
> 请使用*Python3.10.x*版本运行
#### 主题选择:
> 在2.9.0之后的版本不支持自定义天气图标 只能使用最新的图标
#### 配置填写问题:
> `location`项和`only-view-settings`类不需要用户填写, 只需要打开`QWeather.py`进行自助填写
## 声明
- > 程序作者: **RTAkland (markushammered@gmail.com)**
- > 和风天气开发者平台https://dev.qweather.com
- > 和风天气官网: https://qweather.com
## 如何使用
- 程序基于python3.10开发 务必使用python3.10版本运行
- 将config.yml正确填写完成
@@ -54,8 +55,9 @@
### 网页上查看天气
- 将所有准备工作完成(能正常运行QWeather.py)
- 运行QWeather.py
- 打开浏览器输入***127.0.0.1***:7898 (127.0.0.1可以更改为部署本项目的服务器ip)
- 运行`QWeather.py`
- 打开浏览器输入**127.0.0.1:7898**
>127.0.0.1可以更改为部署本项目的服务器ip, 7898端口不能被其他程序占用或不开放此端口
## 联系方式

View File

@@ -103,7 +103,12 @@ client-settings:
# [选填/只能一个] 开启本地网页快速查看天气
# 默认: False
# 可选: "True" "False"
webserver: false
webservice: false
# [必填/只能一个] 是否使用SSL 连接到邮箱服务器
# 默认 True
# 可选: "True" "False"
SSL: True
# [标识/不填] 仅作用户读取标识

View File

@@ -6,7 +6,7 @@
# @File Name: information.py
import requests
from core import read_config
from core.read_config import read_config
class WeatherInfo:

View File

@@ -13,7 +13,7 @@ import time
level = read_config()[2]['level']
date_format = '%H:%M:%S'
info_format_console = '%(log_color)s[%(asctime)s] |%(filename)s[ %(lineno)-3s] |%(levelname)-8s |%(message)s'
info_format_console = '%(log_color)s[%(asctime)s] |%(filename)s[%(lineno)-3s] |%(levelname)-8s |%(message)s'
info_format_file = '[%(asctime)s] |%(filename)s[%(funcName)sline:%(lineno)d] |%(levelname)-8s |%(message)s'
formatter = ColoredFormatter(fmt=info_format_console,
datefmt=date_format,

View File

@@ -13,9 +13,13 @@ from concurrent.futures import ProcessPoolExecutor
from core.logger import Logger
from core.language import Language
from core.settings import change_settings
from lib import webserver
from core.read_config import read_config
from core.sendmail import Mail
from lib.webservice import accept_requests
language = Language()
settings = read_config()
processes = ProcessPoolExecutor(max_workers=3)
def check_time():
@@ -83,6 +87,12 @@ def main():
主程序
:return:
"""
Logger.info(f'{language["statement_1"]}')
Logger.info(f'{language["statement_2"]}')
Logger.info(f'{language["statement_3"]}')
Logger.info(f'{language["statement_4"]}')
parser = argparse.ArgumentParser()
arg_keywords = ['free', 'dev', 'warning', 'setting']
parser.add_argument('-t',
@@ -105,16 +115,18 @@ def main():
Logger.debug(f'{language["debug_done"]}')
sys.exit(0)
case 'setting':
change_settings()
Logger.debug(f'{language["debug_done"]}')
case _:
if check_config():
setting()
else:
change_settings()
Logger.debug(f'{language["debug_done"]}')
case _:
pass
processes.submit(check_time)
if settings[2]['webserver']:
processes.submit(webserver.process_request())
if settings[2]['webservice']:
Logger.info(f'{language["webservice_ip"]}:127.0.0.1:7898')
processes.submit(accept_requests())
time_count = 0
while True:
@@ -123,14 +135,3 @@ def main():
if time_count == 600:
Mail().warning_()
time_count = 0
if __name__ != '__main__':
language = Language()
settings = read_config()
processes = ProcessPoolExecutor(max_workers=3)
Logger.info(f'{language["statement_1"]}')
Logger.info(f'{language["statement_2"]}')
Logger.info(f'{language["statement_3"]}')
Logger.info(f'{language["statement_4"]}')

View File

@@ -16,6 +16,7 @@ from core.logger import Logger
from core.language import Language
from core.information import WeatherInfo
from core.read_config import read_config
from lib.hitokoto import hitokoto
class Mail:
@@ -33,6 +34,8 @@ class Mail:
self.message['From'] = Header('QWeather') # 发件人名称
self.message['To'] = Header('All allowed User') # 收件人显示名称
self.hitokoto = hitokoto() # 一言
if self.enableSSL:
self.smtp = smtplib.SMTP_SSL(self.server, self.port) # 登录服务器 使用SSL连接
else:
@@ -128,8 +131,8 @@ class Mail:
<th>&nbsp;</th>
</tr>
<tr>
<th>风速/级/向</th>
<th>相对湿度</th>
<th>风速/级/向</th>
<th>湿度</th>
<th>紫外线</th>
</tr>
<tr>
@@ -142,8 +145,8 @@ class Mail:
</tr>
<tr>
<th>能见度</th>
<th>大气压强</th>
<th>相对云量</th>
<th>压强</th>
<th>云量</th>
</tr>
<tr>
<td>{vis}km&nbsp;</td>
@@ -166,9 +169,13 @@ class Mail:
<td>&nbsp;{sunset}&nbsp;</td>
</tr>
</table>
<br />
<div style="text-align: center" id="hitokoto">
<p>
{self.hitokoto}
</p>
</div>
<div style="text-align: center;" id="About">
<br />
<br />
<br />
<i>
<b>
@@ -271,8 +278,8 @@ class Mail:
<th>&nbsp;</th>
</tr>
<tr>
<th>风速/级/向</th>
<th>相对湿度</th>
<th>风速/级/向</th>
<th>湿度</th>
<th>紫外线</th>
</tr>
<tr>
@@ -285,8 +292,8 @@ class Mail:
</tr>
<tr>
<th>能见度</th>
<th>大气压强</th>
<th>相对云量</th>
<th>压强</th>
<th>云量</th>
</tr>
<tr>
<td>{vis}km&nbsp;</td>
@@ -309,9 +316,13 @@ class Mail:
<td>&nbsp;{sunset}&nbsp;</td>
</tr>
</table>
<br />
<div style="text-align: center" id="hitokoto">
<p>
{self.hitokoto}
</p>
</div>
<div style="text-align: center;" id="About">
<br />
<br />
<br />
<i>
<b>

23
lib/hitokoto.py Normal file
View File

@@ -0,0 +1,23 @@
# -- coding:utf-8 --
# @Author: markushammered@gmail.com
# @Development Tool: PyCharm
# @Create Time: 2021/12/24
# @File Name: hitokoto.py
import requests
from core.language import Language
from core.logger import Logger
def hitokoto():
language = Language()
url = 'https://v1.hitokoto.cn/'
Logger.info(f'{language["hitokoto"]}')
res = requests.get(url).json()
speaker = res['from_who']
text = res['hitokoto']
if not speaker:
speaker = language['noname']
full_text = f'{text} --{speaker}'
return full_text

View File

@@ -3,22 +3,29 @@
# @Author: markushammered@gmail.com
# @Development Tool: PyCharm
# @Create Time: 2021/12/18
# @File Name: webserver.py
# @File Name: webservice.py
import socket
import sys
import threading
from core.logger import Logger
from core.read_config import read_config
from core.information import WeatherInfo
from core.language import Language
server = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
server.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
server.bind(('127.0.0.1', 7898))
server.bind(('0.0.0.0', 7898))
server.listen(5)
language = Language()
def build_html():
"""
构建html主体
:return:
"""
settings = read_config()
city = settings[3]['city-name']
mode = settings[1]['mode']
@@ -47,7 +54,7 @@ def build_html():
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
<title>QWeather|网页查看天气服务</title>
</head>
<body>
<p style="text-align: center"><i><b>地区:{city}</b></i></p>
@@ -115,8 +122,8 @@ def build_html():
<th>&nbsp;</th>
</tr>
<tr>
<th>风速//</th>
<th>相对湿度</th>
<th>风速//</th>
<th>湿度</th>
<th>紫外线</th>
</tr>
<tr>
@@ -129,8 +136,8 @@ def build_html():
</tr>
<tr>
<th>能见度</th>
<th>大气压强</th>
<th>相对云量</th>
<th>压强</th>
<th>云量</th>
</tr>
<tr>
<td>{vis}km&nbsp;</td>
@@ -192,7 +199,7 @@ def build_html():
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
<title>QWeather|网页查看天气服务</title>
</head>
<body>
<p style="text-align: center"><i><b>地区:{city}</b></i></p>
@@ -245,8 +252,8 @@ def build_html():
</tr>
<tr>
<th>能见度</th>
<th>大气压强</th>
<th>相对云量</th>
<th>压强</th>
<th>云量</th>
</tr>
<tr>
<td>{vis}km&nbsp;</td>
@@ -288,28 +295,40 @@ def build_html():
return "You hadn't selected a mode"
def process_request():
def process_requests(c, a):
"""
处理请求
:param c: connection
:param a: address
:return:
"""
try:
while True:
c, a = server.accept()
data = str(c.recv(1024)).split(':')[0][6:][:-17]
html = build_html()
if data == '/':
c.send('HTTP1.1/ 200 OK\r\n\r\n'.encode('utf-8'))
c.send(html.encode('utf-8'))
Logger.info(f'{a}: Get {data} --by browser')
else:
try:
with open(f'.{data}', 'rb') as f:
c.send('HTTP1.1/ 200 OK\r\n\r\n'.encode('utf-8'))
c.send(f.read())
Logger.info(f'{a}: Get {data} --by browser')
except FileNotFoundError:
c.send(f'HTTP1.1/ 404 Not Found\r\n\r\n{html}'.encode('utf-8'))
c.close()
data = str(c.recv(1024)).split(':')[0][6:][:-17]
html = build_html()
if data == '/': # 判断用户请求的目标是否为根目录, 如果是则返回html; 如果不是则继续判断
c.send('HTTP/1.1 200 OK\r\n\r\n'.encode('utf-8'))
c.send(html.encode('utf-8'))
Logger.info(f'{language["get_resource"]} {data} {language["get_resource_from"]} {a[0]}:{a[1]}')
try:
with open(f'./{data}', 'rb') as f:
c.send('HTTP/1.1 200 OK\r\n\r\n'.encode('utf-8'))
c.send(f.read())
Logger.info(f'{language["get_resource"]} {data} {language["get_resource_from"]} {a[0]}:{a[1]}')
except FileNotFoundError:
with open('./res/basic-resources/404.html', 'r') as not_found:
c.send(f'HTTP/1.1 404 Not Found\r\n\r\n{not_found.read()}'.encode('utf-8'))
except BrokenPipeError:
Logger.critical('Link speed was too fast! Subprocess:webserver exited')
sys.exit(1)
except IOError:
Logger.critical('An IO Error')
sys.exit(1)
Logger.error(f'{language["connection_speed_too_fast"]}')
finally:
c.close()
return
def accept_requests():
"""
接受请求
:return:
"""
while True:
c, a = server.accept()
threading.Thread(target=process_requests, args=(c, a,)).start()

View File

@@ -0,0 +1,10 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>QWeather|网页查看天气服务</title>
</head>
<body>
<i><b><p style="text-align: center; color: red">404 File Not Found</p></b></i>
</body>
</html>

View File

@@ -31,5 +31,11 @@
"language_not_filled": "Language hasn't filled, default: zh_cn.",
"no_location": "You haven't fill the location in the config file.",
"change_setting": "You are changing setting.",
"exit": "Exited."
"exit": "Exited.",
"webservice_ip": "Local webservice ip",
"connection_speed_too_fast": "Connection speed is too fast",
"noname": "No name.",
"hitokoto": "Get hitokoto successfully.",
"get_resource": "Get resource",
"get_resource_from": "from"
}

View File

@@ -30,5 +30,11 @@
"language_file_read_successfully": "多语言模块加载成功",
"language_not_filled": "语言选项为填写完成或填写错误, 默认zh_cn",
"change_setting": "更改设置",
"exit": "已退出"
"exit": "已退出",
"webservice_ip": "webservice地址",
"connection_speed_too_fast": "连接速度过快",
"noname": "佚名",
"hitokoto": "一言获取成功",
"get_resource": "获取资源",
"get_resource_from": "来自"
}