2019-09-19 00:10:12 +02:00
|
|
|
#!/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
|
2019-09-19 01:00:54 +02:00
|
|
|
ftype = "xml" if data.strip() else "yaml"
|
|
|
|
with open(f"cards/{card_id}.{ftype}", "w") as f:
|
2019-09-19 00:10:12 +02:00
|
|
|
f.write(data + "\n")
|
2019-09-19 01:00:54 +02:00
|
|
|
print(f"./cards/{card_id}.{ftype} written!")
|
2019-09-19 00:10:12 +02:00
|
|
|
|
|
|
|
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()
|
2019-09-19 02:43:21 +02:00
|
|
|
xml_data = f"<ability_card><yaml_data>\n{yaml_data.strip()}\n</yaml_data></ability_card>"
|
2019-09-19 00:10:12 +02:00
|
|
|
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()
|