mirror of
https://github.com/kxgx/2.13-Ink-screen-clock.git
synced 2026-03-15 07:03:16 +08:00
Compare commits
57 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 | ||
|
|
863e9b2f5d | ||
|
|
d523a31006 | ||
|
|
9d4895fca4 | ||
|
|
e7f71c699e | ||
|
|
419f44be7e | ||
|
|
6c90e54728 | ||
|
|
e70b05a529 | ||
|
|
d8c12f7ad3 | ||
|
|
8eb21afb19 | ||
|
|
7bc892d06b | ||
|
|
ca67499854 | ||
|
|
0b5623d5f6 | ||
|
|
7a71514224 | ||
|
|
fac4c591fb | ||
|
|
95e6bb12be | ||
|
|
57ea23a6fa | ||
|
|
5262d729fb | ||
|
|
428d056e58 | ||
|
|
066d9676f3 | ||
|
|
54bb2ccf21 | ||
|
|
47e6ae681a | ||
|
|
7e15bf4172 | ||
|
|
7eeb7ceb12 | ||
|
|
6a498cc04f | ||
|
|
7e302d0843 | ||
|
|
04930156b6 | ||
|
|
5543bc6710 |
@@ -8,10 +8,10 @@
|
||||
|
||||
## API说明:
|
||||
|
||||
#### 一言API
|
||||
此网站可以获取指定长度指定类型的一个句子,可以限定返回长度及返回类型
|
||||
~~#### 一言API~~
|
||||
~~此网站可以获取指定长度指定类型的一个句子,可以限定返回长度及返回类型~~
|
||||
|
||||
https://international.v1.hitokoto.cn/?c=a&encode=text&max_length=20
|
||||
~~https://international.v1.hitokoto.cn/?c=a&encode=text&max_length=20~~
|
||||
|
||||
#### 天气API
|
||||
获取的天气是通过网页的接口,获取的内容保存在当前目录下的json文件接口的地址是在这里抓取的
|
||||
|
||||
14
bin/clean.py
14
bin/clean.py
@@ -16,39 +16,39 @@ if os.path.exists(libdir):
|
||||
from waveshare_epd import epd2in13_V4 #引入墨水屏驱动文件
|
||||
|
||||
# 配置日志
|
||||
logging.basicConfig(level=logging.DEBUG)
|
||||
logging.basicConfig(level=logging.INFO, format='%(asctime)s - %(message)s')
|
||||
|
||||
def clear_screen():
|
||||
try:
|
||||
logging.info("Starting e-Paper clear screen script")
|
||||
logging.info("启动电子纸清屏脚本")
|
||||
|
||||
# 初始化并创建一个 epd2in13_V4 对象
|
||||
epd = epd2in13_V4.EPD()
|
||||
|
||||
# 初始化屏幕
|
||||
logging.info("Initializing e-Paper display")
|
||||
logging.info("初始化电子纸显示屏")
|
||||
epd.init()
|
||||
|
||||
# 清除屏幕
|
||||
logging.info("Clearing e-Paper display")
|
||||
logging.info("清除电子纸显示屏内容")
|
||||
epd.Clear(0xFF)
|
||||
|
||||
# 等待一段时间,确保清除操作完成
|
||||
#time.sleep(1)
|
||||
|
||||
# 将屏幕置于睡眠模式
|
||||
logging.info("Putting e-Paper display to sleep")
|
||||
logging.info("将电子纸显示屏置入睡眠状态")
|
||||
epd.sleep()
|
||||
epd2in13_V4.epdconfig.module_exit(cleanup=True)
|
||||
exit()
|
||||
|
||||
logging.info("e-Paper display cleared successfully")
|
||||
logging.info("电子纸显示屏清屏成功")
|
||||
|
||||
except IOError as e:
|
||||
logging.info(e)
|
||||
|
||||
except KeyboardInterrupt:
|
||||
logging.info("Ctrl+C pressed, exiting")
|
||||
logging.info("检测到键盘中断,正在退出")
|
||||
epd2in13_V4.epdconfig.module_exit(cleanup=True)
|
||||
exit()
|
||||
|
||||
|
||||
@@ -121,25 +121,14 @@ is_raspberry_pi() {
|
||||
fi
|
||||
}
|
||||
|
||||
# 定义链接变量
|
||||
DEBIAN_MIRROR="http://deb.debian.org/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_MIRROR="https://mirrors.tuna.tsinghua.edu.cn/debian"
|
||||
DEBIAN_SECURITY_MIRROR="https://mirrors.tuna.tsinghua.edu.cn/debian-security"
|
||||
PIPY_MIRROR="https://mirrors.tuna.tsinghua.edu.cn/pypi/web/simple"
|
||||
# 使用中国镜像源时,Raspberry Pi 特定源链接保持不变
|
||||
RASPBERRY_PI_SOURCE_DEBIAN11="https://mirrors.tuna.tsinghua.edu.cn/raspberrypi/"
|
||||
RASPBERRY_PI_SOURCE_DEBIAN12="https://mirrors.tuna.tsinghua.edu.cn/raspberrypi/"
|
||||
fi
|
||||
RASPBERRY_PI_SOURCE_DEBIAN11="https://mirrors.tuna.tsinghua.edu.cn/raspberrypi"
|
||||
RASPBERRY_PI_SOURCE_DEBIAN12="https://mirrors.tuna.tsinghua.edu.cn/raspberrypi"
|
||||
|
||||
|
||||
# 定义仓库链接变量
|
||||
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)
|
||||
|
||||
# 检查并替换 Debian 源
|
||||
if [ "$USE_CN_MIRROR" = true ]; then
|
||||
if [ "$debian_mirror_in_use" != "$DEBIAN_MIRROR" ]; then
|
||||
sudo cp /etc/apt/sources.list /etc/apt/sources.list.bak
|
||||
{
|
||||
@@ -196,12 +186,13 @@ update_sources_list() {
|
||||
else
|
||||
echo "Raspberry Pi 源链接已更新,跳过替换" >&2
|
||||
fi
|
||||
fi
|
||||
}
|
||||
|
||||
# 安装包函数
|
||||
install_packages() {
|
||||
echo "正在更新源列表"
|
||||
if ! sudo apt-get -q -y update; then
|
||||
if ! sudo apt-get -q update; then
|
||||
echo "更新源列表失败" >&2
|
||||
exit 1
|
||||
fi
|
||||
@@ -215,7 +206,7 @@ install_packages() {
|
||||
# 安装pip包函数
|
||||
install_pip_packages() {
|
||||
echo "正在安装pip软件包"
|
||||
if ! sudo pip3 install -i "$PIPY_MIRROR" spidev borax pillow requests; then
|
||||
if ! sudo pip3 install -i "$PIPY_MIRROR" spidev borax pillow requests Flask; then
|
||||
echo "pip软件包安装失败,如果是最新版系统或是非lite系统" >&2
|
||||
echo "请手动运行sudo pip3 install -i "$PIPY_MIRROR" spidev borax pillow requests --break-system-packages" >&2
|
||||
exit 1
|
||||
@@ -317,6 +308,7 @@ if [ -f /etc/debian_version ]; then
|
||||
install_packages
|
||||
install_pip_packages
|
||||
setup_service
|
||||
#install_webui
|
||||
install_pisugar-wifi-conf
|
||||
install_pisugar-power-manager
|
||||
;;
|
||||
@@ -326,6 +318,7 @@ if [ -f /etc/debian_version ]; then
|
||||
install_packages
|
||||
install_pip_packages
|
||||
setup_service
|
||||
#install_webui
|
||||
install_pisugar-wifi-conf
|
||||
install_pisugar-power-manager
|
||||
;;
|
||||
|
||||
39
bin/main.py
39
bin/main.py
@@ -12,7 +12,7 @@ import socket
|
||||
|
||||
white = 255 #颜色
|
||||
black = 0
|
||||
logging.basicConfig(level=logging.INFO)
|
||||
logging.basicConfig(level=logging.INFO, format='%(asctime)s - %(message)s')
|
||||
################################引入配置文件开始################################################
|
||||
picdir = os.path.join(os.path.dirname(os.path.dirname(os.path.realpath(__file__))), 'pic')
|
||||
libdir = os.path.join(os.path.dirname(os.path.dirname(os.path.realpath(__file__))), 'lib')
|
||||
@@ -37,7 +37,7 @@ def get_date():#返回当前年月日及星期几
|
||||
today=LunarDate.today()
|
||||
week_day_dict = {0: '星期一',1: '星期二',2: '星期三',3: '星期四',4: '星期五',5: '星期六',6: '星期日',}
|
||||
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():#返回当前时间,不到秒,大写
|
||||
return time.strftime('%H:%M')
|
||||
def Get_address():#获取当前的IP地址
|
||||
@@ -46,13 +46,18 @@ def Get_ipv4_address(): # 获取当前的IP地址
|
||||
try:
|
||||
# 执行命令获取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()
|
||||
# 检查是否为IPv4地址
|
||||
if ip_output and not ip_output.startswith("127."):
|
||||
return ip_output
|
||||
# 分割输出以获取单个IP地址列表
|
||||
ip_list = ip_output.split()
|
||||
# 过滤掉以172开头的IP地址
|
||||
filtered_ips = [ip for ip in ip_list if not ip.startswith("172.")]
|
||||
# 如果有有效的IP地址,返回第一个,否则返回获取失败
|
||||
if filtered_ips:
|
||||
return filtered_ips[0]
|
||||
else:
|
||||
return "IPv4获取失败"
|
||||
return "地址获取失败"
|
||||
except subprocess.CalledProcessError as e:
|
||||
return f"获取失败"
|
||||
logging.error("获取IPv4地址失败: %s", e)
|
||||
return "获取失败"
|
||||
def CPU_temperature():#CPU温度获取
|
||||
temperatura = os.popen('vcgencmd measure_temp').readline()
|
||||
temperatura = temperatura.replace('temp=','').strip()
|
||||
@@ -86,7 +91,7 @@ def Bottom_edge(): #在图片中添加底边内容
|
||||
local_addr= Get_ipv4_address() #获取当前IP地址
|
||||
draw.text((10,107),"IP:"+local_addr,font = font05,fill =255)#显示当前IP地址
|
||||
def Weather(): #在图片中添加天气内容
|
||||
Weather_json = open('weather.json','r')
|
||||
Weather_json = open('/root/2.13-Ink-screen-clock/bin/weather.json','r')
|
||||
Weather_data = Weather_json.read()
|
||||
Weather_json.close()
|
||||
Weather_text=json.loads(Weather_data)
|
||||
@@ -115,7 +120,7 @@ def Weather(): #在图片中添加天气内容
|
||||
draw.text((211,107),weather_update,font = font05,fill =255) #显示天气更新时间
|
||||
|
||||
def Basic_refresh(): #全刷函数
|
||||
logging.info("Refresh and prepare the basic content before starting the canvas")#开始画布前刷新准备基础内容
|
||||
logging.info("在启动画布之前,刷新并准备基本内容")#开始画布前刷新准备基础内容
|
||||
global get_date_var
|
||||
get_date_var=get_date() #记录开始数据
|
||||
draw.text((2,2),get_date_var,font = font02,fill =0)#将日期及星期几显示到屏幕
|
||||
@@ -130,7 +135,7 @@ def Partial_full_brush(): #局部定时全刷函数
|
||||
logging.debug("局部定时全局刷新")
|
||||
epd.init()
|
||||
def Partial_refresh():#局刷函数
|
||||
logging.info("Partial content update, this update is recommended to be synchronized with the minute to save the life of the ink screen")#局部内容更新,此更新建议与分钟同步,以节省墨水屏寿命
|
||||
logging.info("部分内容更新,此更新建议与分钟同步,以节省墨水屏的使用寿命")#局部内容更新,此更新建议与分钟同步,以节省墨水屏寿命
|
||||
epd.displayPartBaseImage(epd.getbuffer(info_image.rotate(180)))
|
||||
epd.init()
|
||||
while (True):
|
||||
@@ -157,7 +162,7 @@ def Partial_refresh():#局刷函数
|
||||
local_addr=local_addr1
|
||||
Local_strong_brush() #局部强刷
|
||||
'''天气局部更新函数'''
|
||||
Weather_json = open('weather.json','r')
|
||||
Weather_json = open('/root/2.13-Ink-screen-clock/bin/weather.json','r')
|
||||
Weather_data = Weather_json.read()
|
||||
Weather_json.close()
|
||||
Weather_text=json.loads(Weather_data)
|
||||
@@ -213,7 +218,7 @@ def Partial_refresh():#局刷函数
|
||||
draw.text((129,108),power_battery(),font = font04,fill =255) #显示当前电量百分比
|
||||
power_str=power_str1
|
||||
Local_strong_brush() #局部强刷
|
||||
logging.info("电源电量局部刷新")
|
||||
# logging.info("电源电量局部刷新")
|
||||
try:
|
||||
##################屏幕初始化#########################
|
||||
epd = epd2in13_V4.EPD() #初始化
|
||||
@@ -221,7 +226,7 @@ try:
|
||||
#epd.Clear(0xFF) #清除屏幕内容
|
||||
##################屏幕初始化#########################
|
||||
logging.info("Width = %s, Height = %s", format(epd.width), format(epd.height)) #打印屏幕高度及宽度
|
||||
logging.info("Initialize and clear the display")#屏幕开始准备相关展示
|
||||
logging.info("初始化并清空显示屏")#屏幕开始准备相关展示
|
||||
info_image = Image.new('1', (epd.height, epd.width), 255) #画布创建准备
|
||||
draw = ImageDraw.Draw(info_image)
|
||||
Basic_refresh() #全局刷新
|
||||
@@ -229,10 +234,10 @@ try:
|
||||
epd.init()
|
||||
epd.Clear(0xFF)
|
||||
epd.sleep()
|
||||
except IOError as e:
|
||||
except OSError as e:
|
||||
logging.info(e)
|
||||
except KeyboardInterrupt:
|
||||
logging.info("Keyboard interrupt detected, exiting gracefully.")
|
||||
logging.info("检测到键盘中断,正在清理并退出")
|
||||
epd.init()
|
||||
epd.Clear(0xFF) # 清除屏幕内容
|
||||
epd.sleep() # 使屏幕进入休眠状态
|
||||
@@ -240,7 +245,7 @@ except KeyboardInterrupt:
|
||||
exit()
|
||||
|
||||
except Exception as e:
|
||||
logging.error("An unexpected error occurred: %s", e)
|
||||
logging.error("发生了意外的错误: %s", e)
|
||||
epd.init()
|
||||
epd.Clear(0xFF) # 清除屏幕内容
|
||||
epd.sleep() # 使屏幕进入休眠状态
|
||||
@@ -252,4 +257,4 @@ epd.init()
|
||||
epd.Clear(0xFF) # 清除屏幕内容
|
||||
epd.sleep() # 使屏幕进入休眠状态
|
||||
epd2in13_V4.epdconfig.module_exit() # 清理资源
|
||||
exit()
|
||||
exit()
|
||||
@@ -4,12 +4,11 @@ After=network.target
|
||||
|
||||
[Service]
|
||||
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
|
||||
ExecStop=/usr/bin/screen -S main_screen -X quit
|
||||
Restart=on-failure
|
||||
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"
|
||||
WorkingDirectory=/root/2.13-Ink-screen-clock/bin
|
||||
Restart=always
|
||||
User=root
|
||||
|
||||
[Install]
|
||||
WantedBy=multi-user.target
|
||||
WantedBy=multi-user.target
|
||||
111
bin/weather.py
111
bin/weather.py
@@ -9,7 +9,7 @@ import requests
|
||||
|
||||
white = 255 # 颜色
|
||||
black = 0
|
||||
logging.basicConfig(level=logging.INFO) # 设置日志级别
|
||||
logging.basicConfig(level=logging.INFO, format='%(asctime)s - %(message)s') # 设置日志级别
|
||||
|
||||
picdir = os.path.join(os.path.dirname(os.path.dirname(os.path.realpath(__file__))), 'pic')
|
||||
libdir = os.path.join(os.path.dirname(os.path.dirname(os.path.realpath(__file__))), 'lib')
|
||||
@@ -17,54 +17,58 @@ if os.path.exists(libdir):
|
||||
sys.path.append(libdir) # 将引入文件添加到环境变量
|
||||
|
||||
def get_area_id(city_name):
|
||||
"""从city.js中检索AREAID"""
|
||||
"""从city.js中检索AREAID,无限重试直到成功"""
|
||||
url = "https://j.i8tq.com/weather2020/search/city.js"
|
||||
try:
|
||||
response = requests.get(url)
|
||||
response.raise_for_status() # 检查请求是否成功
|
||||
# 直接打印原始数据
|
||||
logging.debug("Raw data from city.js: %s", response.text)
|
||||
# 预处理返回的数据,去除非JSON部分
|
||||
city_data_text = response.text.strip()
|
||||
city_data_text = city_data_text.split('var city_data = ')[-1].rstrip(';')
|
||||
if city_data_text:
|
||||
city_data = json.loads(city_data_text)
|
||||
# 遍历数据结构,查找城市名称
|
||||
for province, cities in city_data.items():
|
||||
for city, districts in cities.items():
|
||||
for district, info in districts.items():
|
||||
if info['NAMECN'] == city_name:
|
||||
return info['AREAID']
|
||||
logging.error("City name '%s' not found in city data", city_name)
|
||||
else:
|
||||
logging.error("Empty data received from city.js")
|
||||
except requests.RequestException as e:
|
||||
logging.error("Network error when retrieving city data: %s", e)
|
||||
except json.JSONDecodeError as e:
|
||||
logging.error("JSON decode error when parsing city data: %s", e)
|
||||
return None
|
||||
|
||||
def get_current_city():
|
||||
"""获取当前城市名称"""
|
||||
url = "http://ip-api.com/json/?lang=zh-CN"
|
||||
try:
|
||||
response = requests.get(url)
|
||||
response.raise_for_status() # 检查请求是否成功
|
||||
data = response.json()
|
||||
if data['status'] == 'success':
|
||||
return data['city']
|
||||
else:
|
||||
logging.error("Failed to get current city: %s", data['message'])
|
||||
except requests.RequestException as e:
|
||||
logging.error("Network error when retrieving current city: %s", e)
|
||||
except json.JSONDecodeError as e:
|
||||
logging.error("JSON decode error when parsing current city data: %s", e)
|
||||
return None
|
||||
while True:
|
||||
try:
|
||||
response = requests.get(url)
|
||||
response.raise_for_status() # 检查请求是否成功
|
||||
# 直接打印原始数据
|
||||
logging.debug("Raw data from city.js: %s", response.text)
|
||||
# 预处理返回的数据,去除非JSON部分
|
||||
city_data_text = response.text.strip()
|
||||
city_data_text = city_data_text.split('var city_data = ')[-1].rstrip(';')
|
||||
if city_data_text:
|
||||
city_data = json.loads(city_data_text)
|
||||
# 遍历数据结构,查找城市名称
|
||||
for province, cities in city_data.items():
|
||||
for city, districts in cities.items():
|
||||
for district, info in districts.items():
|
||||
if info['NAMECN'] == city_name:
|
||||
return info['AREAID']
|
||||
logging.error("城市名称 '%s' 在城市数据中未找到,将使用默认城市信息", city_name)
|
||||
else:
|
||||
logging.error("从city.js接收到的数据为空")
|
||||
except (requests.RequestException, json.JSONDecodeError) as e:
|
||||
logging.error("检索或解析城市数据时发生错误: %s", e)
|
||||
time.sleep(180) # 重试前等待
|
||||
|
||||
def getWeath(city='101060111'):
|
||||
def get_current_city():
|
||||
"""获取当前城市名称,无限重试直到成功"""
|
||||
url = "http://ip-api.com/json/?lang=zh-CN"
|
||||
while True:
|
||||
try:
|
||||
response = requests.get(url)
|
||||
response.raise_for_status() # 检查请求是否成功
|
||||
data = response.json()
|
||||
if data['status'] == 'success':
|
||||
return data['city']
|
||||
else:
|
||||
logging.error("获取当前城市失败: %s", data['message'])
|
||||
except (requests.RequestException, json.JSONDecodeError) as e:
|
||||
logging.error("检索或解析当前城市时出现错误: %s", e)
|
||||
time.sleep(180) # 重试前等待
|
||||
# 注意:无限重试可能会在特定情况下导致程序无法终止,请确保在实际使用中考虑适当的退出条件或限制重试次数。
|
||||
|
||||
def schedule_getWeath():
|
||||
"""调度 getWeath 函数每3分钟执行一次"""
|
||||
getWeath()
|
||||
Timer(180, schedule_getWeath).start() # 重新设置定时器
|
||||
|
||||
def getWeath(city='101060101'):
|
||||
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',
|
||||
'Referer': 'http://www.weather.com.cn/'
|
||||
'Referer': 'https://www.weather.com.cn/'
|
||||
}
|
||||
current_city = get_current_city()
|
||||
if current_city:
|
||||
@@ -72,28 +76,27 @@ def getWeath(city='101060111'):
|
||||
if area_id:
|
||||
city = area_id
|
||||
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.encoding = 'utf-8'
|
||||
Weath = response.text[11:]
|
||||
fileHandle = open('weather.json', 'w')
|
||||
fileHandle = open('/root/2.13-Ink-screen-clock/bin/weather.json', 'w')
|
||||
fileHandle.write(str(Weath))
|
||||
fileHandle.close()
|
||||
Timer(180, getWeath).start() # 定时器函数,间隔三分钟下载文件至本地
|
||||
print("天气文件更新")
|
||||
except requests.RequestException as e:
|
||||
logging.error("Network error when retrieving weather data: %s", e)
|
||||
logging.error("获取天气数据时出现网络错误: %s", e)
|
||||
except Exception as e:
|
||||
logging.error("An unexpected error occurred: %s", e)
|
||||
logging.error("发生了意外错误: %s", e)
|
||||
|
||||
try:
|
||||
getWeath() # 天气获取函数开始运行
|
||||
except IOError as e:
|
||||
schedule_getWeath() # 开始调度天气获取函数
|
||||
except OSError as e:
|
||||
logging.info(e)
|
||||
except KeyboardInterrupt:
|
||||
logging.info("Keyboard interrupt detected, exiting gracefully.")
|
||||
logging.info("检测到键盘中断,正在退出")
|
||||
except Exception as e:
|
||||
logging.error("An unexpected error occurred: %s", e)
|
||||
logging.error("发生了意外错误: %s", e)
|
||||
exit()
|
||||
|
||||
# 脚本正常结束后的清理操作
|
||||
|
||||
Reference in New Issue
Block a user