Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Download Card Conjurer from Captured HAR
# Use chrome to capture a har file of the page load and save it as data/cardconjurer.com.har
# To run do cd data/local_site; python3 -m http.server
import base64
import json
import os
import pathlib
import re
import requests
import time
HAR_PATH = 'data/cardconjurer.com.har'
OUT_DIR = 'data/local_site/'
IMAGE_URL_ROOT = "https://card-conjurer.storage.googleapis.com"
DOWNLOAD_RATE_LIMIT = 0
def url_to_local_path(url):
path = '/'.join(url.split('://')[1].split('/')[1:])
if path.endswith('/'):
path = 'index.html'
# print(path)
return OUT_DIR + path
def get_if_missing(url):
out_path = url_to_local_path(url)
pathlib.Path(out_path).parent.mkdir(parents=True, exist_ok=True)
if not os.path.exists(out_path):
print(f'Downloading: {url}')
resp = requests.get(url, timeout=3)
with open(out_path, 'wb') as f_out:
f_out.write(resp.content)
time.sleep(DOWNLOAD_RATE_LIMIT)
return out_path
def unpack_har():
with open(HAR_PATH, 'r') as fd:
data = json.load(fd)
for entry in data['log']['entries']:
req = entry['request']
url = req['url']
content = entry['response']['content']
#print(content['size'])
# Fonts not included in HAR
if content['mimeType'] == 'application/x-font-ttf':
get_if_missing(url)
continue
out_path = url_to_local_path(url)
pathlib.Path(out_path).parent.mkdir(parents=True, exist_ok=True)
with open(out_path, 'wb') as f_out:
if 'encoding' in content:
assert(content['encoding'] == 'base64')
decoded = base64.b64decode(content['text'])
f_out.write(decoded)
else:
f_out.write(content['text'].encode('utf-8'))
# The frames for cards are seperated into groups:
groups = ['Standard-3','Token-2','Saga-1','Planeswalker','Modal-1','DFC','Showcase-5','Promo-2','Textless-4','Custom','Misc-2','Margin','FleshAndBlood']
def fetach_images(text):
images_paths = re.findall(r'(/[^, ]+\.(png|svg))', text)
for image_path in images_paths:
image_path = image_path[0]
get_if_missing(IMAGE_URL_ROOT + image_path)
thumb_path = image_path[:-4]+ 'Thumb.png'
get_if_missing(IMAGE_URL_ROOT + thumb_path)
def fetch_style(style):
url = f'https://cardconjurer.com/js/frames/pack{style}.js'
style_file = get_if_missing(url)
with open(style_file, 'r') as fd:
text = fd.read()
fetach_images(text)
def fetch_groups():
for group in groups:
url = f'https://cardconjurer.com/js/frames/group{group}.js'
group_file = get_if_missing(url)
with open(group_file, 'r') as fd:
text = fd.read()
styles = re.findall(r"value:'(.+)'", text)
for style in styles:
if style == 'disabled':
continue
fetch_style(style)
def fetch_plainswalkers():
plainswalker_file = OUT_DIR + '/js/frames/versionPlaneswalker.js'
with open(plainswalker_file, 'r') as fd:
text = fd.read()
fetach_images(text)\
unpack_har()
fetch_groups()
fetch_plainswalkers()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment