-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlogger.py
121 lines (102 loc) · 4.05 KB
/
logger.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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
# This file is for reading and writing to the csv data files
import csv
import os
from datetime import datetime
class Logger():
def __init__(self):
self.ticker_history_csv = "./data/ticker_history.csv"
self.tickers_columnheaders = ["coin", "at", "buy", "sell", "low", "high", "last","vol", "coinUSDprice", "askpricePerCoin", "bidpricePerCoin", "exchange", "datetime"]
def create_csv(self, csvfile):
if os.path.isfile(csvfile):
pass
else:
try:
if csvfile == self.ticker_history_csv:
with open(csvfile, 'w', newline='') as file:
fwriter = csv.writer(file, delimiter=',', quotechar='|', quoting=csv.QUOTE_MINIMAL)
fwriter.writerow(self.tickers_columnheaders)
except IOError:
print(f"Error: Unable to create {csvfile}")
def write_to_csv(self, csvfile, payload):
try:
with open(csvfile, 'a', newline='') as file:
fwriter = csv.writer(file, delimiter=',', quotechar='|', quoting=csv.QUOTE_MINIMAL)
fwriter.writerow(payload)
except IOError:
print(f"Error: Unable to write to {csvfile}")
def get_lowest_price(self, csvfile):
price_list = []
coin = ""
try:
with open(csvfile) as f:
contents_of_f = csv.reader(f)
for row in contents_of_f:
#price_list.append(float(row[9]))
price_list.append(row[9])
lowest = min(price_list)
except Exception as e:
print(f"{e}")
try:
with open(csvfile) as f:
contents_of_f = csv.reader(f)
# get the coin sysmbol for the lowest price
for row in contents_of_f:
if row[9] == lowest:
coin = row[0]
except Exception as e:
print(f"{e}")
return coin, lowest
def get_highest_price(self, csvfile):
price_list = []
coin = ""
try:
with open(csvfile) as f:
contents_of_f = csv.reader(f)
for row in contents_of_f:
#price_list.append(float(row[9]))
price_list.append(row[10])
highest = max(price_list)
except Exception as e:
print(f"{e}")
try:
with open(csvfile) as f:
contents_of_f = csv.reader(f)
# get the coin sysmbol for the lowest price
for row in contents_of_f:
if row[10] == highest:
coin = row[0]
except Exception as e:
print(f"{e}")
return coin, highest
def get_exchange_details(self, csvfile, exchange):
priceSummaryList = []
try:
with open(csvfile) as f:
contents_of_f = csv.reader(f)
for row in contents_of_f:
clenght = len(row[0])
each_pair = row[0]
c = each_pair[clenght:]
c = c.upper()
summary = f"""****{row[0]} as of {row[12]}****
Lowest sale price on the {exchange}: {row[3]} {c}
Highest buy price on the {exchange}: {row[2]} {c}
CMC price for this coin in USD was : {row[8]} USD
Sale price per coin on {exchange} is approximately : {row[9]} USD
Buy price per coin on {exchange} is approximately : {row[10]} USD\n"""
priceSummaryList.append(summary)
return priceSummaryList
except Exception as e:
print(f"{e}")
if __name__ == '__main__':
log = Logger()
timestamp = datetime.now()
#ticker, lp = log.get_lowest_price("./data/tickers.csv")
#print(f"{ticker}: {lp}")
csvfile = './data/cpatextickers.csv'
#c, h = log.get_highest_price(csvfile)
#print(c, h)
cpatexlist = log.get_exchange_details(csvfile, "C-Patex")
for each in cpatexlist:
print(each)
#print(cpatexlist)