-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathhashrate.py
70 lines (57 loc) · 1.73 KB
/
hashrate.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
69
70
import requests
from bs4 import BeautifulSoup
import pandas as pd
from datetime import date
import os
import lxml
url = "https://poolbay.io/crypto/54/decred"
data = requests.get(url).text
soup = BeautifulSoup(data, 'html.parser')
# Parse the html content
soup = BeautifulSoup(data, "lxml")
gdp_table = soup.find("table", attrs={"id": "pool-table"})
name = soup.find_all('td', class_='td-name')
table = soup.find( "table", {"id":"pool-table"} )
# list for pool names
listPool=[]
# populate pool names
for row in table.find_all('td', class_='td-name'):
vpool = row.find('a').contents[0]
listPool.append(vpool)
# list for hashrate and units
listHash=[]
listUnits=[]
# populate hashrate and units
for span in table.find_all('span', class_= 'mt-2'):
if span.text != "":
hashrate = span.text
rate = float(hashrate[:(hashrate.find('.')+3)])
units = hashrate[(hashrate.find('.')+3):]
# convert to Ph/s
if units == 'TH/s':
rate = rate / 1000
units = 'PH/s'
if units == 'H/s':
rate = rate / 10e15
units = 'PH/s'
vpool = row.find('a').contents[0]
listHash.append(rate)
listUnits.append(units)
# create dict
d = { 'pool':listPool, 'rate':listHash,'units':listUnits}
# create df
df = pd.DataFrame(d)
df = df.set_index('pool')
# check if path exists, if not create
if not os.path.exists('./data/hashrate'):
os.makedirs('./data/hashrate')
# get today's date for file path
todayStr = str(date.today())
yearMonthStr = date.today().strftime("%Y/%m/")
# path for raw data
pathStr = './data/hashrate/' + yearMonthStr
if not os.path.exists(pathStr):
os.makedirs(pathStr)
filename = pathStr + todayStr + '.csv'
# save to csv
df.to_csv(filename)