Skip to content

Instantly share code, notes, and snippets.

@trsqxyz
Last active August 9, 2017 04:08
Show Gist options
  • Save trsqxyz/0df6f6c961b378136f0df583c8d4116f to your computer and use it in GitHub Desktop.
Save trsqxyz/0df6f6c961b378136f0df583c8d4116f to your computer and use it in GitHub Desktop.
from CARD.xml
# coding: utf-8
import os
import json
import requests
import csv
import re
import codecs
'''make a CSV file.
- set name
- card name
- flavor text
- artist name
'''
db_url = "https://api.hearthstonejson.com/v1/20457/jaJP/cards.collectible.json"
db_path = "./db.json"
if os.path.exists(db_path):
with codecs.open(db_path, "r", "utf-8") as f:
db = json.loads(f.read())
else:
response = requests.get(db_url)
if response.status_code == 200:
with codecs.open(db_path, "w", "utf-8") as f:
f.write(response.text)
db = response.json()
else:
raise RuntimeError("Couldn't download cards database: %s"
% response.text)
carddata = []
for data in db:
card = []
for k in ("set", "name", "flavor", "artist"):
try:
card.append(data[k])
except (KeyError):
pass
else:
carddata.append(card)
with codecs.open('hs.csv', 'w', "utf-8") as f:
writer = csv.writer(f, lineterminator='\n')
writer.writerows(carddata)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment