Skip to content

Instantly share code, notes, and snippets.

@greencoder
Last active June 24, 2020 19:51
Show Gist options
  • Save greencoder/f52af91b8e4e7208e797a2b79c49415c to your computer and use it in GitHub Desktop.
Save greencoder/f52af91b8e4e7208e797a2b79c49415c to your computer and use it in GitHub Desktop.
Save your weather from MeteoNook
const years = [2020, 2021, 2022];
const months = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12];
const days = Array.from({length:31},(v,k)=>k+1)
var dailyConditions = [];
var titleRegex = /\(([^)]+)\)/;
years.forEach(year => {
months.forEach(month => {
days.forEach(day => {
displayDayModal({isPreview: !1, year: year, month: month, day: day, isSouthern: false})
var hourlyConditions = [];
let title = document.querySelector('h5.modal-title').innerText;
let nodes = document.querySelector('div.modal-content').querySelector('div.modal-body').querySelectorAll('p');
let wxType = titleRegex.exec(title)[1];
nodes.forEach(node => {
hourlyConditions.push(node.innerHTML);
});
dailyConditions.push({
date: `${year}-${month}-${day}`,
wxType: wxType,
year: year,
month: month,
day: day,
conditions: hourlyConditions,
});
});
});
});
var data = JSON.stringify(dailyConditions, undefined, 2);
var blob = new Blob([data], {type: 'text/json'});
var e = document.createEvent('MouseEvents');
var a = document.createElement('a');
a.download = 'weather.json';
a.href = window.URL.createObjectURL(blob)
a.dataset.downloadurl = ['text/json', a.download, a.href].join(':');
e.initMouseEvent('click', true, false, window, 0, 0, 0, 0, 0, false, false, false, false, 0, null)
a.dispatchEvent(e);
var year = 2020;
var month = 6;
var day = 22;
displayDayModal({isPreview: !1, year: year, month: month, day: day, isSouthern: false})
import arrow
import bs4
import pathlib
import json
contents = pathlib.Path('weather.json').read_text()
data = json.loads(contents)
days = []
for day_data in data:
# Skip invalid dates that were scraped
try:
datetime = arrow.get(day_data.get('date'))
except ValueError:
continue
day = {
'date': datetime.format('YYYY-MM-DD'),
'type': day_data.get('wxType'),
'conditions': [],
'special': None,
'meteors': [],
}
for condition in day_data.get('conditions', []):
hour = {
'd': None,
'w': None,
'c': None,
's': [],
}
# Meteor Showers
if condition.startswith('🌠'):
trimmed_condition = condition.replace('🌠', '').strip()
day['meteors'].append(trimmed_condition)
continue
# Event Days
elif condition.startswith('πŸŽ‰'):
trimmed_condition = condition.replace('πŸŽ‰', '').strip()
day['special'] = trimmed_condition
continue
# Regular Conditions String
condition_soup = bs4.BeautifulSoup(condition)
hour['d'] = arrow.get(day_data.get('date') + ' ' + condition_soup.b.text).for_json()
hour['w'] = int(condition_soup.find('span', {'title': 'Wind strength'}).text[-1])
hour['c'] = condition.split(' ')[-1]
for span_el in condition_soup.find_all('span', {'title': None}):
hour['s'].append(span_el.text)
day['conditions'].append(hour)
# Calculate the datetimes for each meteor
parsed_meteors = []
for meteor in day.get('meteors'):
for time in meteor.split(','):
dt = arrow.get(day.get('date') + ' ' + time.strip()).for_json()
parsed_meteors.append(dt)
day['meteors'] = parsed_meteors
# Append all the hours to the day
days.append(day)
output = json.dumps(days, indent=2)
pathlib.Path('parsed_weather.json').write_text(output)
@greencoder
Copy link
Author

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment