-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.py
71 lines (61 loc) · 1.95 KB
/
utils.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/python
__author__ = 'tjones'
import csv
import json
from optparse import OptionParser
import os
import pprint
import requests
import sys
pp = pprint.PrettyPrinter()
def login(username, password, account):
login_data = {
"username": username,
"password": password,
"accountNumber": account,
"action": "login"
}
print json.dumps(login_data)
r = requests.post('https://api.rescuegroups.org/http/v2.json',
data=json.dumps(login_data))
if r.status_code == 200:
content = json.loads(r.content)
if content['status'] == 'ok':
return (content['data']['token'], content['data']['tokenHash'])
else:
print content['message']
else:
print r.text
return (None, None)
def get_data(data, processor_func, start=0, limit=100, verbose=False):
val = []
printHeader = True
while (1):
r = requests.post('https://api.rescuegroups.org/http/v2.json',
data=json.dumps(data))
if r.status_code == 200:
content = json.loads(r.content)
if content['status'] == 'ok':
if processor_func is not None:
val = processor_func(content, val)
else:
val = append_data(val, content['data'].items())
if verbose:
if printHeader:
print ' total rows %d' % int(content['foundRows'])
for k, v in content['data'].items():
print k, v
start += limit
data['search']['resultStart'] = start
if start >= int(content['foundRows']):
return val
else:
print content['messages']
return
else:
print r.text
return
def append_data(data, val):
for k,v in val:
data.append(v)
return data