-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathholiday.py
executable file
·54 lines (45 loc) · 1.23 KB
/
holiday.py
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
#!/usr/bin/python
# Author: firebovine
# Purpose: HolidayAPI library
# Date: 08/31/2016
# Goals: The bear minimum.
import urllib
import urllib2
import json
import datetime
API_URL = "https://holidayapi.com/v1/holidays"
class Holiday:
def __init__(self, key, country='US'):
self.key = key
self.country = country
def __repr__(self):
return "HolidayAPI Instance"
def get(self, year=datetime.date.today().year, **kwargs):
args = { 'key': self.key,
'country': self.country,
'year': year }
args.update(kwargs)
params = urllib.urlencode(args)
try:
url = "%s?%s" % (API_URL, params)
response = urllib2.urlopen(url)
status_code = response.getcode()
data = json.load(response)
except urllib2.URLError, e:
print e.reason
return None
else:
return data
def today(self, **kwargs):
date = datetime.date.today()
params = { 'year': date.year,
'month': date.month,
'day': date.day }
params.update(kwargs)
return self.get(**params)
def upcoming(self):
params = { 'upcoming': 'true' }
return self.today(**params)
def previous(self):
params = { 'previous': 'true' }
return self.today(**params)