Skip to content

Instantly share code, notes, and snippets.

@dreizehnutters
Last active November 5, 2021 13:46
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dreizehnutters/c3a16bd23b715745ace7045719afe92e to your computer and use it in GitHub Desktop.
Save dreizehnutters/c3a16bd23b715745ace7045719afe92e to your computer and use it in GitHub Desktop.
p5.js webeditor scrapper
"""
what:
download all sketches (as .zip) from a given user on p5.js
how:
python <TARGET> [<OUT_DIR>]
who:
@13utters
"""
from sys import argv, exit
from pathlib import Path
from requests import get
try:
TARGET = argv[1]
except IndexError:
exit(f"python {argv[0]} <TARGET> [<OUT_DIR>]")
try:
BASE = argv[2]
except IndexError:
BASE = argv[1]
finally:
Path(BASE).mkdir(parents=True, exist_ok=True)
CHUNK_SIZE = 1024
PROTO = "https"
HOST = "editor.p5js.org"
ENDPOINT = lambda x : f"/editor/projects/{x}/zip"
BANNER = f"[*] scraping \x1b[1m{TARGET}\x1b[0m's sketches from {HOST}"
print(f"{BANNER}\n{'-'*(len(BANNER)-7)}")
try:
RESPONSE = get(f"{PROTO}://{HOST}/editor/{TARGET}/projects")
except TypeError as err:
exit(f"[!] {TARGET} does not exist", err)
else:
JSONRESP = RESPONSE.json()
for idx, elm in enumerate(JSONRESP):
sketch_name = elm['name'].replace(' ', '_').replace('/','_')
sketch_id = elm['_id']
print(f"\t[{idx+1}/{len(JSONRESP)}]\tscraping: \x1b[1m{sketch_name}\x1b[0m ({sketch_id})")
print('\t...', end='\r')
with open(f"{BASE}/{sketch_name}.zip", 'wb') as fd:
zip_data = get(f"{PROTO}://{HOST}{ENDPOINT(sketch_id)}")
for chunk in zip_data.iter_content(chunk_size=CHUNK_SIZE):
fd.write(chunk)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment