my tools for viewing covid data
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.
 
 
 

68 lines
1.9 KiB

  1. #!/usr/bin/env python3
  2. import csv
  3. from influxdb import InfluxDBClient
  4. from urllib.parse import urlparse
  5. import datetime
  6. sources = {
  7. "recovered": "COVID-19/csse_covid_19_data/csse_covid_19_time_series/time_series_covid19_recovered_global.csv",
  8. "deaths": "COVID-19/csse_covid_19_data/csse_covid_19_time_series/time_series_covid19_deaths_global.csv",
  9. "cases": "COVID-19/csse_covid_19_data/csse_covid_19_time_series/time_series_covid19_confirmed_global.csv",
  10. }
  11. left_headers = ["Province/State", "Country/Region", "Lat", "Long"]
  12. def load_source(fpath, field_name):
  13. first = True
  14. dates = None
  15. data = []
  16. with open(fpath, "r") as f:
  17. r = csv.reader(f)
  18. for line in r:
  19. if first:
  20. first = False
  21. dates = line[len(left_headers):]
  22. continue
  23. state, country, lat, lon = line[0:4]
  24. row = line[len(left_headers):]
  25. for i, date in enumerate(dates):
  26. date_ob = datetime.datetime.strptime(date, "%m/%d/%y")
  27. data.append({
  28. "measurement": "covid",
  29. "tags": {
  30. "country": country,
  31. "state": state or "ALL"
  32. },
  33. "time": date_ob.isoformat(),
  34. "fields": {
  35. field_name: row[i]
  36. }
  37. })
  38. return data
  39. def get_payload():
  40. payload = []
  41. for field_name, fpath in sources.items():
  42. payload.extend(load_source(fpath, field_name))
  43. return payload
  44. def main():
  45. influx_uri = urlparse("http://localhost:10019/")
  46. influx_client = InfluxDBClient(influx_uri.hostname, str(influx_uri.port)) # user, password)
  47. influx_client.create_database("covid")
  48. influx_client.switch_database("covid")
  49. data = get_payload()
  50. influx_client.write_points(data)
  51. if __name__ == '__main__':
  52. main()