RPGs3_card_insanity/sync.py

162 lines
5.6 KiB
Python
Raw Normal View History

2019-09-19 00:10:12 +02:00
#!/usr/bin/env python3
from pprint import pprint
from requests.auth import HTTPBasicAuth
2019-10-14 19:34:45 +02:00
import concurrent.futures
2019-09-19 00:10:12 +02:00
import json
import os
import requests
2019-10-06 21:43:37 +02:00
import requests.packages.urllib3.util.connection as urllib3_cn
import socket
2019-09-19 00:10:12 +02:00
import xmltodict
MY_COLLECTION = "Fjompens fjomperi"
MY_ID = "26"
AUTH = None
CARD_STYLE = 6 # Fjompens's card style
2019-10-06 21:43:37 +02:00
##FORCE_IPV4 = False
##def allowed_gai_family(): # used to force either ipv4 or ipv6
## family = socket.AF_INET
## if not FORCE_IPV4 and urllib3_cn.HAS_IPV6:
## family = socket.AF_INET6 # force ipv6 only if it is available
## return family
##urllib3_cn.allowed_gai_family = allowed_gai_family
2019-09-19 00:10:12 +02:00
def get_card_ids():
2019-10-06 21:43:37 +02:00
resp = requests.get("https://www.pvv.ntnu.no/~andreasd/cards/command.php?cmd=get_all_cards", auth=AUTH)
2019-09-19 00:10:12 +02:00
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):
2019-10-06 21:43:37 +02:00
resp = requests.get(f"https://www.pvv.ntnu.no/~andreasd/cards/command.php?cmd=get_card_xml&id={card_id}", auth=AUTH)
2019-09-19 00:10:12 +02:00
return resp.text
def get_card_style_id(card_id:int):
2019-10-06 21:43:37 +02:00
resp = requests.get(f"https://www.pvv.ntnu.no/~andreasd/cards/command.php?cmd=get_card_style&id={card_id}", auth=AUTH)
2019-09-19 00:10:12 +02:00
return resp.text
def set_card_xml(card_id:int, xml_data):
2019-10-06 21:43:37 +02:00
resp = requests.post(f"https://www.pvv.ntnu.no/~andreasd/cards/command.php", auth=AUTH, data={
2019-09-19 00:10:12 +02:00
"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
2019-10-14 19:34:45 +02:00
def list_collections():
with concurrent.futures.ThreadPoolExecutor(max_workers=50) as e:
resp = requests.get("https://www.pvv.ntnu.no/~andreasd/cards/command.php?cmd=get_all_cards", auth=AUTH)
print("== All collections: ==\n")
for i in resp.json():
print("id: ", i["id"])
print("name: ", i["name"])
print("cards: ", i["cards"])
print("card styles: ", sorted(set(map(int, e.map(get_card_style_id,i["cards"])))))
print()
2019-09-19 00:10:12 +02:00
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!")
2019-10-06 21:43:37 +02:00
def push_style():
print(f"POSTing build/style.xsl to site... ", end="")
with open("build/style.xsl") as f:
xsl_data = f.read()
resp = requests.post(f"http://pvv.org/~andreasd/cards/command.php", auth=AUTH, data={
"cmd" : "update_style_xsl",
"id" : str(CARD_STYLE),
"xsl" : str(xsl_data),
})
if resp.ok and resp.text == "<span class='ui success'>XSL Updated</span>":
print("Success!")
else:
print("Failed:")
print(resp.text)
2019-09-19 00:10:12 +02:00
if __name__ == "__main__":
import sys
if len(sys.argv) < 4:
2019-10-14 19:34:45 +02:00
print(__file__, "<action> <uname> <passwd>\n\n\taction: either 'list', 'push', 'push_style' or 'pull'\n")
else:
2019-10-06 21:43:37 +02:00
AUTH = HTTPBasicAuth(sys.argv[2], sys.argv[3])
2019-10-14 19:34:45 +02:00
action = sys.argv[1].strip().lower()
if action == "list": list_collections()
if action == "pull": pull_all()
if action == "push": push_all()
if action == "push_style": push_style()