27_internetWeatherPrediction.py 4.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. #!/usr/bin/env python
  2. #version : 2023.12.31
  3. #language : ch
  4. from maix import display
  5. from maix import image
  6. from maix import camera
  7. import bs4
  8. import ssl
  9. import json
  10. ssl._create_default_https_context = ssl._create_unverified_context
  11. import http.client
  12. camera.camera.config(size=(320,240))
  13. ScreenOrientation = False
  14. image.load_freetype("/root/preset/fonts/SourceHanSansCN-Regular.otf")
  15. def get_content(city):
  16. weather_list = []
  17. try:
  18. conn = http.client.HTTPSConnection("weather.visualcrossing.com")
  19. conn.request("GET", "/VisualCrossingWebServices/rest/services/timeline/"+city+"?unitGroup=us&include=days&key=XGMGVZSNH3RFJWNQYL4Z5THYS&contentType=json")
  20. res = conn.getresponse()
  21. data = res.read().decode("utf-8")
  22. # print(json.loads(data)["days"],'1111111111111111111')
  23. for i in json.loads(data)["days"]:
  24. # print(i)
  25. weather_list.extend([{"D":i["datetime"],"W":i["conditions"],
  26. "T":str(round((i["tempmax"]-32)/1.8))+"/"+str(round((i["tempmin"]-32)/1.8))+"℃"}])
  27. # weather_list = data.decode("utf-8")["days"]
  28. except:
  29. print('no result')
  30. # with open("/root/wResult.txt","w") as f:
  31. # f.write(data.decode("utf-8"))
  32. # print(weather_list)
  33. return weather_list
  34. # https://weather.visualcrossing.com/VisualCrossingWebServices/rest/services/weatherdata/forecast?locations=Herndon,VA,20170&aggregateHours=24&unitGroup=us&shortColumnNames=false&contentType=csv&key=YOURAPIKEY
  35. canvas = image.new(size=(320, 240),color = (15,21,46),mode = "RGB")
  36. canvas.draw_string(4 , 100 , "正在检查网络连接状态...", scale = 1, color = (255,255,255), thickness = 2)
  37. display.show(canvas)
  38. weather_all1 = get_content('shenzhen')
  39. weather_all2 = get_content('HongKong')
  40. weather_all3 = get_content('Macau')
  41. weather_all4 = get_content('Guangzhou')
  42. while True:
  43. canvas.clear()
  44. try:
  45. canvas = image.new(size=(320, 240),color = (15,21,46),mode = "RGB")
  46. #canvas.draw_string(2,20, ("Day:"+str(weather_all1[0]["D"])), scale = 1, color = (255,255,255) , thickness = 1)
  47. canvas.draw_string(2,20, ("气象变化:"+str(weather_all1[0]["W"])+" ,温度:"+str(weather_all1[0]["T"]))[:-1]+"℃", scale = 1, color = (255,255,255) , thickness = 1)
  48. canvas.draw_string(0,0, "深圳", scale = 1, color = (255,255,255) , thickness = 1)
  49. canvas.draw_string(0,60, "香港", scale = 1, color = (255,255,255) , thickness = 1)
  50. #canvas.draw_string(2,80, ("Day:"+str(weather_all2[0]["D"])), scale = 1, color = (255,255,255) , thickness = 1)
  51. canvas.draw_string(2,80, ("气象变化:"+str(weather_all2[0]["W"])+" ,温度:"+str(weather_all2[0]["T"]))[:-1]+"℃", scale = 1, color = (255,255,255) , thickness = 1)
  52. canvas.draw_string(0,120, "澳门", scale = 1, color = (255,255,255) , thickness = 1)
  53. #canvas.draw_string(2,140, ("Day:"+str(weather_all3[0]["D"])), scale = 1, color = (255,255,255) , thickness = 1)
  54. canvas.draw_string(2,140, ("气象变化:"+str(weather_all3[0]["W"])+" ,温度:"+str(weather_all3[0]["T"]))[:-1]+"℃", scale = 1, color = (255,255,255) , thickness = 1)
  55. canvas.draw_string(0,180, "广州", scale = 1, color = (255,255,255) , thickness = 1)
  56. #canvas.draw_string(2,200, ("Day:"+str(weather_all4[0]["D"])), scale = 1, color = (255,255,255) , thickness = 1)
  57. canvas.draw_string(2,200, ("气象变化:"+str(weather_all4[0]["W"])+" ,温度:"+str(weather_all4[0]["T"]))[:-1]+"℃",scale = 1, color = (255,255,255) , thickness = 1)
  58. except:
  59. canvas.draw_string(20,80, "获取天气信息失败!", scale = 1, color = (255,255,255) , thickness = 1)
  60. if ScreenOrientation:
  61. canvasVER = canvas.crop(0,0,240,320)
  62. canvasVER = canvasVER.rotate(-90, adjust=1)
  63. display.show(canvasVER)
  64. else:
  65. canvas.draw_image((image.open("/root/preset/img/exit_ff0000_24x24.png")).rotate(0, adjust=0),288,216,alpha=1)
  66. display.show(canvas)