Guest User

Untitled

a guest
May 1st, 2018
111
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.49 KB | None | 0 0
  1. {
  2. "forecasts":
  3. [
  4. {
  5. "class": "fod_long_range_daily",
  6. "expire_time_gmt": 1525126617,
  7. "night": {
  8. "fcst_valid": 1525158000,
  9. "fcst_valid_local": "2018-05-01T19:00:00+1200",
  10. "golf_category": ""
  11. },
  12. "day": {
  13. "uv_warning": 0,
  14. "uv_desc": "Moderate",
  15. "golf_index": 10,
  16. "golf_category": "Excellent"
  17. }
  18. }
  19. ]
  20. }
  21.  
  22. import urllib3
  23. import psycopg2
  24. import json
  25.  
  26. conn = psycopg2.connect(host="localhost", database="nzaus",
  27. user="admin", password="123321")
  28. print("Database Connected")
  29. cur = conn.cursor()
  30. rowcount = cur.rowcount
  31.  
  32. http = urllib3.PoolManager()
  33. url = "https://api.data.com/v1/geocode/-35.228208/174.095969/forecast/daily/15day.json?language=en-US&units=m&apiKey=1234"
  34. try:
  35. response = http.request('GET', url)
  36. data = json.loads(response.data.decode('utf-8'))
  37.  
  38. for item in data['forecasts']:
  39. class = None
  40. time = None
  41. fcst_valid = None
  42. golf_category = None
  43.  
  44. result = []
  45.  
  46. class = item['class']
  47. time = item['expire_time_gmt']
  48. fcst_valid = item['night']['fcst_valid']
  49. golf_category = item['morning']['golf_category']
  50. result = [class,time,fcst_valid,golf_category]
  51.  
  52. cur.execute("""INSERT into datatable
  53. VALUES
  54. ( %s,
  55. %s,
  56. %s,
  57. %s,
  58. )""",(result))
  59. conn.commit()
  60. cur.close()
  61. except IOError as io:
  62. print("cannot open")
Add Comment
Please, Sign In to add comment