Isoraqathedh

weather.py

Jun 6th, 2013
166
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.74 KB | None | 0 0
  1. #!/usr/bin/python
  2. # @author Shinmera
  3. # @pyver 3.x
  4. import json
  5. import urllib.request
  6. import time
  7. import sys
  8. import re
  9. import argparse
  10.  
  11. def coordinates(degree, minute, second):
  12.     return degree + minute/60 + second/3600
  13.  
  14. timestamp = time.time()
  15. apikey = "00000000000000000000000000000000"
  16. latitude = coordinates(22, 16, 42)
  17. longitude = coordinates(114, 9, 32)
  18. homeFile = r"\\psf\Home\Documents\AHK Scripts\weather\curr.txt"
  19. forecastIOAddress = "https://api.forecast.io/forecast/{0}/{1},{2}?units=si&exclude=hourly,daily,flags".format(apikey, latitude, longitude, timestamp)
  20. HKOAddress = ("http://www.hko.gov.hk/textonly/forecast/englishwx2.htm", "http://www.hko.gov.hk/wxinfo/ts/text_readings_e.htm")
  21. destPlace = r"\\psf\Home\Documents\AHK Scripts\weather\local.htm"
  22. weatherData = {};
  23.  
  24. ## Code below
  25. cmdparse = argparse.ArgumentParser()
  26. cmdparse.add_argument("source", choices=["hko", "fio"])
  27. cmdparse.add_argument("-v", "--verbose", action="store_true", dest="verbose")
  28. cmdparse.add_argument("-n", "--normalized", action="store_true", dest="normalized")
  29. args = vars(cmdparse.parse_args())
  30.  
  31. if args['source'] == "hko":
  32.     preGrabber = re.compile("</?pre>");
  33.     with urllib.request.urlopen(HKOAddress[0]) as request:
  34.         response = request.read().decode('big5hkscs')
  35.         cutUp = preGrabber.split(response)[1].strip()
  36.         stateValue = re.compile(r"No\. (\d\d?) - ([ \w]+)").search(cutUp)
  37.        
  38.         weatherData['summary'] = "{0} ({1})".format(stateValue.group(2).strip().title(), stateValue.group(1))
  39.         weatherData['temperature'] = float(re.compile("(\d\d?) degrees Celsius").search(cutUp).group(1))
  40.         weatherData['humidity'] = round(float(re.compile("(\d\d?) per cent").search(cutUp).group(1)) / 100, 2)
  41.     with urllib.request.urlopen(HKOAddress[1]) as request:
  42.         response = request.read().decode('ISO-8859-1')
  43.         cutUp = preGrabber.split(response)[1].strip().splitlines()
  44.         # 66 = Wong Chuk Hang WIND; 71 = HKO Observatory PRESSURE;; 35 ~ 38 are character rangea.
  45.         weatherData['windSpeed'] = round(float(''.join(cutUp[66][35:38]).strip()) / 3.6, 3)
  46.         weatherData['pressure'] =  round(float(''.join(cutUp[71][23:]).strip()), 5)
  47. elif args['source'] == "fio":
  48.     with urllib.request.urlopen(forecastIOAddress) as request:
  49.         response = request.read().decode('utf-8')
  50.         data = json.loads(response)
  51.         weatherData = data['currently']
  52.  
  53. tempUnit = "°C"
  54. presUnit = "hPa"
  55. if args['normalized']:
  56.     weatherData['temperature'] += 273.15
  57.     weatherData['pressure'] *= 100
  58.     tempUnit = "K"
  59.     presUnit = "Pa"
  60.  
  61. message = args['source'] + " reports: State: {0[summary]}; T = {0[temperature]} {1}; v = {0[windSpeed]} m/s; Hum = {0[humidity]}; P = {0[pressure]} {2}".format(weatherData, tempUnit, presUnit)
  62. print(message)
  63. with open(homeFile, 'w') as writeTarget:
  64.     writeTarget.write(message)
Advertisement
Add Comment
Please, Sign In to add comment