Skip to content

Instantly share code, notes, and snippets.

@ydm
Created November 17, 2023 12:04
Show Gist options
  • Select an option

  • Save ydm/bff4f7ba09bd9d1e8f4a44d5edfdb777 to your computer and use it in GitHub Desktop.

Select an option

Save ydm/bff4f7ba09bd9d1e8f4a44d5edfdb777 to your computer and use it in GitHub Desktop.
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"cell_type": "code",
"execution_count": 2,
"id": "b0c0c6c6-ebd3-4d5e-a71c-a28b4c15d6f9",
"metadata": {},
"outputs": [],
"source": [
"from typing import Optional\n",
"import requests\n",
"\n",
"# https://beaconcha.in/api/v1/validator/{pubkey}/withdrawals\n",
"# https://beaconcha.in/api/v1/validator/{pubkey}/withdrawals?epoch={epoch}"
]
},
{
"cell_type": "code",
"execution_count": 8,
"id": "28e8c35c-5be1-4cb1-8fb9-ff40879a6f22",
"metadata": {},
"outputs": [],
"source": [
"def withdrawals(pubkey: str, epoch: Optional[int] = None):\n",
" url = f'https://beaconcha.in/api/v1/validator/{pubkey}/withdrawals'\n",
" if epoch is not None:\n",
" url = f'{url}?epoch={epoch}'\n",
" return requests.get(url, headers={\n",
" # 'apikey': '',\n",
" }).json()"
]
},
{
"cell_type": "code",
"execution_count": 13,
"id": "9f98f78f-1b78-46c7-91df-ee2f8b959149",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"{'status': 'OK', 'data': []}"
]
},
"execution_count": 13,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"withdrawals('814b000c5eea8e9f66c9aaafdc13e399678bfab19c99d56667937a25e6bc4b33d421930e4574e645092ce93ab810ec65')"
]
},
{
"cell_type": "markdown",
"id": "753a9657-ab34-439a-b5d5-5d4cdb94b02e",
"metadata": {},
"source": [
"Well... the API doesn't work, so let's scrape..."
]
},
{
"cell_type": "code",
"execution_count": 30,
"id": "c1681832-7a31-46d7-8096-7c7a117f1713",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"[('class', 'timestamp'), ('data-toggle', 'tooltip'), ('data-placement', 'top'), ('data-timestamp', '1667663399')]\n",
"[('class', 'timestamp'), ('data-toggle', 'tooltip'), ('data-placement', 'top'), ('data-timestamp', '1667711147')]\n"
]
}
],
"source": [
"from html.parser import HTMLParser\n",
"\n",
"class MyParser(HTMLParser):\n",
" def handle_starttag(self, tag, attrs):\n",
" if tag == 'span':\n",
" if dict(attrs).get('class') == 'timestamp':\n",
" print(attrs)\n",
"\n",
" def handle_endtag(self, tag):\n",
" pass\n",
"\n",
" def handle_data(self, data):\n",
" pass\n",
"\n",
"resp = requests.get('https://beaconcha.in/validator/814b000c5eea8e9f66c9aaafdc13e399678bfab19c99d56667937a25e6bc4b33d421930e4574e645092ce93ab810ec65#withdrawals')\n",
"parser = MyParser()\n",
"parser.feed(resp.content.decode('utf-8'))"
]
},
{
"cell_type": "markdown",
"id": "90a3785c-e81a-4b21-956e-120fb3c4924b",
"metadata": {},
"source": [
"Withdrawals are loaded dynamically, let's use Selenium..."
]
},
{
"cell_type": "code",
"execution_count": 45,
"id": "96b3b2dd-78d8-49b9-9cd9-7b9aa387d57b",
"metadata": {},
"outputs": [],
"source": [
"import time\n",
"from datetime import datetime\n",
"\n",
"from selenium import webdriver"
]
},
{
"cell_type": "code",
"execution_count": 33,
"id": "aa448486-d979-4b44-8856-94545850fcb8",
"metadata": {},
"outputs": [],
"source": [
"opts = webdriver.FirefoxOptions()\n",
"opts.headless = True\n",
"driver = webdriver.Firefox(options=opts)"
]
},
{
"cell_type": "code",
"execution_count": 63,
"id": "6b72868e-df54-4307-b50e-b363b2cae730",
"metadata": {},
"outputs": [],
"source": [
"def estimation(pubkey: str) -> int:\n",
" driver.get(f'https://beaconcha.in/validator/{pubkey}#withdrawals')\n",
" time.sleep(10) # Let it load.\n",
" xs = driver.find_elements('css selector', '.timestamp')\n",
" ts = max(map(int, (x.get_attribute('data-timestamp') for x in xs)))\n",
" return ts - time.time()"
]
},
{
"cell_type": "code",
"execution_count": 64,
"id": "5d17823c-7f4f-4ad2-bdb9-7b47f5629a94",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"1237.6633534431458"
]
},
"execution_count": 64,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"estimation('814b000c5eea8e9f66c9aaafdc13e399678bfab19c99d56667937a25e6bc4b33d421930e4574e645092ce93ab810ec65')"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3 (ipykernel)",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.10.10"
}
},
"nbformat": 4,
"nbformat_minor": 5
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment