Skip to content

Commit 7d6ae74

Browse files
esqu1adelq
authored andcommitted
Calendar for Penn Mobile Server (#66)
* calendar begun * pull method finished * range_parse functionality * calendar.py had to be renamed due to conflicts with built-in calendar module * adding functionality for year scraping * year change added, code cleanup coming * merge master * init * more docs and code cleanup * tests added, summer bug fix * silly mistake * tests added * docs and comments * iCal used for scraping instead * tests modified * every time... * static methods are no longer necessary * more tests
1 parent d887644 commit 7d6ae74

3 files changed

Lines changed: 84 additions & 0 deletions

File tree

penn/__init__.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,3 +9,4 @@
99
from .laundry import Laundry
1010
from .map import Map
1111
from .studyspaces import StudySpaces
12+
from .calendar3year import Calendar

penn/calendar3year.py

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
import requests
2+
import datetime
3+
4+
5+
BASE_URL = "https://www.google.com/calendar/ical/[email protected]/public/basic.ics"
6+
7+
8+
class Calendar(object):
9+
def __init__(self):
10+
pass
11+
12+
def pull_3year(self):
13+
"""Returns a list (in JSON format) containing all the events from the Penn iCal Calendar.
14+
15+
List contains events in chronological order.
16+
17+
Each element of the list is a dictionary, containing:
18+
- Name of the event 'name'
19+
- Start date 'start'
20+
- End date 'end'
21+
"""
22+
events = []
23+
r = requests.get(BASE_URL).text
24+
l = r.split("\r\n")
25+
d = {}
26+
for line in l:
27+
if line == "BEGIN:VEVENT":
28+
d = {}
29+
elif line[:7] == "DTSTART":
30+
raw_date = line.split(":")[1]
31+
start_date = datetime.datetime.strptime(raw_date, '%Y%m%d').date()
32+
d['start'] = start_date
33+
elif line[:5] == "DTEND":
34+
raw_date = line.split(":")[1]
35+
end_date = datetime.datetime.strptime(raw_date,'%Y%m%d').date()
36+
d['end'] = end_date
37+
elif line[:7] == "SUMMARY":
38+
name = line.split(":")[1]
39+
d['name'] = name.encode('utf-8').strip()
40+
elif line == "END:VEVENT":
41+
events.append(d)
42+
43+
events.sort(key=lambda d: d['start'])
44+
return events

tests/calendar_test.py

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
from nose.tools import ok_
2+
from penn import Calendar
3+
import datetime
4+
5+
class TestCalendar():
6+
7+
def setUp(self):
8+
self.calendar = Calendar()
9+
10+
def test_pull(self):
11+
l = self.calendar.pull_3year()
12+
ok_(len(l) > 0)
13+
for event in l:
14+
ok_(len(event) == 3)
15+
16+
def test_date(self):
17+
l = self.calendar.pull_3year()
18+
ok_(len(l) > 0)
19+
for event in l:
20+
if event['name'] == "Independence Day Observed (no classes)":
21+
independence = event['start']
22+
ok_(isinstance(independence, datetime.date))
23+
ok_(independence.month == 7)
24+
25+
def test_name(self):
26+
l = self.calendar.pull_3year()
27+
ok_(len(l) > 0)
28+
for event in l:
29+
start = event['start']
30+
end = event['end']
31+
ok_((end - start).total_seconds() >= 0)
32+
33+
def test_chrono(self):
34+
l = self.calendar.pull_3year()
35+
ok_(len(l) > 0)
36+
for i, event in enumerate(l[:-1]):
37+
start = event['start']
38+
nextstart = l[i]['start']
39+
ok_((nextstart - start).total_seconds() >= 0)

0 commit comments

Comments
 (0)