/
getTokenDayDatas.py
68 lines (63 loc) · 1.7 KB
/
getTokenDayDatas.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
import requests, json, sys, csv, os, datetime
from utils import toUnixtime, toString
# make data directory
if not os.path.exists('data'):
os.mkdir('data')
# get system var
token_symbol = sys.argv[1]
start_date = toUnixtime(sys.argv[2])
end_date = toUnixtime(sys.argv[3])
# make api call
url = "https://api.thegraph.com/subgraphs/name/uniswap/uniswap-v2"
query = """{
tokens(
orderBy: txCount
orderDirection:desc
first:1
where: {
symbol:"%s"
}
) {
id
name
}
}
""" %(token_symbol)
r = requests.post(url, json={'query': query})
json_data = json.loads(r.text)
token_id = json_data.get('data').get('tokens')[0].get('id')
token_name = json_data.get('data').get('tokens')[0].get('name')
print(token_id)
print(token_name)
query = """{
tokenDayDatas(
where: {
token: "%s"
date_gte: %s
date_lte: %s
}
) {
id
date
dailyVolumeETH
dailyVolumeUSD
dailyVolumeToken
priceUSD
}
}
""" %(token_id, start_date, end_date)
r = requests.post(url, json={'query': query})
json_data = json.loads(r.text)
# write data file
filename = 'data/TokenDayData_{}_{}.csv'.format(
token_symbol,
datetime.datetime.now().strftime('%Y.%m.%d_%H:%M:%S')
)
with open(filename, mode='w') as csv_file:
csv_writer = csv.writer(csv_file)
csv_writer.writerow(['날짜', '토큰', '거래량(ETH)', '거래량(USD)', '거래량(token)','USD가격'])
for data in json_data.get('data').get('tokenDayDatas'):
csv_writer.writerow([
toString(data.get('date')), token_name,
data.get('dailyVolumeETH'), data.get('dailyVolumeUSD'), data.get('dailyVolumeToken'),data.get('priceUSD')
])