Skip to content

Instantly share code, notes, and snippets.

@iahuang
Last active May 31, 2024 19:35
Show Gist options
  • Save iahuang/909b8557765c1cb6d53a3ebe70c98c6b to your computer and use it in GitHub Desktop.
Save iahuang/909b8557765c1cb6d53a3ebe70c98c6b to your computer and use it in GitHub Desktop.
Simple Python API for snowdaycalculator.com
import requests
from dataclasses import dataclass
import re
import datetime
class SchoolType:
PUBLIC = 0
URBAN_PUBLIC = 0.4
RURAL_PUBLIC = -0.4
PRIVATE = -0.4
BOARDING = 1
def datetime_to_daycode(day: datetime.datetime):
return '{0:%Y%m%d}'.format(day)
class Prediction:
def __init__(self):
self.data = {}
def _set_data(self, daycode, chance):
self.data[daycode] = min(99, chance)
def chance(self, day):
daycode = datetime_to_daycode(day)
if daycode in self.data:
return self.data[daycode]
return None
def chance_today(self):
return self.chance(datetime.datetime.today())
def chance_tmrw(self):
return self.chance(datetime.datetime.today() + datetime.timedelta(days=1))
def predict(zipcode: str, snowdays: int=0, schooltype: int=SchoolType.PUBLIC):
response = requests.get(
'https://www.snowdaycalculator.com/prediction.php',
{
'zipcode': zipcode,
'snowdays': snowdays,
'extra': schooltype
}
).text
js_predictions = re.findall(r'theChance\[\d+\] = [\d\.]+;', response)
result = Prediction()
for pred in js_predictions:
key, value = pred.split(" = ")
daycode = re.findall(r'\d+', key)[0]
chance = re.findall(r'[\d+\.]+', value)[0]
result._set_data(daycode, chance)
return result

python-snowdaycalculator

Basic usage

import snowday

prediction = snowday.predict('12345', 0, snowday.SchoolType.PUBLIC)
print(f'There is a {prediction.chance_tmrw()}% chance of a snow day tomorrow.')

Documentation

snowday.predict(zipcode: str, snowdays: int=0, schooltype: int=SchoolType.PUBLIC)

Returns a Prediction object

snowday.Prediction()

  • Prediction.chance(day: datetime.datetime)

    Return the % chance of a snow day on the specified day represented as a datetime object or None if no prediction was listed for that day

  • Prediction.chance_today()

    Returns the chance of a snow day for today, or None if not found

  • Prediction.chance_tmrw()

    Returns the chance of a snow day for tomorrow, or None if not found

snowday.SchoolType

Constants describing the type of school

  • SchoolType.PUBLIC
  • SchoolType.URBAN_PUBLIC
  • SchoolType.RURAL_PUBLIC
  • SchoolType.PRIVATE
  • SchoolType.BOARDING
@thecooldoggo
Copy link

Awesome! nice job

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