mirror of
https://github.com/kxgx/2.13-Ink-screen-clock.git
synced 2026-03-19 07:39:45 +08:00
Compare commits
30 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
0085656d20 | ||
|
|
f8857cbd17 | ||
|
|
90b6ae1403 | ||
|
|
e9b284a9b3 | ||
|
|
7405aabf2c | ||
|
|
ca450afb14 | ||
|
|
c82aadd4e7 | ||
|
|
0a96f265f7 | ||
|
|
298a3f43f1 | ||
|
|
17544b607a | ||
|
|
99780f1bf9 | ||
|
|
118cc0a2ee | ||
|
|
3155088921 | ||
|
|
28f0a9d37d | ||
|
|
fc1678aa95 | ||
|
|
f0eb8dc53e | ||
|
|
9e5114b7bd | ||
|
|
bb44237caa | ||
|
|
e82fd225f5 | ||
|
|
c67ae6bd78 | ||
|
|
72c2b23566 | ||
|
|
590494d734 | ||
|
|
8a29bf9110 | ||
|
|
ab6abaf2e3 | ||
|
|
5436cba3d0 | ||
|
|
70d1b3d710 | ||
|
|
6fa3fe1407 | ||
|
|
cd6fc91cda | ||
|
|
60272f75ea | ||
|
|
faf0e2f505 |
79
app/app.py
79
app/app.py
@@ -1,79 +0,0 @@
|
|||||||
from flask import Flask, render_template, request, send_from_directory, redirect, url_for, jsonify
|
|
||||||
import os
|
|
||||||
import re
|
|
||||||
import subprocess
|
|
||||||
|
|
||||||
app = Flask(__name__, template_folder='webui', static_url_path='', static_folder='webui')
|
|
||||||
FONT_DIR = os.path.join(os.path.dirname(os.path.abspath(__file__)), '..', 'pic') # 字体文件夹路径
|
|
||||||
MAIN_PY_PATH = os.path.join(os.path.dirname(os.path.abspath(__file__)), '..', 'bin', 'main.py')
|
|
||||||
|
|
||||||
def list_font_files(font_dir):
|
|
||||||
try:
|
|
||||||
return os.listdir(font_dir)
|
|
||||||
except Exception as e:
|
|
||||||
print(f"Error listing font files: {e}")
|
|
||||||
return [] # 返回空列表以避免迭代错误
|
|
||||||
|
|
||||||
@app.route('/execute-shell')
|
|
||||||
def execute_shell():
|
|
||||||
# 替换以下命令为您想要执行的Shell命令
|
|
||||||
command = "sudo reboot now"
|
|
||||||
result = subprocess.run(command, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE, text=True)
|
|
||||||
if result.returncode == 0:
|
|
||||||
return jsonify({'status': 'success', 'output': result.stdout})
|
|
||||||
else:
|
|
||||||
return jsonify({'status': 'error', 'output': result.stderr})
|
|
||||||
|
|
||||||
@app.route('/edit_main_py')
|
|
||||||
def edit_main_py():
|
|
||||||
try:
|
|
||||||
with open(MAIN_PY_PATH, 'r') as file:
|
|
||||||
content = file.read()
|
|
||||||
except Exception as e:
|
|
||||||
return f"Error reading main.py: {e}", 500
|
|
||||||
return render_template('edit_main_py.html', content=content)
|
|
||||||
|
|
||||||
@app.route('/save_main_py', methods=['POST'])
|
|
||||||
def save_main_py():
|
|
||||||
new_content = request.form.get('content')
|
|
||||||
if new_content is None:
|
|
||||||
return "No content provided", 400
|
|
||||||
try:
|
|
||||||
with open(MAIN_PY_PATH, 'w') as file:
|
|
||||||
file.write(new_content)
|
|
||||||
except Exception as e:
|
|
||||||
return f"Error saving main.py: {e}", 500
|
|
||||||
return redirect(url_for('index'))
|
|
||||||
|
|
||||||
@app.route('/')
|
|
||||||
def index():
|
|
||||||
font_files = list_font_files(FONT_DIR)
|
|
||||||
return render_template('index.html', font_files=font_files)
|
|
||||||
|
|
||||||
@app.route('/upload', methods=['POST'])
|
|
||||||
def upload():
|
|
||||||
if 'font_file' not in request.files:
|
|
||||||
return '没有文件部分'
|
|
||||||
file = request.files['font_file']
|
|
||||||
if file.filename == '':
|
|
||||||
return '没有选择文件'
|
|
||||||
if file:
|
|
||||||
filename = os.path.join(FONT_DIR, file.filename)
|
|
||||||
file.save(filename)
|
|
||||||
return '文件已上传成功'
|
|
||||||
|
|
||||||
@app.route('/update_font_names')
|
|
||||||
def update_font_names():
|
|
||||||
font_files = list_font_files(FONT_DIR)
|
|
||||||
return render_template('update_font_names.html', font_files=font_files)
|
|
||||||
|
|
||||||
@app.route('/fonts/<filename>')
|
|
||||||
def fonts(filename):
|
|
||||||
return send_from_directory(FONT_DIR, filename)
|
|
||||||
|
|
||||||
if not os.path.exists(FONT_DIR):
|
|
||||||
os.makedirs(FONT_DIR)
|
|
||||||
|
|
||||||
if __name__ == '__main__':
|
|
||||||
# 绑定到0.0.0.0,允许远程访问
|
|
||||||
app.run(host='0.0.0.0', port=80, debug=False)
|
|
||||||
9
app/webui/default.min.css
vendored
9
app/webui/default.min.css
vendored
@@ -1,9 +0,0 @@
|
|||||||
/*!
|
|
||||||
Theme: Default
|
|
||||||
Description: Original highlight.js style
|
|
||||||
Author: (c) Ivan Sagalaev <maniac@softwaremaniacs.org>
|
|
||||||
Maintainer: @highlightjs/core-team
|
|
||||||
Website: https://highlightjs.org/
|
|
||||||
License: see project LICENSE
|
|
||||||
Touched: 2021
|
|
||||||
*/pre code.hljs{display:block;overflow-x:auto;padding:1em}code.hljs{padding:3px 5px}.hljs{background:#f3f3f3;color:#444}.hljs-comment{color:#697070}.hljs-punctuation,.hljs-tag{color:#444a}.hljs-tag .hljs-attr,.hljs-tag .hljs-name{color:#444}.hljs-attribute,.hljs-doctag,.hljs-keyword,.hljs-meta .hljs-keyword,.hljs-name,.hljs-selector-tag{font-weight:700}.hljs-deletion,.hljs-number,.hljs-quote,.hljs-selector-class,.hljs-selector-id,.hljs-string,.hljs-template-tag,.hljs-type{color:#800}.hljs-section,.hljs-title{color:#800;font-weight:700}.hljs-link,.hljs-operator,.hljs-regexp,.hljs-selector-attr,.hljs-selector-pseudo,.hljs-symbol,.hljs-template-variable,.hljs-variable{color:#ab5656}.hljs-literal{color:#695}.hljs-addition,.hljs-built_in,.hljs-bullet,.hljs-code{color:#397300}.hljs-meta{color:#1f7199}.hljs-meta .hljs-string{color:#38a}.hljs-emphasis{font-style:italic}.hljs-strong{font-weight:700}
|
|
||||||
@@ -1,59 +0,0 @@
|
|||||||
<!DOCTYPE html>
|
|
||||||
<html lang="en">
|
|
||||||
<head>
|
|
||||||
<meta charset="UTF-8">
|
|
||||||
<title>编辑 main.py</title>
|
|
||||||
<!-- 引用本地的highlight.js CSS -->
|
|
||||||
<link rel="stylesheet" href="{{ url_for('static', filename='default.min.css') }}">
|
|
||||||
</head>
|
|
||||||
<body>
|
|
||||||
<h1>编辑 main.py</h1>
|
|
||||||
<!-- 添加一个表单来提交编辑后的代码 -->
|
|
||||||
<form method="post" action="{{ url_for('save_main_py') }}">
|
|
||||||
<pre style="background-color: black !important;"><code class="python" id="code">{{ content }}</code></pre>
|
|
||||||
<input type="hidden" name="content" id="hidden-content">
|
|
||||||
<input type="submit" value="保存">
|
|
||||||
</form>
|
|
||||||
|
|
||||||
<!-- 引用本地的highlight.js JavaScript -->
|
|
||||||
<script src="{{ url_for('static', filename='highlight.min.js') }}"></script>
|
|
||||||
<script>
|
|
||||||
// 初始化highlight.js
|
|
||||||
document.addEventListener('DOMContentLoaded', (event) => {
|
|
||||||
hljs.highlightAll();
|
|
||||||
});
|
|
||||||
</script>
|
|
||||||
|
|
||||||
<script>
|
|
||||||
document.addEventListener('DOMContentLoaded', function() {
|
|
||||||
var preElements = document.querySelectorAll('pre.code-editor');
|
|
||||||
preElements.forEach(function(pre) {
|
|
||||||
pre.style.backgroundColor = 'black';
|
|
||||||
});
|
|
||||||
});
|
|
||||||
</script>
|
|
||||||
|
|
||||||
<h2>重启</h2>
|
|
||||||
</head>
|
|
||||||
<body>
|
|
||||||
<button id="executeShellBtn">重启</button>
|
|
||||||
|
|
||||||
<script>
|
|
||||||
document.getElementById('executeShellBtn').addEventListener('click', function() {
|
|
||||||
fetch('/execute-shell')
|
|
||||||
.then(response => response.json())
|
|
||||||
.then(data => {
|
|
||||||
if (data.status === 'success') {
|
|
||||||
alert('Command executed successfully:\n' + data.output);
|
|
||||||
} else {
|
|
||||||
alert('Error executing command:\n' + data.output);
|
|
||||||
}
|
|
||||||
})
|
|
||||||
.catch(error => {
|
|
||||||
alert('Error: ' + error);
|
|
||||||
});
|
|
||||||
});
|
|
||||||
</script>
|
|
||||||
</body>
|
|
||||||
</html>
|
|
||||||
|
|
||||||
1213
app/webui/highlight.min.js
vendored
1213
app/webui/highlight.min.js
vendored
File diff suppressed because one or more lines are too long
@@ -1,27 +0,0 @@
|
|||||||
<!DOCTYPE html>
|
|
||||||
<html lang="en">
|
|
||||||
<head>
|
|
||||||
<meta charset="UTF-8">
|
|
||||||
<title>字体上传和管理</title>
|
|
||||||
</head>
|
|
||||||
<body>
|
|
||||||
<h1>上传字体文件</h1>
|
|
||||||
<form method="post" action="/upload" enctype="multipart/form-data">
|
|
||||||
<input type="file" name="font_file">
|
|
||||||
<input type="submit" value="上传">
|
|
||||||
</form>
|
|
||||||
|
|
||||||
<h2>已上传的字体文件:</h2>
|
|
||||||
<ul>
|
|
||||||
{% for font_file in font_files %}
|
|
||||||
<li>{{ font_file }}</li>
|
|
||||||
{% else %}
|
|
||||||
<li>没有找到字体文件。</li>
|
|
||||||
{% endfor %}
|
|
||||||
</ul>
|
|
||||||
|
|
||||||
<!-- 添加跳转到edit_main_py的按钮 -->
|
|
||||||
<a href="/edit_main_py"><button>编辑main.py</button></a>
|
|
||||||
|
|
||||||
</body>
|
|
||||||
</html>
|
|
||||||
@@ -1,26 +0,0 @@
|
|||||||
<!-- update_font_names.html -->
|
|
||||||
<!DOCTYPE html>
|
|
||||||
<html lang="en">
|
|
||||||
<head>
|
|
||||||
<meta charset="UTF-8">
|
|
||||||
<title>更新字体文件名</title>
|
|
||||||
</head>
|
|
||||||
<body>
|
|
||||||
<h1>更新字体文件名</h1>
|
|
||||||
<form method="post" action="/save_font_names">
|
|
||||||
<label for="font01">字体文件名 01:</label>
|
|
||||||
<input type="text" id="font01" name="font01"><br>
|
|
||||||
<label for="font02">字体文件名 02:</label>
|
|
||||||
<input type="text" id="font02" name="font02"><br>
|
|
||||||
<label for="font03">字体文件名 03:</label>
|
|
||||||
<input type="text" id="font03" name="font03"><br>
|
|
||||||
<label for="font04">字体文件名 04:</label>
|
|
||||||
<input type="text" id="font04" name="font04"><br>
|
|
||||||
<label for="font05">字体文件名 05:</label>
|
|
||||||
<input type="text" id="font05" name="font05"><br>
|
|
||||||
<label for="font06">字体文件名 06:</label>
|
|
||||||
<input type="text" id="font06" name="font06"><br>
|
|
||||||
<input type="submit" value="保存">
|
|
||||||
</form>
|
|
||||||
</body>
|
|
||||||
</html>
|
|
||||||
@@ -121,25 +121,14 @@ is_raspberry_pi() {
|
|||||||
fi
|
fi
|
||||||
}
|
}
|
||||||
|
|
||||||
# 定义链接变量
|
# 使用中国镜像源
|
||||||
DEBIAN_MIRROR="http://deb.debian.org/debian/"
|
DEBIAN_MIRROR="https://mirrors.tuna.tsinghua.edu.cn/debian"
|
||||||
DEBIAN_SECURITY_MIRROR="http://security.debian.org/"
|
|
||||||
PISUGAR_WIFI_CONF_URL="https://cdn.pisugar.com/PiSugar-wificonfig/script/install.sh"
|
|
||||||
PISUGAR_POWER_MANAGER_URL="https://cdn.pisugar.com/release/pisugar-power-manager.sh"
|
|
||||||
PIPY_MIRROR="https://pypi.org/simple"
|
|
||||||
# 修改 Raspberry Pi 特定源链接
|
|
||||||
RASPBERRY_PI_SOURCE_DEBIAN11="https://archive.raspberrypi.org/debian/"
|
|
||||||
RASPBERRY_PI_SOURCE_DEBIAN12="https://archive.raspberrypi.com/debian/"
|
|
||||||
|
|
||||||
# 如果使用中国镜像源,则更新链接变量
|
|
||||||
if [ "$USE_CN_MIRROR" = true ]; then
|
|
||||||
DEBIAN_MIRROR="https://mirrors.tuna.tsinghua.edu.cn/debian/"
|
|
||||||
DEBIAN_SECURITY_MIRROR="https://mirrors.tuna.tsinghua.edu.cn/debian-security"
|
DEBIAN_SECURITY_MIRROR="https://mirrors.tuna.tsinghua.edu.cn/debian-security"
|
||||||
PIPY_MIRROR="https://mirrors.tuna.tsinghua.edu.cn/pypi/web/simple"
|
PIPY_MIRROR="https://mirrors.tuna.tsinghua.edu.cn/pypi/web/simple"
|
||||||
# 使用中国镜像源时,Raspberry Pi 特定源链接保持不变
|
# 使用中国镜像源时,Raspberry Pi 特定源链接保持不变
|
||||||
RASPBERRY_PI_SOURCE_DEBIAN11="https://mirrors.tuna.tsinghua.edu.cn/raspberrypi/"
|
RASPBERRY_PI_SOURCE_DEBIAN11="https://mirrors.tuna.tsinghua.edu.cn/raspberrypi"
|
||||||
RASPBERRY_PI_SOURCE_DEBIAN12="https://mirrors.tuna.tsinghua.edu.cn/raspberrypi/"
|
RASPBERRY_PI_SOURCE_DEBIAN12="https://mirrors.tuna.tsinghua.edu.cn/raspberrypi"
|
||||||
fi
|
|
||||||
|
|
||||||
# 定义仓库链接变量
|
# 定义仓库链接变量
|
||||||
INK_SCREEN_CLOCK_REPO_URL="https://github.com/kxgx/2.13-Ink-screen-clock"
|
INK_SCREEN_CLOCK_REPO_URL="https://github.com/kxgx/2.13-Ink-screen-clock"
|
||||||
@@ -155,6 +144,7 @@ update_sources_list() {
|
|||||||
local raspberry_pi_source_in_use=$(grep -oP 'deb\s+\K.+' /etc/apt/sources.list.d/raspi.list | head -1)
|
local raspberry_pi_source_in_use=$(grep -oP 'deb\s+\K.+' /etc/apt/sources.list.d/raspi.list | head -1)
|
||||||
|
|
||||||
# 检查并替换 Debian 源
|
# 检查并替换 Debian 源
|
||||||
|
if [ "$USE_CN_MIRROR" = true ]; then
|
||||||
if [ "$debian_mirror_in_use" != "$DEBIAN_MIRROR" ]; then
|
if [ "$debian_mirror_in_use" != "$DEBIAN_MIRROR" ]; then
|
||||||
sudo cp /etc/apt/sources.list /etc/apt/sources.list.bak
|
sudo cp /etc/apt/sources.list /etc/apt/sources.list.bak
|
||||||
{
|
{
|
||||||
@@ -196,12 +186,13 @@ update_sources_list() {
|
|||||||
else
|
else
|
||||||
echo "Raspberry Pi 源链接已更新,跳过替换" >&2
|
echo "Raspberry Pi 源链接已更新,跳过替换" >&2
|
||||||
fi
|
fi
|
||||||
|
fi
|
||||||
}
|
}
|
||||||
|
|
||||||
# 安装包函数
|
# 安装包函数
|
||||||
install_packages() {
|
install_packages() {
|
||||||
echo "正在更新源列表"
|
echo "正在更新源列表"
|
||||||
if ! sudo apt-get -q -y update; then
|
if ! sudo apt-get -q update; then
|
||||||
echo "更新源列表失败" >&2
|
echo "更新源列表失败" >&2
|
||||||
exit 1
|
exit 1
|
||||||
fi
|
fi
|
||||||
@@ -317,6 +308,7 @@ if [ -f /etc/debian_version ]; then
|
|||||||
install_packages
|
install_packages
|
||||||
install_pip_packages
|
install_pip_packages
|
||||||
setup_service
|
setup_service
|
||||||
|
#install_webui
|
||||||
install_pisugar-wifi-conf
|
install_pisugar-wifi-conf
|
||||||
install_pisugar-power-manager
|
install_pisugar-power-manager
|
||||||
;;
|
;;
|
||||||
@@ -326,6 +318,7 @@ if [ -f /etc/debian_version ]; then
|
|||||||
install_packages
|
install_packages
|
||||||
install_pip_packages
|
install_pip_packages
|
||||||
setup_service
|
setup_service
|
||||||
|
#install_webui
|
||||||
install_pisugar-wifi-conf
|
install_pisugar-wifi-conf
|
||||||
install_pisugar-power-manager
|
install_pisugar-power-manager
|
||||||
;;
|
;;
|
||||||
|
|||||||
19
bin/main.py
19
bin/main.py
@@ -37,7 +37,7 @@ def get_date():#返回当前年月日及星期几
|
|||||||
today=LunarDate.today()
|
today=LunarDate.today()
|
||||||
week_day_dict = {0: '星期一',1: '星期二',2: '星期三',3: '星期四',4: '星期五',5: '星期六',6: '星期日',}
|
week_day_dict = {0: '星期一',1: '星期二',2: '星期三',3: '星期四',4: '星期五',5: '星期六',6: '星期日',}
|
||||||
day = date.weekday()
|
day = date.weekday()
|
||||||
return time.strftime('%Y年%m月%d日')+' '+week_day_dict[day]+' '+today.strftime('农 %M月%D')
|
return time.strftime('%Y年%m月%d日')+''+week_day_dict[day]+''+today.strftime('农历%M月%D')
|
||||||
def get_time():#返回当前时间,不到秒,大写
|
def get_time():#返回当前时间,不到秒,大写
|
||||||
return time.strftime('%H:%M')
|
return time.strftime('%H:%M')
|
||||||
def Get_address():#获取当前的IP地址
|
def Get_address():#获取当前的IP地址
|
||||||
@@ -46,13 +46,18 @@ def Get_ipv4_address(): # 获取当前的IP地址
|
|||||||
try:
|
try:
|
||||||
# 执行命令获取IP地址,并处理输出以仅返回IPv4地址
|
# 执行命令获取IP地址,并处理输出以仅返回IPv4地址
|
||||||
ip_output = subprocess.check_output("hostname -I | grep -oE '[0-9]{1,3}(\.[0-9]{1,3}){3}'", shell=True).decode('utf-8').strip()
|
ip_output = subprocess.check_output("hostname -I | grep -oE '[0-9]{1,3}(\.[0-9]{1,3}){3}'", shell=True).decode('utf-8').strip()
|
||||||
# 检查是否为IPv4地址
|
# 分割输出以获取单个IP地址列表
|
||||||
if ip_output and not ip_output.startswith("127."):
|
ip_list = ip_output.split()
|
||||||
return ip_output
|
# 过滤掉以172开头的IP地址
|
||||||
|
filtered_ips = [ip for ip in ip_list if not ip.startswith("172.")]
|
||||||
|
# 如果有有效的IP地址,返回第一个,否则返回获取失败
|
||||||
|
if filtered_ips:
|
||||||
|
return filtered_ips[0]
|
||||||
else:
|
else:
|
||||||
return "IPv4获取失败"
|
return "地址获取失败"
|
||||||
except subprocess.CalledProcessError as e:
|
except subprocess.CalledProcessError as e:
|
||||||
return f"获取失败"
|
logging.error("获取IPv4地址失败: %s", e)
|
||||||
|
return "获取失败"
|
||||||
def CPU_temperature():#CPU温度获取
|
def CPU_temperature():#CPU温度获取
|
||||||
temperatura = os.popen('vcgencmd measure_temp').readline()
|
temperatura = os.popen('vcgencmd measure_temp').readline()
|
||||||
temperatura = temperatura.replace('temp=','').strip()
|
temperatura = temperatura.replace('temp=','').strip()
|
||||||
@@ -213,7 +218,7 @@ def Partial_refresh():#局刷函数
|
|||||||
draw.text((129,108),power_battery(),font = font04,fill =255) #显示当前电量百分比
|
draw.text((129,108),power_battery(),font = font04,fill =255) #显示当前电量百分比
|
||||||
power_str=power_str1
|
power_str=power_str1
|
||||||
Local_strong_brush() #局部强刷
|
Local_strong_brush() #局部强刷
|
||||||
logging.info("电源电量局部刷新")
|
# logging.info("电源电量局部刷新")
|
||||||
try:
|
try:
|
||||||
##################屏幕初始化#########################
|
##################屏幕初始化#########################
|
||||||
epd = epd2in13_V4.EPD() #初始化
|
epd = epd2in13_V4.EPD() #初始化
|
||||||
|
|||||||
@@ -4,12 +4,11 @@ After=network.target
|
|||||||
|
|
||||||
[Service]
|
[Service]
|
||||||
Type=forking
|
Type=forking
|
||||||
# 等待网络完全启动
|
|
||||||
ExecStartPre=/bin/bash -c 'until ping -c1 baidu.com &>/dev/null; do sleep 1; done'
|
|
||||||
ExecStart=/root/2.13-Ink-screen-clock/bin/start.sh
|
ExecStart=/root/2.13-Ink-screen-clock/bin/start.sh
|
||||||
ExecStop=/usr/bin/screen -S main_screen -X quit
|
ExecStop=/bin/bash -c "pkill -f /root/2.13-Ink-screen-clock/bin/main.py; pkill -f /root/2.13-Ink-screen-clock/bin/weather.py"
|
||||||
Restart=on-failure
|
WorkingDirectory=/root/2.13-Ink-screen-clock/bin
|
||||||
|
Restart=always
|
||||||
User=root
|
User=root
|
||||||
|
|
||||||
[Install]
|
[Install]
|
||||||
WantedBy=multi-user.target
|
WantedBy=multi-user.target
|
||||||
@@ -1,10 +1,8 @@
|
|||||||
#!/bin/bash
|
#!/bin/bash
|
||||||
f_name=main.py
|
f_name=main.py
|
||||||
f1_name=weather.py
|
f1_name=weather.py
|
||||||
f2_name=app.py
|
|
||||||
dir=$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )
|
dir=$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )
|
||||||
logdir="${dir%/*}/log"
|
logdir="${dir%/*}/log"
|
||||||
appdir="${dir%/*}/app"
|
|
||||||
# 确保日志目录存在
|
# 确保日志目录存在
|
||||||
mkdir -p "${logdir}"
|
mkdir -p "${logdir}"
|
||||||
pid=`ps -ef |grep $dir/$f_name | grep -v grep |awk '{print $2}'`
|
pid=`ps -ef |grep $dir/$f_name | grep -v grep |awk '{print $2}'`
|
||||||
@@ -14,4 +12,3 @@ do
|
|||||||
done
|
done
|
||||||
nohup /usr/bin/python3 -u $dir/$f_name > $logdir/info.log 2>&1 &
|
nohup /usr/bin/python3 -u $dir/$f_name > $logdir/info.log 2>&1 &
|
||||||
nohup /usr/bin/python3 -u $dir/$f1_name > $logdir/info-wenter.log 2>&1 &
|
nohup /usr/bin/python3 -u $dir/$f1_name > $logdir/info-wenter.log 2>&1 &
|
||||||
nohup /usr/bin/python3 -u $appdir/$f2_name > $logdir/info-app.log 2>&1 &
|
|
||||||
|
|||||||
@@ -41,7 +41,7 @@ def get_area_id(city_name):
|
|||||||
logging.error("从city.js接收到的数据为空")
|
logging.error("从city.js接收到的数据为空")
|
||||||
except (requests.RequestException, json.JSONDecodeError) as e:
|
except (requests.RequestException, json.JSONDecodeError) as e:
|
||||||
logging.error("检索或解析城市数据时发生错误: %s", e)
|
logging.error("检索或解析城市数据时发生错误: %s", e)
|
||||||
time.sleep(5) # 重试前等待5秒
|
time.sleep(180) # 重试前等待
|
||||||
|
|
||||||
def get_current_city():
|
def get_current_city():
|
||||||
"""获取当前城市名称,无限重试直到成功"""
|
"""获取当前城市名称,无限重试直到成功"""
|
||||||
@@ -57,13 +57,18 @@ def get_current_city():
|
|||||||
logging.error("获取当前城市失败: %s", data['message'])
|
logging.error("获取当前城市失败: %s", data['message'])
|
||||||
except (requests.RequestException, json.JSONDecodeError) as e:
|
except (requests.RequestException, json.JSONDecodeError) as e:
|
||||||
logging.error("检索或解析当前城市时出现错误: %s", e)
|
logging.error("检索或解析当前城市时出现错误: %s", e)
|
||||||
time.sleep(5) # 重试前等待5秒
|
time.sleep(180) # 重试前等待
|
||||||
# 注意:无限重试可能会在特定情况下导致程序无法终止,请确保在实际使用中考虑适当的退出条件或限制重试次数。
|
# 注意:无限重试可能会在特定情况下导致程序无法终止,请确保在实际使用中考虑适当的退出条件或限制重试次数。
|
||||||
|
|
||||||
|
def schedule_getWeath():
|
||||||
|
"""调度 getWeath 函数每3分钟执行一次"""
|
||||||
|
getWeath()
|
||||||
|
Timer(180, schedule_getWeath).start() # 重新设置定时器
|
||||||
|
|
||||||
def getWeath(city='101060101'):
|
def getWeath(city='101060101'):
|
||||||
headers = {
|
headers = {
|
||||||
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/131.0.0.0 Safari/537.36',
|
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/131.0.0.0 Safari/537.36',
|
||||||
'Referer': 'http://www.weather.com.cn/'
|
'Referer': 'https://www.weather.com.cn/'
|
||||||
}
|
}
|
||||||
current_city = get_current_city()
|
current_city = get_current_city()
|
||||||
if current_city:
|
if current_city:
|
||||||
@@ -71,14 +76,13 @@ def getWeath(city='101060101'):
|
|||||||
if area_id:
|
if area_id:
|
||||||
city = area_id
|
city = area_id
|
||||||
try:
|
try:
|
||||||
response = requests.get('http://d1.weather.com.cn/sk_2d/'+city+'.html',headers=headers)
|
response = requests.get('https://d1.weather.com.cn/sk_2d/'+city+'.html',headers=headers)
|
||||||
response.raise_for_status() # 检查请求是否成功
|
response.raise_for_status() # 检查请求是否成功
|
||||||
response.encoding = 'utf-8'
|
response.encoding = 'utf-8'
|
||||||
Weath = response.text[11:]
|
Weath = response.text[11:]
|
||||||
fileHandle = open('/root/2.13-Ink-screen-clock/bin/weather.json', 'w')
|
fileHandle = open('/root/2.13-Ink-screen-clock/bin/weather.json', 'w')
|
||||||
fileHandle.write(str(Weath))
|
fileHandle.write(str(Weath))
|
||||||
fileHandle.close()
|
fileHandle.close()
|
||||||
Timer(180, getWeath).start() # 定时器函数,间隔三分钟下载文件至本地
|
|
||||||
print("天气文件更新")
|
print("天气文件更新")
|
||||||
except requests.RequestException as e:
|
except requests.RequestException as e:
|
||||||
logging.error("获取天气数据时出现网络错误: %s", e)
|
logging.error("获取天气数据时出现网络错误: %s", e)
|
||||||
@@ -86,8 +90,8 @@ def getWeath(city='101060101'):
|
|||||||
logging.error("发生了意外错误: %s", e)
|
logging.error("发生了意外错误: %s", e)
|
||||||
|
|
||||||
try:
|
try:
|
||||||
getWeath() # 天气获取函数开始运行
|
schedule_getWeath() # 开始调度天气获取函数
|
||||||
except IOError as e:
|
except OSError as e:
|
||||||
logging.info(e)
|
logging.info(e)
|
||||||
except KeyboardInterrupt:
|
except KeyboardInterrupt:
|
||||||
logging.info("检测到键盘中断,正在退出")
|
logging.info("检测到键盘中断,正在退出")
|
||||||
@@ -96,4 +100,4 @@ except Exception as e:
|
|||||||
exit()
|
exit()
|
||||||
|
|
||||||
# 脚本正常结束后的清理操作
|
# 脚本正常结束后的清理操作
|
||||||
exit()
|
exit()
|
||||||
|
|||||||
Reference in New Issue
Block a user