Skip to content

Instantly share code, notes, and snippets.

@aaronpenne
Created June 23, 2021 16:29
Show Gist options
  • Save aaronpenne/d91a66b543d8114d3a509e6020031851 to your computer and use it in GitHub Desktop.
Save aaronpenne/d91a66b543d8114d3a509e6020031851 to your computer and use it in GitHub Desktop.
Python 3 script to parse hash, token, and features for a given ArtBlocks project
#
# Python 3 script to parse hash, token, and features for a given ArtBlocks project
# @aaronpenne
# MIT License
#
# Usage:
# python get_features.py -s 77000000 -e 77000299
#
import sys
from datetime import datetime
import logging
import requests
import csv
import time
import argparse
# Set up logging
logging.basicConfig(
level=logging.INFO,
stream=sys.stdout,
format="%(asctime)s - %(levelname)s - %(message)s",
datefmt="%Y-%m-%d %H:%M:%S",
)
log = logging.getLogger(__name__)
# Take input from command line
parser = argparse.ArgumentParser(
formatter_class=argparse.RawDescriptionHelpFormatter,
description="Get features for a range of token IDs.",
usage="python get_features.py -s 77000000 -e 77000299",
)
parser.add_argument(
"-s", "--start", type=int, default=77000000, help="token ID to start on"
)
parser.add_argument(
"-e", "--end", type=int, default=77000299, help="token ID to end on"
)
args = parser.parse_args()
# Always smart to have a timestamp
timestamp = datetime.now().strftime("%Y-%m-%d %H:%M:%S")
output = []
# FIXME for now hardcode the token_ids
for cnt, token_id in enumerate(range(args.start, args.end)):
# Be kind to the ArtBlocks server and pause a bit
time.sleep(0.1)
log.info("Getting token_id {}".format(token_id))
# Grab data from ArtBlocks API, info here: https://api.artblocks.io/
url = "https://api.artblocks.io/token/{}".format(token_id)
try:
response = requests.request("GET", url).json()
except:
log.error("Failed to retrieve token_id {}".format(token_id))
time.sleep(0.1) # slight pause to catch any ctrl-c
continue
# Parse relevant response
token_id = response["tokenID"]
token_hash = response["token hash"]
features = response["features"]
# FIXME more values can be grabbed here, be sure to modify the header_row accordingly
# Create header row once with lowercase strings
if cnt == 0:
header_row = [
"timestamp" "token_id",
"token_hash",
]
for f in features:
header_row.append(f.split(":")[0].strip().lower())
output.append(header_row)
# Gather relevant data for this token
token_row = [
timestamp,
token_id,
token_hash,
]
for f in features:
token_row.append(f.split(":")[0].strip())
# Add "row" to list of lists
output.append(token_row)
# Dump to a CSV (better to put this in sqlite3, etc)
with open("ab_features.csv", "w", newline="") as f:
writer = csv.writer(f)
writer.writerows(output)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment