You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

getweather.py 3.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. #!/usr/bin/python3
  2. from datetime import datetime
  3. import argparse
  4. import configparser
  5. import mysql.connector
  6. import requests
  7. import sys
  8. def usage():
  9. print(
  10. "Usage: " + __file__ +
  11. "[--config path_to_file.ini] [--station smhi_station_number] [--period latest-hour|latest-day|latest-months] [--database database] [--host dbhost] [--user dbuser] [--password dbpassword]\n"
  12. "Default configfile is weather.ini, any parameter can be overwritten on the command line"
  13. )
  14. parser = argparse.ArgumentParser()
  15. parser.add_argument('--config')
  16. parser.add_argument('--station')
  17. parser.add_argument('--period')
  18. parser.add_argument('--database')
  19. parser.add_argument('--host')
  20. parser.add_argument('--user')
  21. parser.add_argument('--password')
  22. args = parser.parse_args()
  23. config_file = "weather.ini"
  24. if args.config:
  25. config_file = args.config
  26. config = configparser.ConfigParser()
  27. config.read(config_file)
  28. db = config['MySQL']['database']
  29. host = config['MySQL']['host']
  30. user = config['MySQL']['user']
  31. password = config['MySQL']['password']
  32. station = config['SMHI']['station']
  33. period = config['SMHI']['period']
  34. if args.station:
  35. station = args.station
  36. if not station:
  37. station = "97100"
  38. if args.period:
  39. period = args.period
  40. if not period:
  41. period = "latest-day"
  42. if period not in ["latest-hour", "latest-day", "latest-months"]:
  43. period = "latest-day"
  44. if args.database:
  45. db = args.database
  46. if args.host:
  47. host = args.host
  48. if not host:
  49. host = "localhost"
  50. if args.user:
  51. user = args.usr
  52. if args.password:
  53. password = args.password
  54. if not (db and host and user and password):
  55. usage()
  56. sys.exit(1)
  57. mydb = mysql.connector.connect(auth_plugin='mysql_native_password',
  58. database=db,
  59. host=host,
  60. passwd=password,
  61. user=user)
  62. cursor = mydb.cursor()
  63. upsert = ("REPLACE INTO weather "
  64. "(date, time, rainfall, rel_hum, temp, winddir, windspeed, station, station_name) "
  65. "VALUES (%s, %s, %s, %s, %s, %s, %s, %s, %s)")
  66. # Metrics mapping
  67. rainfall = "7"
  68. rel_hum = "6"
  69. temp = "1"
  70. winddir = "3"
  71. windspeed = "4"
  72. results = {}
  73. for metric in [rainfall, rel_hum, temp, winddir, windspeed]:
  74. results[metric] = requests.get(
  75. url=
  76. 'https://opendata-download-metobs.smhi.se/api/version/1.0/parameter/{}/station/{}/period/{}/data.json'
  77. .format(metric, station, period))
  78. station_name = results[rainfall].json()['station']['name']
  79. rainfall_arr = sorted(results[rainfall].json()['value'],
  80. key=lambda i: i['date'])
  81. rel_hum_arr = sorted(results[rel_hum].json()['value'], key=lambda i: i['date'])
  82. temp_arr = sorted(results[temp].json()['value'], key=lambda i: i['date'])
  83. winddir_arr = sorted(results[winddir].json()['value'], key=lambda i: i['date'])
  84. windspeed_arr = sorted(results[windspeed].json()['value'],
  85. key=lambda i: i['date'])
  86. i = 0
  87. for k in rainfall_arr:
  88. ts = int(k['date'] / 1000)
  89. date = datetime.utcfromtimestamp(ts).strftime('%Y-%m-%d')
  90. time = datetime.utcfromtimestamp(ts).strftime('%H:%M:%S')
  91. rain = k['value']
  92. hum = rel_hum_arr[i]['value']
  93. te = temp_arr[i]['value']
  94. widi = winddir_arr[i]['value']
  95. wisp = windspeed_arr[i]['value']
  96. data = (date, time, rain, hum, te, widi, wisp, station, station_name)
  97. cursor.execute(upsert, data)
  98. i = i + 1
  99. mydb.commit()
  100. mydb.close()