-
Notifications
You must be signed in to change notification settings - Fork 0
/
broadcast-json.py
executable file
·81 lines (54 loc) · 2.19 KB
/
broadcast-json.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
#!/usr/bin/env python
import redis
import tempfile
import json
import os.path
import logging
import ConfigParser
def syndicate(urls, outdir, channel):
try:
fname = "%s.json" % channel
outfile = os.path.join(outdir, fname)
tmpdir = tempfile.gettempdir()
tmpfile = os.path.join(tmpdir, fname)
data = { 'loops': urls }
fh = open(tmpfile, 'w')
json.dump(data, fh, indent=2)
fh.close()
os.rename(tmpfile, outfile)
except Exception, e:
print e
if __name__ == '__main__':
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)
# MAYBE: allow CLI flags to override the config file?
# parser.add_option("-b", "--bucket", dest="bucket", help="", default=None)
# parser.add_option("-v", "--verbose", dest="verbose", action="store_true", help="enable chatty logging; default is false", default=False)
# parser.add_option("-c", "--count", dest="count", action="store", help="number of loops to include", default=15, type='int')
(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-json', 'pubsub_channel')
count = cfg.get('broadcast-json', 'count')
outdir = cfg.get('broadcast-json', 'out')
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, count)
syndicate(urls, outdir, channel)
else:
pass