-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcsv_executor.py
71 lines (53 loc) · 2.13 KB
/
csv_executor.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
import requests
import sys
from datetime import datetime
import pandas as pd
from pandas import ExcelWriter
import copy
class CSVExecutor(object):
@staticmethod
def cryptocompare_api_call(coin_name):
# get price (e.g. in USD) for certain coin_name from the cryptocompare API
compare_url = 'https://min-api.cryptocompare.com/data/price?fsym='
usd = requests.get(url=compare_url + coin_name + '&tsyms=USD')
try:
returner = float(usd.json()['USD'])
except KeyError:
print("failed cryptocompare API with coin: " + coin_name)
return 0
return returner
def __init__(self, list_coins_exch, ask_price=None):
dic_pd = {}
max_length = 0
# np.array(details_np)
# build a pandas dataframe from list_coins_exch information
for exch_element in list_coins_exch:
details_np = []
for key, value in exch_element.items():
if len(value) > max_length:
max_length = len(value)
for k, v in value.items():
if ask_price:
p = self.cryptocompare_api_call(k)
details_np.append(str(k + ' vol: ' + v + ' price: ' + str(p * float(v))))
else:
details_np.append(str(k + ': ' + v))
dic_pd.update({key: details_np})
# extend arrays to longest array length for pandas Dataframe conformity
dic_copy = copy.copy(dic_pd)
dic_pd = {}
for key, value in dic_copy.items():
if len(value) < max_length:
rest = max_length-len(value)
b = ['' for i in range(0, rest)]
value.extend(b)
dic_pd.update({key: value})
else:
dic_pd.update({key: value})
df = pd.DataFrame(dic_pd)
# get datetime
now = datetime.now()
# save information to an excel file with datetime as the name
writer = ExcelWriter('./csv/' + str(now) + '.xlsx')
df.to_excel(writer, 'Sheet1', index=False)
writer.save()