status2rss.py 1.29 KB
Newer Older
1 2 3
import datetime
import PyRSS2Gen
import sys
4 5
import sqlite3
import time
6 7 8 9 10 11 12
import base64

# Based on http://thehelpfulhacker.net/2011/03/27/a-rss-feed-for-your-crontabs/

# ### Defaults
TITLE = sys.argv[1]
LINK = sys.argv[2]
13
db_path = sys.argv[3]
14
DESCRIPTION = TITLE
15 16
SUCCESS = "SUCCESS"
FAILURE = "FAILURE"
17 18

items = []
19
status = ""
20

21 22 23 24 25 26 27 28
current_timestamp = int(time.time())
# We only build the RSS for the last ten days
period = 3600 * 24 * 10
db = sqlite3.connect(db_path)
rows = db.execute("select timestamp, status from status where timestamp>? order by timestamp", (current_timestamp - period,))
for row in rows:
  line_timestamp, line_status = row
  line_status = line_status.encode()
29

30 31
  if line_status == status:
    continue
32

33 34 35
  status = line_status

  event_time = datetime.datetime.fromtimestamp(line_timestamp).strftime('%Y-%m-%d %H:%M:%S')
36 37

  rss_item = PyRSS2Gen.RSSItem(
38 39
    title = status,
    description = "%s: %s" % (event_time, status),
40
    link = LINK,
41 42
    pubDate = event_time,
    guid = PyRSS2Gen.Guid(base64.b64encode("%s, %s" % (event_time, status)))
43 44 45 46
    )
  items.append(rss_item)

### Build the rss feed
47
items.reverse()
48 49 50 51 52 53 54 55
rss_feed = PyRSS2Gen.RSS2 (
  title = TITLE,
  link = LINK,
  description = DESCRIPTION,
  lastBuildDate = datetime.datetime.utcnow(),
  items = items
  )

56
print rss_feed.to_xml()