-
Notifications
You must be signed in to change notification settings - Fork 21
Expand file tree
/
Copy pathglassnode.py
More file actions
56 lines (45 loc) · 1.1 KB
/
glassnode.py
File metadata and controls
56 lines (45 loc) · 1.1 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
import json
import requests
import datetime
import iso8601
import pandas as pd
class GlassnodeClient:
def __init__(self):
self._api_key = ''
@property
def api_key(self):
return self._api_key
def set_api_key(self, value):
self._api_key = value
def get(self, url, a='BTC', i='24h', c='native', s=None, u=None):
p = dict()
p['a'] = a
p['i'] = i
p['c'] = c
if s is not None:
try:
p['s'] = iso8601.parse_date(s).strftime('%s')
except ParseError:
p['s'] = s
if u is not None:
try:
p['u'] = iso8601.parse_date(u).strftime('%s')
except ParseError:
p['u'] = s
p['api_key'] = self.api_key
r = requests.get(url, params=p)
try:
r.raise_for_status()
except Exception as e:
print(e)
print(r.text)
try:
df = pd.DataFrame(json.loads(r.text))
df = df.set_index('t')
df.index = pd.to_datetime(df.index, unit='s')
df = df.sort_index()
s = df.v
s.name = '_'.join(url.split('/')[-2:])
return s
except Exception as e:
print(e)