add a card_syncer for local play
This commit is contained in:
parent
c035af41a0
commit
0c2346bf59
|
@ -1 +1,2 @@
|
|||
build/*
|
||||
build/
|
||||
cards/build/
|
||||
|
|
|
@ -0,0 +1,122 @@
|
|||
#!/usr/bin/env python3
|
||||
from pprint import pprint
|
||||
from requests.auth import HTTPBasicAuth
|
||||
import json
|
||||
import os
|
||||
import requests
|
||||
import xmltodict
|
||||
|
||||
MY_COLLECTION = "Fjompens fjomperi"
|
||||
MY_ID = "26"
|
||||
AUTH = None
|
||||
CARD_STYLE = 6 # Fjompens's card style
|
||||
|
||||
def get_card_ids():
|
||||
resp = requests.get("http://pvv.org/~andreasd/cards/command.php?cmd=get_all_cards", auth=AUTH)
|
||||
for collection in resp.json():
|
||||
if collection["name"] == MY_COLLECTION and collection["id"] == MY_ID:
|
||||
return collection["cards"]
|
||||
|
||||
def get_card_xml(card_id:int):
|
||||
resp = requests.get(f"http://pvv.org/~andreasd/cards/command.php?cmd=get_card_xml&id={card_id}", auth=AUTH)
|
||||
return resp.text
|
||||
|
||||
def get_card_style_id(card_id:int):
|
||||
resp = requests.get(f"http://pvv.org/~andreasd/cards/command.php?cmd=get_card_style&id={card_id}", auth=AUTH)
|
||||
return resp.text
|
||||
|
||||
def set_card_xml(card_id:int, xml_data):
|
||||
resp = requests.post(f"http://pvv.org/~andreasd/cards/command.php", auth=AUTH, data={
|
||||
"cmd" : "update_card_text",
|
||||
"id" : str(card_id),
|
||||
"data" :xml_data,
|
||||
})
|
||||
if resp.ok and resp.text == "<span class='ui success'>Card Updated</span>":
|
||||
return True
|
||||
else:
|
||||
print(resp.text)
|
||||
return False
|
||||
|
||||
def set_card_style_id(card_id:int, style_id:int):
|
||||
resp = requests.post(f"http://pvv.org/~andreasd/cards/command.php", auth=AUTH, data={
|
||||
"cmd" : "update_card_style",
|
||||
"id" : str(card_id),
|
||||
"style" : str(style_id),
|
||||
})
|
||||
if resp.ok and resp.text == "<span class='ui success'>Card Updated</span>": # lol
|
||||
return True
|
||||
else:
|
||||
print(resp.text)
|
||||
return False
|
||||
|
||||
|
||||
def pull_all():
|
||||
card_ids = get_card_ids()
|
||||
for card_id in card_ids:
|
||||
print(f"pulling card {card_id}... ", end="")
|
||||
|
||||
data = get_card_xml(card_id)
|
||||
style_id = get_card_style_id(card_id)
|
||||
if style_id not in map(str, (0, CARD_STYLE)):
|
||||
print("Error! style id was",style_id)
|
||||
continue
|
||||
|
||||
try:
|
||||
xml = xmltodict.parse(data)
|
||||
except:
|
||||
xml = None
|
||||
|
||||
if xml and "ability_card" in xml:
|
||||
if "yaml_data" in xml["ability_card"]:
|
||||
if len(xml["ability_card"].keys()) == 1:
|
||||
yaml_data = xml["ability_card"]["yaml_data"]
|
||||
with open(f"cards/{card_id}.yaml", "w") as f:
|
||||
f.write(yaml_data + "\n")
|
||||
print(f"./cards/{card_id}.yaml written!")
|
||||
continue
|
||||
|
||||
with open(f"cards/{card_id}.xml", "w") as f:
|
||||
f.write(data + "\n")
|
||||
print(f"./cards/{card_id}.xml written!")
|
||||
|
||||
def push_all():
|
||||
existing_card_ids = get_card_ids()
|
||||
|
||||
for card_id in existing_card_ids:
|
||||
if os.path.isfile(f"cards/{card_id}.yaml"):
|
||||
fname = f"cards/{card_id}.yaml"
|
||||
with open(fname) as f:
|
||||
yaml_data = f.read()
|
||||
xml_data = f"<ability_card><yaml_data>\n{yaml_data.strip()}\n</yaml_data>\n\n\n</ability_card>"
|
||||
elif os.path.isfile(f"cards/{card_id}.xml"):
|
||||
fname = f"cards/{card_id}.xml"
|
||||
with open(fname) as f:
|
||||
xml_data = f.read()
|
||||
else:
|
||||
continue
|
||||
|
||||
if xml_data:
|
||||
print(f"POSTing {fname} to site... ", end="")
|
||||
if set_card_xml(card_id, xml_data) == True:
|
||||
print("OK!")
|
||||
else:
|
||||
print("FAILED!")
|
||||
|
||||
style_id = get_card_style_id(card_id)
|
||||
if style_id == "0":
|
||||
print("Setting card", card_id, "style to", CARD_STYLE, "...", end=" ")
|
||||
if set_card_style_id(card_id, CARD_STYLE):
|
||||
print("OK!")
|
||||
else:
|
||||
print("FAILED!")
|
||||
|
||||
if __name__ == "__main__":
|
||||
import sys
|
||||
if len(sys.argv) < 4:
|
||||
print(__file__, "<action> <uname> <passwd>\n\n\taction: either 'push' or 'pull'\n")
|
||||
elif sys.argv[1].strip().lower() == "pull":
|
||||
AUTH = HTTPBasicAuth(sys.argv[2], sys.argv[3])
|
||||
pull_all()
|
||||
elif sys.argv[1].strip().lower() == "push":
|
||||
AUTH = HTTPBasicAuth(sys.argv[2], sys.argv[3])
|
||||
push_all()
|
Loading…
Reference in New Issue