Skip to content

Commit c4ff38c

Browse files
authored
Merge pull request #5 from dhruvan2006/the-block
Add support for The Block
2 parents f105535 + 2d8c019 commit c4ff38c

File tree

6 files changed

+54
-0
lines changed

6 files changed

+54
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ handles the heavy lifting so you can focus on insights.
3535
- [Bitcoin Magazine Pro](https://www.bitcoinmagazinepro.com)
3636
- [Blockchain.com](https://www.blockchain.com/explorer/charts)
3737
- [Glassnode](https://studio.glassnode.com/charts/)
38+
- [The Block](https://www.theblock.co/data/)
3839

3940
## Installation
4041
To install the `chaindl` package, use pip:

chaindl/download.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ def download(url, start=None, end=None, **kwargs):
4444
BITCOINMAGAZINEPRO_BASE_URL = "https://www.bitcoinmagazinepro.com"
4545
BLOCKCHAIN_BASE_URL = "https://www.blockchain.com/explorer/charts"
4646
GLASSNODE_BASE_URL = "https://studio.glassnode.com/charts"
47+
THEBLOCK_BASE_URL = "https://www.theblock.co"
4748

4849
data = pd.DataFrame()
4950

@@ -63,6 +64,8 @@ def download(url, start=None, end=None, **kwargs):
6364
data = scraper.blockchain._download(url, **kwargs)
6465
elif url.startswith(GLASSNODE_BASE_URL):
6566
data = scraper.glassnode._download(url, **kwargs)
67+
elif url.startswith(THEBLOCK_BASE_URL):
68+
data = scraper.theblock._download(url)
6669
else:
6770
raise ValueError("Unsupported source. Find the list of supported websites here: https://chaindl.readthedocs.io/")
6871

chaindl/scraper/__init__.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,3 +6,4 @@
66
from .bitcoinmagazinepro import _download
77
from .blockchain import _download
88
from .glassnode import _download
9+
from .theblock import _download

chaindl/scraper/theblock.py

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
import json
2+
import requests
3+
import pandas as pd
4+
from urllib.parse import urlparse
5+
6+
def _download(url):
7+
parsed = urlparse(url)
8+
parts = [p for p in parsed.path.split("/") if p]
9+
last_three = "/".join(parts[-3:])
10+
11+
api = f"https://www.theblock.co/api/charts/chart/{last_three}"
12+
response = requests.get(api)
13+
response.raise_for_status()
14+
_json = response.json()
15+
jsonFile = _json.get("jsonFile", {}).get("data", {})
16+
jsonFile = json.loads(jsonFile)
17+
18+
df = pd.concat(
19+
{
20+
series: pd.DataFrame(data["Data"]).set_index("Timestamp")["Result"]
21+
for series, data in jsonFile["Series"].items()
22+
},
23+
axis=1
24+
)
25+
df.index = pd.to_datetime(df.index, unit="s")
26+
df.index.name = "Date"
27+
28+
return df

docs/index.rst

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ It supports:
3333
- `Bitcoin Magazine Pro <https://www.bitcoinmagazinepro.com>`__
3434
- `Blockchain.com <https://www.blockchain.com/explorer/charts/>`__
3535
- `Glassnode <https://studio.glassnode.com/charts/>`__
36+
- `The Block <https://www.theblock.co/data/>`__
3637

3738
---
3839

@@ -191,6 +192,18 @@ Example:
191192
url = "https://studio.glassnode.com/charts/addresses.ActiveCount?a=BTC"
192193
df = chaindl.download(url)
193194
195+
The BLock (`theblock.co/data <https://www.theblock.co/data/>`__)
196+
---------------------------------------------------------------------------
197+
198+
Click 'Share' and 'Copy Link' to get the URL of the respective metric.
199+
200+
Example:
201+
202+
.. code-block:: python
203+
204+
url = "https://www.theblock.co/data/crypto-markets/spot/total-exchange-volume-daily"
205+
df = chaindl.download(url)
206+
194207
Optional Arguments
195208
==================
196209

tests/test_theblock.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
from chaindl.scraper.theblock import _download
2+
3+
def test_transactions_on_the_bitcoin_network():
4+
url = "https://www.theblock.co/data/on-chain-metrics/bitcoin/transactions-on-the-bitcoin-network-daily"
5+
df = _download(url)
6+
assert "7DMA" in df.columns
7+
assert "Date" == df.index.name
8+
assert not df.empty

0 commit comments

Comments
 (0)