-
Notifications
You must be signed in to change notification settings - Fork 0
/
broadcast-syndication-feeds.py
executable file
·147 lines (101 loc) · 3.8 KB
/
broadcast-syndication-feeds.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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
#!/usr/bin/env python
import redis
import tempfile
import json
import os.path
import logging
import feedformatter
import time
import uuid
import re
import ConfigParser
import datetime
def syndicate(urls, cfg):
feed = feedformatter.Feed()
feed.feed["title"] = cfg.get('broadcast-syndication-feeds', 'title')
feed.feed["link"] = cfg.get('broadcast-syndication-feeds', 'link')
feed.feed["author"] = cfg.get('broadcast-syndication-feeds', 'author')
feed.feed["description"] = cfg.get('broadcast-syndication-feeds', 'description')
fname_pattern = re.compile('loopr-(\d+)\.gif')
for url in urls:
fname = os.path.basename(url)
match = fname_pattern.match(fname)
if match:
groups = match.groups()
ts = int(groups[0])
dt = datetime.datetime.fromtimestamp(ts)
title = dt.strftime('%HH%M')
pubdate = time.localtime(ts)
else:
title = fname
pubdate = time.localtime()
desc = '<img src="%s" />' % url
# So yeah... this causes the RSS2 serializer
# to LOSE ITS MIND. Because... Guido?
# hash = hashlib.md5()
# hash.update(url)
# guid = hash.digest()
# TO DO: flags to generate guids using artisanal
# integers...
guid = uuid.uuid4()
guid = str(guid)
item = {}
item["title"] = title
item["link"] = url
item["description"] = desc
item["pubDate"] = pubdate
item["guid"] = guid
# Why you no stick <content> ?
# https://code.google.com/p/feedformatter/source/browse/trunk/feedformatter.py#207
# item["content"] = { 'content': desc, 'type': 'html' }
feed.items.append(item)
rss1 = "rss1.xml"
rss2 = "rss2.xml"
# This is not yet being produced because the <content> blob
# is not being set correctly. See above. (20130127/straup)
atom = "atom.xml"
outdir = cfg.get('broadcast-syndication-feeds', 'out')
for what in (rss1, rss2):
logging.debug("generate %s" % what)
try:
tmpdir = tempfile.gettempdir()
pending_path = os.path.join(tmpdir, what)
publish_path = os.path.join(outdir, what)
if what.endswith('rss1.xml'):
feed.format_rss1_file(pending_path)
elif what.endswith('rss2.xml'):
feed.format_rss2_file(pending_path)
else:
feed.format_atom_file(pending_path)
os.rename(pending_path, publish_path)
except Exception, e:
logging.error("failed to write %s: %s" % (what, e))
if __name__ == '__main__':
import sys
import optparse
parser = optparse.OptionParser()
parser.add_option('-c', '--config', dest='config', action='store', help='path to a loopr config file - see source code for a sample config')
parser.add_option("-v", "--verbose", dest="verbose", action="store_true", help="enable chatty logging; default is false", default=False)
(opts, args) = parser.parse_args()
if opts.verbose:
logging.basicConfig(level=logging.DEBUG)
else:
logging.basicConfig(level=logging.INFO)
cfg = ConfigParser.ConfigParser()
cfg.read(opts.config)
channel = cfg.get('broadcast-syndication-feeds', 'pubsub_channel')
r = redis.Redis()
ps = r.pubsub()
ps.subscribe(channel)
while True:
for item in ps.listen():
logging.debug(item)
if item['type'] != 'message':
continue
elif item['channel'] == channel:
r.lpush('loopr_urls', item['data'])
r.ltrim('loopr_urls', 0, 100)
urls = r.lrange('loopr_urls', 0, 15)
syndicate(urls, cfg)
else:
pass