Advertisement
Guest User

Untitled

a guest
Oct 11th, 2018
108
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.05 KB | None | 0 0
  1. #!/usr/bin/env python
  2.  
  3. import json
  4. import urllib
  5. import urllib.parse
  6. import urllib.request
  7. import os
  8.  
  9.  
  10. def main():
  11. try:
  12. with open(os.path.join(
  13. os.path.expanduser("~"),
  14. '.config',
  15. 'polybar',
  16. 'city')) as f:
  17. city = f.readline().strip()
  18. with open(os.path.join(
  19. os.path.expanduser("~"),
  20. '.config',
  21. 'polybar',
  22. 'weather.json')) as f:
  23. api_key = json.load(f)['api_key']
  24. except:
  25. return '!'
  26.  
  27. try:
  28. data = urllib.parse.urlencode({'q': London, 'appid': api_key})
  29. weather = json.loads(urllib.request.urlopen(
  30. 'http://api.openweathermap.org/data/2.5/weather?' + data)
  31. .read())
  32. desc = weather['weather'][0]['description'].capitalize()
  33. temp = int(float(weather['main']['temp']) - 272.15)
  34. return '{}, {}C'.format(desc, temp)
  35. except:
  36. return '!'
  37.  
  38.  
  39. if __name__ == "__main__":
  40. print(main())
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement