mirror of
https://github.com/kxgx/2.13-Ink-screen-clock.git
synced 2026-03-15 07:03:16 +08:00
Compare commits
2 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
e85188dda8 | ||
|
|
7c73382851 |
@@ -1,6 +1,6 @@
|
||||
# 墨水屏展示当前时间及天气数据
|
||||
|
||||
本项目复刻自[Seek-Huang](https://github.com/Seek-Huang/2.13-Ink-screen-clock)的代码仓库
|
||||
本项目复刻自[Seek-Huang](https://github.com/Seek-Huang)的[代码仓库](https://github.com/Seek-Huang/2.13-Ink-screen-clock)
|
||||
并在此基础上进行改进
|
||||
|
||||
## 本仓库已添加[一键安装部署脚本](https://github.com/kxgx/2.13-Ink-screen-clock#%E4%BD%BF%E7%94%A8%E8%84%9A%E6%9C%AC%E7%9B%B4%E6%8E%A5%E5%AE%89%E8%A3%85%E6%8E%A8%E8%8D%90)
|
||||
|
||||
@@ -13,35 +13,40 @@ logging.basicConfig(
|
||||
datefmt='%Y-%m-%d %H:%M:%S'
|
||||
)
|
||||
|
||||
def get_area_id(city_name):
|
||||
"""从city.js中检索AREAID,无限重试直到成功"""
|
||||
url = "https://j.i8tq.com/weather2020/search/city.js"
|
||||
while True:
|
||||
try:
|
||||
resp = requests.get(url, timeout=10)
|
||||
resp.encoding = 'utf-8' # 显式设置编码
|
||||
city_data = json.loads(resp.text.split('=', 1)[1].rstrip(';'))
|
||||
for province in city_data.values():
|
||||
for city in province.values():
|
||||
for district, info in city.items():
|
||||
if info['NAMECN'] == city_name:
|
||||
return info['AREAID']
|
||||
logging.error("未找到城市: %s", city_name)
|
||||
except Exception as e:
|
||||
logging.error("获取城市ID失败: %s", str(e))
|
||||
time.sleep(180)
|
||||
def get_ip():
|
||||
"""从ip.cn获取当前IP地址"""
|
||||
url = "https://ip.cn/api/index?ip=&type=0"
|
||||
headers = {
|
||||
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36'
|
||||
}
|
||||
try:
|
||||
resp = requests.get(url, headers=headers, timeout=10)
|
||||
resp.raise_for_status()
|
||||
data = resp.json()
|
||||
if data.get('code') == 'Success':
|
||||
return data.get('ip', '')
|
||||
logging.error("获取IP失败: %s", data.get('msg', '未知错误'))
|
||||
except Exception as e:
|
||||
logging.error("获取IP异常: %s", str(e))
|
||||
return None
|
||||
|
||||
def get_current_city():
|
||||
"""获取当前定位城市并去除'市'后缀"""
|
||||
url = "http://ip-api.com/json/?lang=zh-CN"
|
||||
"""通过IP地址获取当前定位城市并去除'市'后缀"""
|
||||
ip = get_ip()
|
||||
if not ip:
|
||||
return None
|
||||
|
||||
url = f"http://ip-api.com/json/{ip}?fields=city&lang=zh-CN"
|
||||
headers = {
|
||||
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36'
|
||||
}
|
||||
while True:
|
||||
try:
|
||||
resp = requests.get(url, timeout=10)
|
||||
resp.encoding = 'utf-8' # 显式设置编码
|
||||
resp = requests.get(url, headers=headers, timeout=10)
|
||||
data = resp.json()
|
||||
if data['status'] == 'success':
|
||||
return data['city'].rstrip('市')
|
||||
logging.error("定位失败: %s", data.get('message'))
|
||||
if data.get('status') == 'success':
|
||||
return data.get('city', '').replace('市', '')
|
||||
logging.error("定位失败: %s", data.get('message', '未知错误'))
|
||||
except Exception as e:
|
||||
logging.error("定位异常: %s", str(e))
|
||||
time.sleep(180)
|
||||
@@ -56,7 +61,7 @@ def schedule_getWeath():
|
||||
def getWeath(default_city='101060101'):
|
||||
"""获取天气数据核心函数"""
|
||||
city_name = get_current_city()
|
||||
area_id = default_city # 设置默认值
|
||||
area_id = default_city
|
||||
|
||||
if city_name:
|
||||
try:
|
||||
@@ -75,13 +80,11 @@ def getWeath(default_city='101060101'):
|
||||
headers=headers,
|
||||
timeout=15
|
||||
)
|
||||
resp.encoding = 'utf-8' # 关键修改:强制设置响应编码
|
||||
resp.encoding = 'utf-8'
|
||||
resp.raise_for_status()
|
||||
|
||||
# 直接处理原始字节数据
|
||||
weather_data = resp.content[11:].decode('utf-8')
|
||||
|
||||
# 使用utf-8编码写入文件
|
||||
with open('/root/2.13-Ink-screen-clock/bin/weather.json', 'w', encoding='utf-8') as f:
|
||||
f.write(weather_data)
|
||||
|
||||
@@ -89,10 +92,31 @@ def getWeath(default_city='101060101'):
|
||||
except Exception as e:
|
||||
logging.error("天气更新失败: %s", str(e))
|
||||
|
||||
def get_area_id(city_name):
|
||||
"""从city.js中检索AREAID,无限重试直到成功"""
|
||||
url = "https://j.i8tq.com/weather2020/search/city.js"
|
||||
headers = {
|
||||
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36'
|
||||
}
|
||||
while True:
|
||||
try:
|
||||
resp = requests.get(url, headers=headers, timeout=10)
|
||||
resp.encoding = 'utf-8'
|
||||
city_data = json.loads(resp.text.split('=', 1)[1].rstrip(';'))
|
||||
for province in city_data.values():
|
||||
for city in province.values():
|
||||
for district, info in city.items():
|
||||
if info['NAMECN'] == city_name:
|
||||
return info['AREAID']
|
||||
logging.error("未找到城市: %s", city_name)
|
||||
except Exception as e:
|
||||
logging.error("获取城市ID失败: %s", str(e))
|
||||
time.sleep(180)
|
||||
|
||||
if __name__ == "__main__":
|
||||
try:
|
||||
schedule_getWeath()
|
||||
while True:
|
||||
time.sleep(1) # 保持主线程存活
|
||||
time.sleep(1)
|
||||
except KeyboardInterrupt:
|
||||
logging.info("程序已终止")
|
||||
logging.info("程序已终止")
|
||||
|
||||
Reference in New Issue
Block a user