Advertisement
ProfCTurner

Untitled

Dec 3rd, 2020
1,136
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.17 KB | None | 0 0
  1. import csv
  2. import datetime
  3. import json
  4. import pandas as pd
  5. import urllib.request as request
  6.  
  7. one_day = datetime.datetime.today() - datetime.timedelta(days=1)
  8. two_days = datetime.datetime.today() - datetime.timedelta(days=2)
  9. thr_days = datetime.datetime.today() - datetime.timedelta(days=3)
  10. for_days = datetime.datetime.today() - datetime.timedelta(days=4)
  11. fiv_days = datetime.datetime.today() - datetime.timedelta(days=5)
  12. six_days = datetime.datetime.today() - datetime.timedelta(days=6)
  13. sev_days = datetime.datetime.today() - datetime.timedelta(days=7)
  14. egt_days = datetime.datetime.today() - datetime.timedelta(days=8)
  15.  
  16. one_day_str = one_day.strftime("%Y-%m-%dT00:00:00.000")
  17. two_day_str = two_days.strftime("%Y-%m-%dT00:00:00.000")
  18. thr_day_str = thr_days.strftime("%Y-%m-%dT00:00:00.000")
  19. for_day_str = for_days.strftime("%Y-%m-%dT00:00:00.000")
  20. fiv_day_str = fiv_days.strftime("%Y-%m-%dT00:00:00.000")
  21. six_day_str = six_days.strftime("%Y-%m-%dT00:00:00.000")
  22. sev_day_str = sev_days.strftime("%Y-%m-%dT00:00:00.000")
  23. egt_day_str = egt_days.strftime("%Y-%m-%dT00:00:00.000")
  24.  
  25. url_one = 'https://health.data.ny.gov/resource/xdss-u53e.json?test_date=' + one_day_str
  26. url_two = 'https://health.data.ny.gov/resource/xdss-u53e.json?test_date=' + two_day_str
  27. url_thr = 'https://health.data.ny.gov/resource/xdss-u53e.json?test_date=' + thr_day_str
  28. url_for = 'https://health.data.ny.gov/resource/xdss-u53e.json?test_date=' + for_day_str
  29. url_fiv = 'https://health.data.ny.gov/resource/xdss-u53e.json?test_date=' + fiv_day_str
  30. url_six = 'https://health.data.ny.gov/resource/xdss-u53e.json?test_date=' + six_day_str
  31. url_sev = 'https://health.data.ny.gov/resource/xdss-u53e.json?test_date=' + sev_day_str
  32. url_egt = 'https://health.data.ny.gov/resource/xdss-u53e.json?test_date=' + egt_day_str
  33.  
  34. url_lst = [url_one,url_two,url_thr, url_for, url_fiv, url_six, url_sev, url_egt]
  35.  
  36. d = []
  37.  
  38. def write_json(data, filename='data.json'):
  39.     with open(filename,'w') as f:
  40.         json.dump(data, f, indent=4)
  41.  
  42. for url in url_lst:
  43.     with request.urlopen(url) as response:
  44.         #   print(url)
  45.         source = response.read()
  46.         data = json.loads(source)
  47.  
  48.         if len(data) == 0:
  49.             continue
  50.  
  51.         with open ("covid.json", 'w') as outfile:
  52.             json.dump(data, outfile)
  53.  
  54.         with open('covid.json') as json_data:
  55.             j = json.load(json_data)
  56.             d.append(j)
  57.             write_json(d)  
  58.    
  59.         filename = "County Stats.csv"
  60.         fields = ["Date","County", "New Positives", "All Positives", "New Tests", "All Tests"]
  61.         with open(filename, 'w') as fw:
  62.             cf = csv.writer(fw, lineterminator='\n')
  63.             # write the header
  64.             cf.writerow(fields)
  65.        
  66.             for counties in data:
  67.                 date = counties['test_date']
  68.                 cnty = counties['county']
  69.                 new_pos = counties['new_positives']
  70.                 cum_pos = counties['cumulative_number_of_positives']
  71.                 new_tests = counties['total_number_of_tests']
  72.                 cum_tests = counties['cumulative_number_of_tests']
  73.                 cf.writerow([date,cnty, new_pos, cum_pos, new_tests, cum_tests])
  74.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement