forked from dfkthf125/api-v1-client-python
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstatistics.py
More file actions
105 lines (85 loc) · 3.46 KB
/
statistics.py
File metadata and controls
105 lines (85 loc) · 3.46 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
"""This module corresponds to functionality documented
at https://blockchain.info/api/charts_api
"""
from . import util
import json
def get(api_code=None):
"""Get network statistics.
:param str api_code: Blockchain.info API code (optional)
:return: an instance of :class:`Stats` class
"""
resource = 'stats?format=json'
if api_code is not None:
resource += '&api_code=' + api_code
response = util.call_api(resource)
json_response = json.loads(response)
return Stats(json_response)
def get_chart(chart_type, time_span=None, rolling_average=None, api_code=None):
"""Get chart data of a specific chart type.
:param str chart_type: type of chart
:param str time_span: duration of the chart.
Default is 1 year for most charts, 1 week for mempool charts (optional)
(Example: 5weeks)
:param str rolling_average: duration over which the data should be averaged (optional)
:param str api_code: Blockchain.info API code (optional)
:return: an instance of :class:`Chart` class
"""
resource = 'charts/' + chart_type + '?format=json'
if time_span is not None:
resource += '×pan=' + time_span
if rolling_average is not None:
resource += '&rollingAverage=' + rolling_average
if api_code is not None:
resource += '&api_code=' + api_code
response = util.call_api(resource)
json_response = json.loads(response)
return Chart(json_response)
def get_pools(time_span=None, api_code=None):
"""Get number of blocks mined by each pool.
:param str time_span: duration of the chart.
Default is 4days (optional)
:param str api_code: Blockchain.info API code (optional)
:return: an instance of dict:{str,int}
"""
resource = 'pools'
if time_span is not None:
resource += '?timespan=' + time_span
if api_code is not None:
resource += '&api_code=' + api_code
response = util.call_api(resource, base_url='https://api.blockchain.info/')
json_response = json.loads(response)
return {k: v for (k, v) in json_response.items()}
class Stats:
def __init__(self, s):
self.trade_volume_btc = s['trade_volume_btc']
self.miners_revenue_usd = s['miners_revenue_usd']
self.btc_mined = s['n_btc_mined']
self.trade_volume_usd = s['trade_volume_usd']
self.difficulty = s['difficulty']
self.minutes_between_blocks = s['minutes_between_blocks']
self.number_of_transactions = s['n_tx']
self.hash_rate = s['hash_rate']
self.timestamp = s['timestamp']
self.mined_blocks = s['n_blocks_mined']
self.blocks_size = s['blocks_size']
self.total_fees_btc = s['total_fees_btc']
self.total_btc_sent = s['total_btc_sent']
self.estimated_btc_sent = s['estimated_btc_sent']
self.total_btc = s['totalbc']
self.total_blocks = s['n_blocks_total']
self.next_retarget = s['nextretarget']
self.estimated_transaction_volume_usd = s['estimated_transaction_volume_usd']
self.miners_revenue_btc = s['miners_revenue_btc']
self.market_price_usd = s['market_price_usd']
class Chart:
def __init__(self, c):
self.status = c['status']
self.name = c['name']
self.unit = c['unit']
self.period = c['period']
self.description = c['description']
self.values = [Point(point) for point in c['values']]
class Point:
def __init__(self, p):
self.x = p['x']
self.y = p['y']