forked from weshack/WesHappening
-
Notifications
You must be signed in to change notification settings - Fork 0
/
feed.py
executable file
·103 lines (83 loc) · 3.25 KB
/
feed.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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
#!/usr/bin/python
import feedparser
import re
import datetime
from weshappening import add_event
from get_data import xml_parser
from time import sleep
import logging
path = "/var/www/weshappening/weshappening/weshappening.log"
logging.basicConfig(filename=path, level=logging.DEBUG,
format="%(asctime)s: %(message)s")
logging.debug("Updating events")
feed_url = "http://events.wesleyan.edu/events/cal_rss_today"
feed = feedparser.parse(feed_url)
wesleying_feed = xml_parser()
ite = 0
for item in feed["items"]:
name = str(item["title"])
if name.startswith("TBA"):
name = name[4:]
else:
name = name[9:]
value = item["summary_detail"]["value"].split("<br />")
date = re.match("\d\d/\d\d/\d\d\d\d", str(value[0])).group().split("/")
time = re.search("(TBA|\d\d:\d\d (a|p)m( - \d\d:\d\d (a|p)m)*)", str(value[0])).group().split(" ")
desc = value[0]
loc = re.search("Location: .*", str(value[-1])).group().lstrip("Location: ")
link = ""
for v in value:
if v.startswith("URL"):
link = v.lstrip("URL: ")
if len(time) > 1:
t = time[0].split(":")
if (time[1] == "pm") and not (int(t[0]) == 12):
dt = datetime.datetime(int(date[2]), int(date[0]), int(date[1]), (int(t[0])+12)%24, int(t[1]))
elif (int(t[0]) == 12) and (t[1] == ("am")):
dt = datetime.datetime(int(date[2]), int(date[0]), int(date[1]), 0, int(t[1]))
else:
dt = datetime.datetime(int(date[2]), int(date[0]), int(date[1]), int(t[0]), int(t[1]))
else:
dt = datetime.datetime(int(date[2]), int(date[0]), int(date[1]))
cat = ite % 4
ite += 1
event = {"name": name, "location": loc, "time": dt, "link": link, "description": desc, "category":cat}
add_event(event)
sleep(1)
##FOR WESLEYING
ite = 0
for item in wesleying_feed:
name = str(item["title"])
date = str(item["date"])
time = re.search("(TBA|\d\d:\d\d (a|p)m( - \d\d:\d\d (a|p)m)*)", str(item['time']))
time = item["time"]
#this removes some unicode characters that I can't
#seem to convert to ascii. Therefore grammar=messy
#desc = item['description'].encode('ascii','ignore')[:75]
desc = "wesleying.org/category/events/"
loc = item["location"]
if loc:
loc = str(loc[0].encode('ascii','ignore'))
#print loc
link = str(item['url'])
try:
if len(time) > 1:
t = time[0].split(":")
if (time[1] == "pm") and not (int(t[0]) == 12):
dt = datetime.datetime(int(date[2]), int(date[0]), int(date[1]), (int(t[0])+12)%24, int(t[1]))
elif (int(t[0]) == 12) and (t[1] == ("am")):
dt = datetime.datetime(int(date[2]), int(date[0]), int(date[1]), 0, int(t[1]))
else:
dt = datetime.datetime(int(date[2]), int(date[0]), int(date[1]), int(t[0]), int(t[1]))
else:
dt = datetime.datetime(int(date[2]), int(date[0]), int(date[1]))
except:
# print "nope"
pass
cat = ite % 4
ite += 1
event = {"name": name, "location": loc, "time": datetime.datetime.today() ,
"link": link, "description": desc, "category":cat}
#print event,"EVENT"
add_event(event)
logging.debug("Events updated!")