-
Notifications
You must be signed in to change notification settings - Fork 4
/
firehoser.py
executable file
·71 lines (56 loc) · 2.36 KB
/
firehoser.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
#!/usr/bin/env python
import sys
import json
import requests
from time import sleep
import argparse
import ConfigParser
import firehoser_requests
#Handle Command Line Parameters
parser = argparse.ArgumentParser(description="Settings to control the feed")
#pull a config file if available
parser.add_argument("-c", "--config_file", help="the config file your want to use (optional)", type=str, required=False, default="sample.conf")
args, remaining_argv = parser.parse_known_args()
config = ConfigParser.ConfigParser()
defaults = {}
if args.config_file:
config = ConfigParser.SafeConfigParser()
config.read([args.config_file])
defaults = dict(config.items("Firehose"))
parser.set_defaults(**defaults)
parser.add_argument("-n", "--number", help="The number of hits to return", required=False, type=int)
parser.add_argument("-s", "--seconds", help="The number of seconds between the each hit", type=float, required=False)
parser.add_argument("-a", "--access_token", help="The access token you will use for the requests", type=str, required=False)
parser.add_argument("-u", "--firehose_url", help="The URL of your firehose stream", type=str, required=False)
args = parser.parse_args()
#Parse Config File
#Assemble the request
if args.access_token is not None:
access_token = args.access_token
else:
sys.exit("Error: Please Provide an Access Token (-a or --access_token)")
if args.firehose_url is not None:
url = args.firehose_url;
else:
sys.exit("Error: Please provide a Firehose URL (-u or --firehose_url)")
bearer = "Bearer " + access_token
headers = {"Authorization": bearer,"accept-encoding":"gzip,deflate"}
r = firehoser_requests.get(url, stream=True, headers=headers)
#Read the Stream
if r.status_code == requests.codes.ok:
count = 0
for line in r.iter_lines():
if line:
print "\r\n"
print(json.dumps(json.loads(line),indent=2))
#Break the loop if there are is a -n argument
if args.number is not None:
count = count + 1
if count >= args.number:
break
#How long to wait between writes
if args.seconds is not None :
sleep(args.seconds)
else:
print "There was a problem with the Request"
print "Returned Status Code: " + str(r.status_code)