Guest User

Untitled

a guest
May 11th, 2016
51
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.68 KB | None | 0 0
  1. import psycopg2
  2. import sys
  3. import random,urllib.request,os, csv, datetime,re
  4. from bs4 import BeautifulSoup
  5.  
  6.  
  7. con = None
  8. station_id = ["001D0A00B36E", "001D0A00B27C", "001D0A00B37E", "001D0A00529A"]
  9. stations_weather_data = []
  10. for i in station_id:
  11. url = "http://www.weatherlink.com/mapgetdata.php?DID="
  12. full_url = url + i
  13. htmlfile = urllib.request.urlopen(full_url).read().decode('utf-8')
  14. print(htmlfile)
  15. data = htmlfile.split('|')
  16. weather_data = {}
  17. weather_data['weather station name'] = data[3]
  18. weather_data['date'] = data[4]
  19. weather_data['temperature'] = float(''.join(ele for ele in data[5] if ele.isdigit() or ele == '.'))
  20. weather_data['temp_unit'] = " ".join(re.findall("[a-zA-Z]+", data[5]))
  21. weather_data['humidity'] = int(''.join(ele for ele in data[6] if ele.isdigit() or ele == '.'))
  22. weather_data['hum_unit'] = " ".join(re.findall("[%]+", data[6]))
  23. temp_wind = data[7].replace(" ", "")
  24. if temp_wind.isalnum():
  25. weather_data['wind_speed_status'] = " ".join(re.findall("[a-zA-Z]+", data[7]))
  26. weather_data['wind_unit'] = 'nounit'
  27. else:
  28. weather_data['wind_speed'] = float(''.join(ele for ele in data[7] if ele.isdigit() or ele == '.'))
  29. weather_data['wind_unit'] = " ".join(re.findall("[a-zA-Z/]+", data[7]))
  30. weather_data['wind_direction'] = " ".join(re.findall("[a-zA-Z0-9]+", data[8][:-4]))
  31. weather_data['wind_dir_unit'] = "degree"
  32. if data[9][-3:] == 'hPa':
  33. weather_data['barometer'] = float(''.join(ele for ele in data[9] if ele.isdigit() or ele == '.'))
  34. weather_data['barometer_unit'] = " ".join(re.findall("[a-zA-Z\']+", data[9]))
  35. else:
  36. weather_data['barometer'] = float(''.join(ele for ele in data[9] if ele.isdigit() or ele == '.'))
  37. weather_data['barometer_unit'] = data[9][-1:]
  38. weather_data['place_id'] = i
  39. stations_weather_data.append(weather_data)
  40.  
  41. final_weather_data = tuple(stations_weather_data)
  42. try:
  43.  
  44. con = psycopg2.connect("dbname='NPCLWeatherForecast' user='postgres' host='localhost' password='postgres'")
  45. cur = con.cursor()
  46. cur.mogrify("""INSERT INTO weather_data(temperature,temp_unit,humidity,hum_unit,wind,wind_speed_status,wind_unit,wind_dir,wind_dir_unit,barometer,bar_unit,updated_on,station_id) VALUES (%(temperature)s, %(temp_unit)s, %(humidity)s, %(hum_unit)s, %(wind)s, %(wind_speed_status)s, %(wind_unit)s, %(wind_dir)s, %(wind_dir_unit)s, %(barometer)s, %(bar_unit)s, %(updated_on)s, %(station_id)s);""", final_weather_data)
  47. ver = cur.fetchone()
  48. print(ver)
  49.  
  50.  
  51. except psycopg2.DatabaseError as e:
  52. print('Error {}'.format(e))
  53. sys.exit(1)
  54.  
  55.  
  56. finally:
  57.  
  58. if con:
  59. con.close()
Add Comment
Please, Sign In to add comment