27_internetWeatherPrediction.py 4.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. #!/usr/bin/env python
  2. #version : 2023.04.03
  3. #language : en
  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/CascadiaCodePL-Italic.ttf")
  15. image.load_freetype("/root/preset/fonts/CascadiaCodePL-Italic.ttf")
  16. def get_content(city):
  17. weather_list = []
  18. try:
  19. conn = http.client.HTTPSConnection("weather.visualcrossing.com")
  20. conn.request("GET", "/VisualCrossingWebServices/rest/services/timeline/"+city+"?unitGroup=us&include=days&key=XGMGVZSNH3RFJWNQYL4Z5THYS&contentType=json")
  21. res = conn.getresponse()
  22. data = res.read().decode("utf-8")
  23. # print(json.loads(data)["days"],'1111111111111111111')
  24. for i in json.loads(data)["days"]:
  25. # print(i)
  26. weather_list.extend([{"D":i["datetime"],"W":i["conditions"],
  27. "T":str(round((i["tempmax"]-32)/1.8))+"/"+str(round((i["tempmin"]-32)/1.8))+"℃"}])
  28. # weather_list = data.decode("utf-8")["days"]
  29. except:
  30. print('no result')
  31. # with open("/root/wResult.txt","w") as f:
  32. # f.write(data.decode("utf-8"))
  33. # print(weather_list)
  34. return weather_list
  35. # https://weather.visualcrossing.com/VisualCrossingWebServices/rest/services/weatherdata/forecast?locations=Herndon,VA,20170&aggregateHours=24&unitGroup=us&shortColumnNames=false&contentType=csv&key=YOURAPIKEY
  36. canvas = image.new(size=(320, 240),color = (15,21,46),mode = "RGB")
  37. canvas.draw_string(4 , 100 , "Checking Wi-Fi connection", scale = 1, color = (255,255,255), thickness = 2)
  38. display.show(canvas)
  39. weather_all1 = get_content('shenzhen')
  40. weather_all2 = get_content('HongKong')
  41. weather_all3 = get_content('Macau')
  42. weather_all4 = get_content('Guangzhou')
  43. while True:
  44. canvas.clear()
  45. try:
  46. canvas = image.new(size=(320, 240),color = (15,21,46),mode = "RGB")
  47. #canvas.draw_string(2,20, ("Day:"+str(weather_all1[0]["D"])), scale = 1, color = (255,255,255) , thickness = 1)
  48. canvas.draw_string(2,20, ("W/W:"+str(weather_all1[0]["W"])+" ,T:"+str(weather_all1[0]["T"]))[:-1]+"'C", scale = 1, color = (255,255,255) , thickness = 1)
  49. canvas.draw_string(0,0, "Shenzhen", scale = 1, color = (255,255,255) , thickness = 1)
  50. canvas.draw_string(0,60, "Hong Kong", scale = 1, color = (255,255,255) , thickness = 1)
  51. #canvas.draw_string(2,80, ("Day:"+str(weather_all2[0]["D"])), scale = 1, color = (255,255,255) , thickness = 1)
  52. canvas.draw_string(2,80, ("W/W:"+str(weather_all2[0]["W"])+" ,T:"+str(weather_all2[0]["T"]))[:-1]+"'C", scale = 1, color = (255,255,255) , thickness = 1)
  53. canvas.draw_string(0,120, "Macau", scale = 1, color = (255,255,255) , thickness = 1)
  54. #canvas.draw_string(2,140, ("Day:"+str(weather_all3[0]["D"])), scale = 1, color = (255,255,255) , thickness = 1)
  55. canvas.draw_string(2,140, ("W/W:"+str(weather_all3[0]["W"])+" ,T:"+str(weather_all3[0]["T"]))[:-1]+"'C", scale = 1, color = (255,255,255) , thickness = 1)
  56. canvas.draw_string(0,180, "Guangzhou", scale = 1, color = (255,255,255) , thickness = 1)
  57. #canvas.draw_string(2,200, ("Day:"+str(weather_all4[0]["D"])), scale = 1, color = (255,255,255) , thickness = 1)
  58. canvas.draw_string(2,200, ("W/W:"+str(weather_all4[0]["W"])+" ,T:"+str(weather_all4[0]["T"]))[:-1]+"'C",scale = 1, color = (255,255,255) , thickness = 1)
  59. except:
  60. canvas.draw_string(20,80, "Get weather information failed!", scale = 1, color = (255,255,255) , thickness = 1)
  61. if ScreenOrientation:
  62. canvasVER = canvas.crop(0,0,240,320)
  63. canvasVER = canvasVER.rotate(-90, adjust=1)
  64. display.show(canvasVER)
  65. else:
  66. canvas.draw_image((image.open("/root/preset/img/exit_ff0000_24x24.png")).rotate(0, adjust=0),288,216,alpha=1)
  67. display.show(canvas)