-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
188 lines (156 loc) · 6.27 KB
/
main.py
File metadata and controls
188 lines (156 loc) · 6.27 KB
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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
# Start2Impact: Python project
# Cryptocurrency reporting system
import json
import os
from password import COINMARKETCAP_API_KEY
import requests
import time
class CoinmarketcapHandler:
""" Coinmarketcap APIs handler. Connect and fetch data from Coinmarketcap APIs """
def __init__(self):
self.url = ''
self.parameters = {}
self.headers = {}
def fetch_currencies_data(self):
# Get and return data via Coinmarketcap APIs
response = requests.get(url=self.url, headers=self.headers, params=self.parameters).json()
return response['data']
class CryptoReport(CoinmarketcapHandler):
""" Cryptocurrencies reporting system.
Generate 6 types of reports about cryptocurrencies using Coinmarketcap APIs """
def __init__(self):
super(CryptoReport, self).__init__()
self.url = 'https://pro-api.coinmarketcap.com/v1/cryptocurrency/listings/latest'
self.headers = {
'Accepts': 'application/json',
'X-CMC_PRO_API_KEY': COINMARKETCAP_API_KEY,
}
self.reports = self.get_reports()
def get_reports(self):
# Return a dict with 6 types of reports about cryptocurrencies
reports = {
'most traded': self.most_traded_currency(),
'best 10': self.best_ten_currencies(),
'worst 10': self.worst_ten_currencies(),
'amount top 20': self.amount_top_twenty_currencies(),
'amount by volumes': self.amount_by_volumes_currencies(),
'gain top 20': self.gain_top_twenty_currencies()
}
return reports
def most_traded_currency(self):
# Return the cryptocurrency with the largest volume (in $) of the last 24 hours
self.parameters = {
'start': 1,
'limit': 1,
'sort': 'volume_24h',
'sort_dir': 'desc',
'convert': 'USD'
}
currencies = self.fetch_currencies_data()
return currencies[0]
def best_ten_currencies(self):
# Return the best 10 cryptocurrencies by percentage increase in the last 24 hours
self.parameters = {
'start': 1,
'limit': 10,
'sort': 'percent_change_24h',
'sort_dir': 'desc',
'convert': 'USD'
}
currencies = self.fetch_currencies_data()
return currencies
def worst_ten_currencies(self):
# Return the worst 10 cryptocurrencies by percentage increase in the last 24 hours
self.parameters = {
'start': 1,
'limit': 10,
'sort': 'percent_change_24h',
'sort_dir': 'asc',
'convert': 'USD'
}
currencies = self.fetch_currencies_data()
return currencies
def amount_top_twenty_currencies(self):
# Return the amount of money required to purchase one unit of each of the top 20 cryptocurrencies in order of capitalization
amount = 0
self.parameters = {
'start': 1,
'limit': 20,
'sort': 'market_cap',
'sort_dir': 'desc',
'convert': 'USD'
}
currencies = self.fetch_currencies_data()
for currency in currencies:
amount += currency['quote']['USD']['price']
return round(amount, 2)
def amount_by_volumes_currencies(self):
# Return the amount of money required to purchase one unit of all cryptocurrencies whose last 24-hour volume exceeds $ 76,000,000
amount = 0
self.parameters = {
'start': 1,
'limit': 100,
'volume_24h_min': 76000000,
'convert': 'USD'
}
currencies = self.fetch_currencies_data()
for currency in currencies:
amount += currency['quote']['USD']['price']
return round(amount, 2)
def gain_top_twenty_currencies(self):
# Return the percentage of gain or loss you would have made if you had bought one unit of each of the top 20 cryptocurrencies the day before
initial_amount = 0
final_amount = 0
self.parameters = {
'start': 1,
'limit': 20,
'sort': 'market_cap',
'sort_dir': 'desc',
'convert': 'USD'
}
currencies = self.fetch_currencies_data()
for currency in currencies:
old_price = currency['quote']['USD']['price'] / (1 + (currency['quote']['USD']['percent_change_24h'] / 100))
initial_amount += old_price
final_amount += currency['quote']['USD']['price']
gain = round((((final_amount - initial_amount) / initial_amount) * 100), 1)
return gain
def make_json(report):
# Create a json file named with the actual date into the 'Report' directory
file_name = time.strftime('Report_%d_%m_%Y.json', time.localtime())
script_dir = os.path.dirname(os.path.abspath(__file__))
destination_dir = os.path.join(script_dir, 'report')
path = os.path.join(destination_dir, file_name)
# Create new directory if does not already exists
try:
os.mkdir(destination_dir)
except OSError:
pass # Already exists
with open(path, 'w') as f:
json.dump(report, f)
def main():
seconds = 60
minutes = 60
hours = 24
while True:
report = CryptoReport()
# Display essential reports
print('------------------------------------------------------------')
print('Crypto currencies reports of ' + time.strftime('%d/%m/%Y', time.localtime()))
print('Most traded: ' + str(report.reports['most traded']['symbol']))
print('Best 10:', end=' ')
for currency in report.reports['best 10']:
print(str(currency['symbol']), end=' ')
print('')
print('Worst 10:', end=' ')
for currency in report.reports['worst 10']:
print(str(currency['symbol']), end=' ')
print('')
print('Amount top 20: ' + str(report.reports['amount top 20']) + '$')
print('Amount by volumes: ' + str(report.reports['amount by volumes']) + '$')
print('Gain top 20: ' + str(report.reports['gain top 20']) + '%')
print('------------------------------------------------------------')
make_json(report.reports)
time.sleep(seconds * minutes * hours)
if __name__ == '__main__':
main()