CanadianGamer

Weather.py

Aug 3rd, 2023
105
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.19 KB | None | 0 0
  1. import requests
  2. import pyimage
  3. API_KEY = 'fe45f6fdef7b4ce7a45174059230308'
  4.  
  5.  
  6. class Weather:
  7.  
  8. def __init__(self, loaction:str= "08505"):
  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. pass
  59.  
  60.  
  61. def getWindSpeedMPH(self):
  62. return self.getCurrentData('wind_mph') +' mph'
  63.  
  64.  
  65. def getWindDirection(self):
  66. return self.getCurrentData('wind_dir')
  67.  
  68.  
  69. def getFeelsLikeF(self):
  70. return self.getCurrentData('feelslike_f') +'\u00B0F'
  71.  
  72.  
  73. def getFeelsLikeC(self):
  74. return self.getCurrentData('feelslike_c') +'\u00B0C'
  75.  
  76.  
  77.  
  78.  
  79. def getCurrentTempF(self):
  80. return self.getCurrentData("temp_f") +'\u00B0F'
  81.  
  82.  
  83. def getCurrentTempC(self):
  84. return self.getCurrentData("temp_c") +'\u00B0C'
  85.  
  86. # ---- Fetch ----
  87. def fetch(self, query):
  88. try:
  89. url = f'http://api.weatherapi.com/v1/current.json' + \
  90. f'?key={API_KEY}&q={query}&aqi=no'
  91. self.weatherData = requests.get(url).json()
  92. except:
  93. self.weatherData = {'error': []}
  94.  
Advertisement
Add Comment
Please, Sign In to add comment