mirror of
https://github.com/kxgx/2.13-Ink-screen-clock.git
synced 2026-03-15 07:03:16 +08:00
Update weather.py
This commit is contained in:
@@ -17,49 +17,48 @@ if os.path.exists(libdir):
|
|||||||
sys.path.append(libdir) # 将引入文件添加到环境变量
|
sys.path.append(libdir) # 将引入文件添加到环境变量
|
||||||
|
|
||||||
def get_area_id(city_name):
|
def get_area_id(city_name):
|
||||||
"""从city.js中检索AREAID"""
|
"""从city.js中检索AREAID,无限重试直到成功"""
|
||||||
url = "https://j.i8tq.com/weather2020/search/city.js"
|
url = "https://j.i8tq.com/weather2020/search/city.js"
|
||||||
try:
|
while True:
|
||||||
response = requests.get(url)
|
try:
|
||||||
response.raise_for_status() # 检查请求是否成功
|
response = requests.get(url)
|
||||||
# 直接打印原始数据
|
response.raise_for_status() # 检查请求是否成功
|
||||||
logging.debug("Raw data from city.js: %s", response.text)
|
# 直接打印原始数据
|
||||||
# 预处理返回的数据,去除非JSON部分
|
logging.debug("Raw data from city.js: %s", response.text)
|
||||||
city_data_text = response.text.strip()
|
# 预处理返回的数据,去除非JSON部分
|
||||||
city_data_text = city_data_text.split('var city_data = ')[-1].rstrip(';')
|
city_data_text = response.text.strip()
|
||||||
if city_data_text:
|
city_data_text = city_data_text.split('var city_data = ')[-1].rstrip(';')
|
||||||
city_data = json.loads(city_data_text)
|
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 province, cities in city_data.items():
|
||||||
for district, info in districts.items():
|
for city, districts in cities.items():
|
||||||
if info['NAMECN'] == city_name:
|
for district, info in districts.items():
|
||||||
return info['AREAID']
|
if info['NAMECN'] == city_name:
|
||||||
logging.error("城市名称 '%s' 在城市数据中未找到,将使用默认城市信息", city_name)
|
return info['AREAID']
|
||||||
else:
|
logging.error("城市名称 '%s' 在城市数据中未找到,将使用默认城市信息", city_name)
|
||||||
logging.error("从city.js接收到的数据为空")
|
else:
|
||||||
except requests.RequestException as e:
|
logging.error("从city.js接收到的数据为空")
|
||||||
logging.error("检索城市数据时发生网络错误: %s", e)
|
except (requests.RequestException, json.JSONDecodeError) as e:
|
||||||
except json.JSONDecodeError as e:
|
logging.error("检索或解析城市数据时发生错误: %s", e)
|
||||||
logging.error("解析城市数据时发生JSON解码错误: %s", e)
|
time.sleep(5) # 重试前等待5秒
|
||||||
return None
|
|
||||||
|
|
||||||
def get_current_city():
|
def get_current_city():
|
||||||
"""获取当前城市名称"""
|
"""获取当前城市名称,无限重试直到成功"""
|
||||||
url = "http://ip-api.com/json/?lang=zh-CN"
|
url = "http://ip-api.com/json/?lang=zh-CN"
|
||||||
try:
|
while True:
|
||||||
response = requests.get(url)
|
try:
|
||||||
response.raise_for_status() # 检查请求是否成功
|
response = requests.get(url)
|
||||||
data = response.json()
|
response.raise_for_status() # 检查请求是否成功
|
||||||
if data['status'] == 'success':
|
data = response.json()
|
||||||
return data['city']
|
if data['status'] == 'success':
|
||||||
else:
|
return data['city']
|
||||||
logging.error("获取当前城市失败: %s", data['message'])
|
else:
|
||||||
except requests.RequestException as e:
|
logging.error("获取当前城市失败: %s", data['message'])
|
||||||
logging.error("检索当前城市时出现网络错误: %s", e)
|
except (requests.RequestException, json.JSONDecodeError) as e:
|
||||||
except json.JSONDecodeError as e:
|
logging.error("检索或解析当前城市时出现错误: %s", e)
|
||||||
logging.error("解析当前城市数据时出现JSON解码错误: %s", e)
|
time.sleep(5) # 重试前等待5秒
|
||||||
return None
|
# 注意:无限重试可能会在特定情况下导致程序无法终止,请确保在实际使用中考虑适当的退出条件或限制重试次数。
|
||||||
|
|
||||||
def getWeath(city='101060101'):
|
def getWeath(city='101060101'):
|
||||||
headers = {
|
headers = {
|
||||||
@@ -97,4 +96,4 @@ except Exception as e:
|
|||||||
exit()
|
exit()
|
||||||
|
|
||||||
# 脚本正常结束后的清理操作
|
# 脚本正常结束后的清理操作
|
||||||
exit()
|
exit()
|
||||||
|
|||||||
Reference in New Issue
Block a user