-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathcryptocoinmarket.py
More file actions
91 lines (70 loc) · 2.6 KB
/
cryptocoinmarket.py
File metadata and controls
91 lines (70 loc) · 2.6 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
# -*- coding: utf-8 -*-
"""
CryptoCoinBot is an open source project developed by @eniolw.
<https://github.com/eniolw>
It is distributed under the GNU Affero General Public License 3.0
<https://fsf.org/>
"""
import coinmarketcap
class CrytoCoinData(object):
"""
A model for the incoming data from cryptocoin.com API
"""
def __init__(self,
id,
name,
symbol,
price_usd,
price_btc,
rank,
volume_usd_24h,
percent_change_24h,
available_supply,
**kwargs):
self.id = id.encode("utf-8")
self.name = name.encode("utf-8")
self.symbol = symbol.encode("utf-8")
self.price_usd = float(price_usd)
self.price_btc = float(price_btc)
self.rank = int(rank)
self.volume_usd_24h = float(volume_usd_24h)
self.percent_change_24h = float(percent_change_24h)
self.available_supply = float(available_supply)
def __str__(self):
return "%s %s %s %s %s %s %s %s %s" % (self.id, self.name, self.symbol,
self.price_usd, self.price_btc,
self.rank, self.volume_usd_24h,
self.percent_change_24h,
self.available_supply)
class CryptoCoinMarketError(Exception):
pass
class CryptoCoinMarket(object):
"""
Wrapper to retrieve data using the coinmarketcap module but
converting it into a appropiate model
"""
ERROR = CryptoCoinMarketError
def ticker(currency=None):
"""
Retrieves data from coinmarketcap.com API using the
coinmarketcap module and converts it into a CryptoCoinData
instance. Returns an arrange of CryptoCoinData
"""
try:
results = coinmarketcap.Market().ticker()
except:
raise self.ERROR
return
data = [CrytoCoinData(res.get("id"),
res.get("name"),
res.get("symbol"),
res.get("price_usd"),
res.get("price_btc"),
res.get("rank"),
res.get("24h_volume_usd"),
res.get("percent_change_24h"),
res.get("available_supply"))
for res in results]
return data
# Instance to be imported:
market = CryptoCoinMarket()