-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmodels.py
103 lines (83 loc) · 2.92 KB
/
models.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
from google.appengine.ext import db
import feedparser
from datetime import datetime
from urllib import urlencode, quote
from urllib2 import urlopen
HC_URL = 'https://api.hipchat.com/v1/rooms/message'
class RSSFeed(db.Model):
name = db.StringProperty(indexed=False)
url = db.LinkProperty()
last_fetched = db.DateTimeProperty()
room = db.StringProperty()
token = db.StringProperty()
def getMessages(self):
return self.hipchatmessage_set.order('-date')
def update(self):
feed_response = feedparser.parse(self.url)
items = feed_response.get('entries')
for item in items:
exists = HipChatMessage.get_by_key_name(item.get('guid'))
if not exists:
t = item.published_parsed
item_date = datetime(t.tm_year, t.tm_mon, t.tm_mday, t.tm_hour, t.tm_min, t.tm_sec)
message = HipChatMessage(
feed = self,
created = datetime.now(),
date = item_date,
title = item.title,
summary = item.summary,
key_name = item.guid,
)
message.put()
self.last_fetched = datetime.now()
self.put()
return
class HipChatMessage(db.Model):
feed = db.ReferenceProperty(RSSFeed)
created = db.DateTimeProperty()
date = db.DateTimeProperty()
dispatched = db.DateTimeProperty()
summary = db.TextProperty()
title = db.TextProperty()
def dispatch(self):
summary = self.summary
title = self.title
if len(summary) > 200:
summary = summary[:200] + '…'
message = """
<b>{title}</b><br>
<p>
{summary}
</p>
(<a href="{guid}">Status Page »</a>)
""".format(
title = title,
summary = summary,
guid = 'http://status.aws.amazon.com/',
)
if title.find('[RESOLVED]') > -1:
color = 'green'
elif title.find('Informational message') > -1:
color = 'yellow'
else:
color = 'red'
payload = {
'auth_token' : self.feed.token,
'room_id' : self.feed.room,
'from' : self.feed.name,
'message' : message,
'notify' : 1,
'color' : color,
}
params = []
for param, value in payload.items():
params.append('%s=%s' % (param, quote(str(value))))
url = HC_URL + '?' + '&'.join(params)
try:
r = urlopen(url, data={})
except:
return False
else:
self.dispatched = datetime.now()
self.put()
return True