-
Notifications
You must be signed in to change notification settings - Fork 7
/
transfer.py
60 lines (52 loc) · 2.02 KB
/
transfer.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
'''Transfer a transaction database dump to the OpenSpending website where
it can be browsed and searched.
The spending.csv that is the result of dump.py is an optional parameter.
Requires config variables set to appropriate values:
openspending.apikey
openspending-dataset-source.url
'''
import sys
import os
import requests
from common import *
import sqlaload as sl
def transfer(spend_data_filepath):
# gather all the options
if not os.path.exists(spend_data_filepath):
print 'Error: Spend data file does not exist: %r' % spend_data_filepath
sys.exit(1)
size = os.stat(spend_data_filepath).st_size
if size < 3e9:
print 'Error: Size of the spend data (%i bytes) seems too small to be a complete DGU spend database!' % size
sys.exit(1)
apikey = config_get('openspending.apikey')
if not apikey:
print 'Error: Essential config option is not specified: openspending.apikey'
sys.exit(1)
url = config_get('openspending-dataset-source.url')
if not url:
print 'Error: Essential config option is not specified: openspending-dataset-source.url'
sys.exit(1)
# do the transfer
post_url = url + '/load'
headers = {'Authorization': 'Apikey %s' % apikey}
print 'Transferring %s (%.1f GB) to %s' % (spend_data_filepath, float(size)/1e9, post_url)
with open(spend_data_filepath, 'rb') as f:
res = requests.post(post_url, data=f, headers=headers)
if not res.ok:
print 'Error: POSTing the data did not succeed: %s %s' % \
(res.status_code, res.content)
sys.exit(1)
print 'Transferred ok' % spend_data_filepath
if __name__ == '__main__':
args = sys.argv[1:]
filter = {}
if '-h' in args or '--help' in args or len(args) > 1:
print __doc__
print 'Usage: python %s [<spending.csv>]' % sys.argv[0]
sys.exit(1)
elif len(args) == 1:
spend_data_filepath = args[0]
elif len(args) == 0:
spend_data_filepath = 'spending.csv'
transfer(spend_data_filepath)