Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added date_time column #8

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
*.pyc
*.csv
.vscode/
.vscode/
.vs/
3 changes: 3 additions & 0 deletions .vs/PythonSettings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"SuppressEnvironmentCreationPrompt": true
}
8 changes: 8 additions & 0 deletions .vs/VSWorkspaceState.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"ExpandedNodes": [
"",
"\\util"
],
"SelectedNode": "\\weather_scraper.py",
"PreviewInSolutionExplorer": false
}
Binary file added .vs/slnx.sqlite
Binary file not shown.
Binary file added .vs/the-weather-scraper/v17/.suo
Binary file not shown.
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ python weather_scraper.py
```

### How to run TWS?
First, find the weather stations you are looking for.
First, find the weather stations you are looking for.
Then you just have to update 2 config files before running TWS.

1. Go to https://www.wunderground.com/wundermap and zoom in to your location
Expand Down Expand Up @@ -44,12 +44,12 @@ UNIT_SYSTEM = "metric"
FIND_FIRST_DATE = False
```

Now you are read to run your downloads:
Now you are ready to run your downloads:
```sh
$ python weather_scraper.py
```
Wait until TWS finishes writing your data to files with this naming pattern ***station_name.csv***!

You resulting CSV file will look something like this (if you give it a nice format)
You resulting CSV file will look something like this (if you format it nicely)

![CSV example](https://raw.githubusercontent.com/Karlheinzniebuhr/the-weather-scraper/master/resources/csv.JPG)
2 changes: 1 addition & 1 deletion TODO
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,4 @@ Todo:
✔ add error handling to UnitConverter @done(20-07-03 15:11)
✔ Write Dates in YYYY MM DD format @done(20-07-04 11:54)
☐ See if there is a way to start downloading from first available date
Write one more column with date_time
Write one more column with date_time
2 changes: 2 additions & 0 deletions util/Parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,10 @@ def parse_html_table(date_string: str, history_table: list) -> dict:
if i == 0:
date = datetime.strptime(date_string, "%Y-%m-%d")
time = datetime.strptime(td_content, "%I:%M %p")
blank = datetime.strptime('','')
row_dict['Date'] = date.strftime('%Y/%m/%d')
row_dict['Time'] = time.strftime('%I:%M %p')
row_dict['Date_time'] = ((time-blank)+date).strftime('%Y/%m/%d %I:%M:%S')
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

strftime will by default read a time and assume the date 1900/01/01, so i subtract that to get to 0000/00/00 and then all thats left is the timedelta of the time, which i then add to the date giving the full datetime

else:
row_dict[Parser.format_key(headers_list[i])] = td_content

Expand Down
39 changes: 16 additions & 23 deletions util/UnitConverter.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,16 +24,7 @@ def temperature(self, temp_string: str):
except Exception as e:
print(f'{e}! probably caused by an empty row in the data')
return 'NA'

def dew_point(self, dew_point_string: str):
try:
fahrenheit = float(re.findall(self.extract_numbers_pattern, dew_point_string)[0]) if dew_point_string else 'NA'
if self.system == "metric":
celsius = (fahrenheit - 32) * 5/9
return round(celsius, self.round_to_decimals)
else:
return fahrenheit


Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This was exactly the same as temperature(), so i DRYed it and followed the pattern we used for precipitation.

except Exception as e:
print(f'{e}! probably caused by an empty row in the data')
return 'NA'
Expand Down Expand Up @@ -111,29 +102,31 @@ def clean_and_convert(self, dict_list: list):
for key, value in dict.items():
if key == 'Date':
converted_dict['Date'] = value
if key == 'Time':
elif key == 'Time':
converted_dict['Time'] = value
if key == 'Temperature':
elif key == 'Date_time':
converted_dict['Date_time'] = value
elif key == 'Temperature':
converted_dict['Temperature'] = self.temperature(value)
if key == 'Dew_Point':
converted_dict['Dew_Point'] = self.dew_point(value)
if key == 'Humidity':
elif key == 'Dew_Point':
converted_dict['Dew_Point'] = self.temperature(value)
elif key == 'Humidity':
converted_dict['Humidity'] = self.humidity(value)
if key == 'Wind':
elif key == 'Wind':
converted_dict['Wind'] = value
if key == 'Speed':
elif key == 'Speed':
converted_dict['Speed'] = self.speed(value)
if key == 'Gust':
elif key == 'Gust':
converted_dict['Gust'] = self.speed(value)
if key == 'Pressure':
elif key == 'Pressure':
converted_dict['Pressure'] = self.pressure(value)
if key == 'Precip_Rate':
elif key == 'Precip_Rate':
converted_dict['Precip_Rate'] = self.precipitation(value)
if key == 'Precip_Accum':
elif key == 'Precip_Accum':
converted_dict['Precip_Accum'] = self.precipitation(value)
if key == 'UV':
elif key == 'UV':
converted_dict['UV'] = self.uv(value)
if key == 'Solar':
elif key == 'Solar':
converted_dict['Solar'] = self.solar(value)

converted_dict_list.append(converted_dict)
Expand Down
4 changes: 2 additions & 2 deletions weather_scraper.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
FIND_FIRST_DATE = config.FIND_FIRST_DATE


def scrap_station(weather_station_url):
def scrape_station(weather_station_url):

session = requests.Session()
timeout = 5
Expand Down Expand Up @@ -87,4 +87,4 @@ def scrap_station(weather_station_url):
for url in URLS:
url = url.strip()
print(url)
scrap_station(url)
scrape_station(url)