CanadianGamer

Weather.py

Aug 6th, 2023
63
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.46 KB | None | 0 0
  1. import requests
  2. import tkinter
  3. API_KEY = 'fe45f6fdef7b4ce7a45174059230308'
  4.  
  5.  
  6. class Weather:
  7.  
  8. def __init__(self, loaction:str= "08341"):
  9. self.weatherData = {}
  10. self.fetch(loaction)
  11.  
  12. # ---- Information ----
  13.  
  14. # ---- Loaction ----
  15. def getLocationData(self, name):
  16. data = self.weatherData['location'][name]
  17. return str(data)
  18.  
  19. def getCity(self):
  20. return self.getLocationData('name')
  21.  
  22.  
  23. def getState(self):
  24. return self.getLocationData('region')
  25.  
  26.  
  27. def getCountry(self):
  28. return self.getLocationData('country')
  29.  
  30.  
  31. def getTimeZone(self):
  32. return self.getLocationData('tz_id')
  33.  
  34.  
  35. def getLocation(self):
  36. if 'America' in self.getTimeZone():
  37. return f'{self.getCity()}, {self.getState()}'
  38. else:
  39. return f'{self.getCity()}, {self.getCountry()}'
  40.  
  41. # ------ Current Data -----
  42.  
  43. def getCurrentData(self, name):
  44. data = self.weatherData['current'][name]
  45. return data if name == 'condition' else str(data)
  46.  
  47.  
  48. def getHumid(self):
  49. return self.getCurrentData("humidity") + "%"
  50.  
  51.  
  52. def getConditionText(self):
  53. condition = self.getCurrentData('condition')
  54. return str(condition['text'])
  55.  
  56.  
  57. def getConditionIcon(self):
  58. condition = self.getCurrentData('condition')
  59. icon_url = f"http:{condition['icon']}"
  60. try:
  61. icon_data = requests.get(icon_url).content
  62. except Exception as ex:
  63. print(ex)
  64. icon_data = None
  65. return icon_data
  66.  
  67.  
  68.  
  69. def getWindSpeedMPH(self):
  70. return self.getCurrentData('wind_mph') +' mph'
  71.  
  72.  
  73. def getWindDirection(self):
  74. return self.getCurrentData('wind_dir')
  75.  
  76.  
  77. def getFeelsLikeF(self):
  78. return self.getCurrentData('feelslike_f') +'\u00B0F'
  79.  
  80.  
  81. def getFeelsLikeC(self):
  82. return self.getCurrentData('feelslike_c') +'\u00B0C'
  83.  
  84.  
  85.  
  86.  
  87. def getCurrentTempF(self):
  88. return self.getCurrentData("temp_f") +'\u00B0F'
  89.  
  90.  
  91. def getCurrentTempC(self):
  92. return self.getCurrentData("temp_c") +'\u00B0C'
  93.  
  94. # ---- Fetch ----
  95. def fetch(self, query):
  96. try:
  97. url = f'http://api.weatherapi.com/v1/current.json' + \
  98. f'?key={API_KEY}&q={query}&aqi=no'
  99. self.weatherData = requests.get(url).json()
  100. except:
  101. self.weatherData = {'error': []}
  102.  
Advertisement
Add Comment
Please, Sign In to add comment