-
Notifications
You must be signed in to change notification settings - Fork 0
/
currencies.py
executable file
·86 lines (72 loc) · 2.69 KB
/
currencies.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
82
83
84
85
86
#!/usr/bin/env python3
import sys
import urllib.request
import json
import csv
import re
from datetime import datetime
import signal
import argparse
def signal_handler(signal, frame):
print('\n')
sys.exit(0)
def main():
signal.signal(signal.SIGINT, signal_handler)
parser = argparse.ArgumentParser(
description="""Create a csv of currencies from www.coinbase.com
Use .import "| tail - n + 2 currencies.csv"
onboardingclient_currencytypes to import in sqllite."""
)
parser.add_argument("-o", "--output",
action='store', nargs='?',
type=argparse.FileType('w'),
default='currencies.csv',
help='Path and Name of a file to direct output.'
)
parser.add_argument("-v", "--verbose",
action="store_true",
help="Print to stdout."
)
args = parser.parse_args()
currencies = get_currencies()
currencies = scrub_currencies(currencies)
if args.verbose:
for row in currencies.items():
for element in row:
print(''.join(str(element)), end=' ')
print('')
# print("Creating file: ", args.output)
with args.output as csvfile:
fieldnames = ['id', 'currency code', 'currency name',
'create timestamp', 'modified timestamp',
'createById', 'modifiedById'
]
writer = csv.DictWriter(
csvfile, fieldnames=fieldnames, quoting=csv.QUOTE_NONNUMERIC)
# writer = csv.writer(csvfile, quotechar='|')
writer.writeheader()
id = 0
for code, name in currencies.items():
id += 1
writer.writerow({
'id': id,
'currency code': code,
'currency name': name,
'modified timestamp': datetime.now().strftime("%Y-%m-%d %H:%M:%S.%f"),
'create timestamp': datetime.now().strftime("%Y-%m-%d %H:%M:%S.%f"),
'createById': 'coinbase',
'modifiedById': 'coinbase'
})
def get_currencies():
query_currencies = "http://www.coinbase.com/api/v1/currencies/"
with urllib.request.urlopen(query_currencies) as document:
# print(document.info().items())
currencies = json.loads(document.read().decode("utf-8"))
return currencies
def scrub_currencies(currencies):
currencies = dict((key, re.sub(' \(.*\)', '', value))
for value, key in currencies)
# print(currencies)
return currencies
if __name__ == '__main__':
main()