Skip to content

Instantly share code, notes, and snippets.

@trsqxyz
Created June 4, 2017 15:20
Show Gist options
  • Save trsqxyz/928fe4e9dbd27dd9a0488e59b7b72480 to your computer and use it in GitHub Desktop.
Save trsqxyz/928fe4e9dbd27dd9a0488e59b7b72480 to your computer and use it in GitHub Desktop.
#!/usr/bin/env python3
import os
import json
import requests
import base64
import click
from hearthstone.deckstrings import Deck
db_url = "https://api.hearthstonejson.com/v1/19506/jaJP/cards.collectible.json"
db_path = "./db.json"
if os.path.exists(db_path):
with open(db_path, "r") as f:
db = json.loads(f.read())
else:
response = requests.get(db_url)
if response.status_code == 200:
with open(db_path, "w") as f:
f.write(response.text)
db = response.json()
else:
raise RuntimeError("Couldn't download cards database: %s"
% response.text)
@click.group()
def hsd():
return
def get_carddata(id):
for data in db:
if data["dbfId"] == id:
return data
def get_cardinfo(id, want):
data = get_carddata(id)
return data[str(want)]
def write_deck(deck):
hero = deck.heroes
format = deck.format
cards = deck.cards
print(hero)
print(format)
for card, count in cards:
print(
str(count) + "x",
get_cardinfo(card, "name"),
"(" + str(get_cardinfo(card, "cost")) + ")"
)
print(deck.as_deckstring)
return
@hsd.command()
@click.argument("code", type=str)
def decode(code):
deck = Deck.from_deckstring(code)
return write_deck(deck)
if __name__ == "__main__":
hsd()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment