-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathfreshData_script.py
More file actions
122 lines (102 loc) · 5.41 KB
/
Copy pathfreshData_script.py
File metadata and controls
122 lines (102 loc) · 5.41 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
# -----------------------------------------------------------------------------------
# Step 1: Import the modules
# -----------------------------------------------------------------------------------
import seaborn as sns
import json
import datetime as dt
import matplotlib.pyplot as plt
import requests as req
import pandas as pd
import random as rd
from citipy import citipy
import localenv
import aux
# -----------------------------------------------------------------------------------
# Step 2: Store all our basic API data. I'd use params, but I couldn't get it to work
# with the unique structure of some of these parameters
# -----------------------------------------------------------------------------------
openWeatherURL = "http://api.openweathermap.org/data/2.5/weather?"
# -----------------------------------------------------------------------------------
# Step 3: Create a list of non-duplicate cities
# -----------------------------------------------------------------------------------
# create a list that we'll store all our data in
weatherData_list = []
city_list = []
# create dupe checking set
cityDupeChecker = set()
# create counter
i = 0
# create a list of 1500 possible cities (so even if a query fails still have good sample)
while len(cityDupeChecker) < 1500:
# set random lat and long
latitude = rd.uniform(-90.0,90.0)
longitude = rd.uniform(-180.0,180.0)
# get city
city = citipy.nearest_city(latitude,longitude).city_name
country = citipy.nearest_city(latitude,longitude).country_code
city_country_pair = f"{city}_{country}"
if city_country_pair not in cityDupeChecker:
cityDupeChecker.add(city_country_pair)
# try to pull in a random value and add to dupe checker
city_list.append([city, country])
# -----------------------------------------------------------------------------------
# Step 4: Pull city data from openweatherapi
# -----------------------------------------------------------------------------------
for i in range(len(city_list)):
# get current city and country
city = city_list[i][0]
country= city_list[i][1]
# try searching by city + country code
try:
response = req.get(f"{openWeatherURL}q={city},{country}&units=imperial&APPID={localenv.api_key}").json()
# add information from response to list
weatherData_list.append({'cityID':response['id'],
'date': dt.datetime.utcnow().strftime('%Y-%m-%d'),
'time': dt.datetime.utcnow().strftime('%H:%M'),
'city': response['name'],
'country': country.upper(),
'continent': aux.continentFromCountry(country.upper()),
'latitude':response['coord']['lat'],
'longitude':response['coord']['lon'],
'humidity':response['main']['humidity'],
'temperature':response['main']['temp'],
'windSpeed':response['wind']['speed'],
'cloudiness':response['clouds']['all'] })
#show city
#aux.displayProcessingCity(i,response)
except:
try:
response = req.get(f"{openWeatherURL}q={city}&units=metric&APPID={localenv.api_key}").json()
# add information from response to list
weatherData_list.append({'cityID':response['id'],
'date': dt.datetime.now().strftime('%Y-%m-%d'),
'time': dt.datetime.utcnow().strftime('%H:%M'),
'city': response['name'],
'country': response['main']['sys']['country'].upper(),
'continent': aux.continentFromCountry(response['main']['sys']['country'].upper()),
'latitude': response['coord']['lat'],
'longitude': response['coord']['lon'],
'humidity':response['main']['humidity'],
'temperature':response['main']['temp'],
'windSpeed':response['wind']['speed'],
'cloudiness':response['clouds']['all']})
#show city
#aux.displayProcessingCity(i,response)
except:
#aux.displayFailedCity(i, city, country)
pass
# -----------------------------------------------------------------------------------
# Step 4: Create a pretty csv
# -----------------------------------------------------------------------------------
cleanedWeather_df = pd.DataFrame(weatherData_list)
# rearrange columns sensibly
cleanedWeather_df = cleanedWeather_df[['cityID', 'date', 'time', 'city', 'country', 'continent',
'latitude', 'longitude',
'temperature',
'humidity',
'windSpeed',
'cloudiness']].sort_index(ascending=True)
# export the csv
date = dt.datetime.now().strftime('%Y-%m-%d')
filename = f"../csv/WeatherData{date}.csv"
cleanedWeather_df.to_csv(filename)