forked from tantalor/gogogon
-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathconsumer.py
executable file
·59 lines (46 loc) · 1.34 KB
/
consumer.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
#!/usr/bin/env python
import os
import sys
import json
import logging
import logging.handlers
import signal
import pycurl
import optparse
def main():
global logger
parser = optparse.OptionParser()
parser.add_option('-f', '--file', dest="use_log_file",
default='/var/log/gogogon/consumer.log')
options, remainder = parser.parse_args()
log_file = options.use_log_file
formatter = logging.Formatter('%(process)d %(levelname)s %(created)d %(message)s', '%Y-%m-%d %H:%M:%S')
handler = logging.handlers.TimedRotatingFileHandler(
log_file, 'midnight', 1, backupCount=3
)
handler.setFormatter(formatter)
logger = logging.getLogger()
logger.addHandler( handler )
logger.setLevel(logging.DEBUG)
signal.signal(signal.SIGINT, shutdown)
def recv(line):
if line.strip():
(globalhash, url) = get_fields(line)
logger.info("%s %s" % (globalhash, url))
logger.debug("starting up")
curl = pycurl.Curl()
curl.setopt(pycurl.URL, "http://bitly.measuredvoice.com/usa.gov")
curl.setopt(pycurl.WRITEFUNCTION, recv)
curl.perform()
def get_fields(line):
data = json.loads(line)
globalhash = data.get('g')
url = data.get('u')
return (globalhash, url)
def shutdown(*args):
global logger
logger.debug("shutting down")
logger.flush()
sys.exit()
if __name__ == '__main__':
main()