Modify sync to only donwload as xml and support multiple collections

This commit is contained in:
Peder Bergebakken Sundt 2020-03-07 01:27:43 +01:00
parent c3538928f3
commit 984e59234a
1 changed files with 19 additions and 17 deletions

34
sync.py
View File

@ -9,8 +9,10 @@ import requests.packages.urllib3.util.connection as urllib3_cn
import socket import socket
import xmltodict import xmltodict
MY_COLLECTION = "Fjompens fjomperi" MY_COLLECTIONS = {
MY_ID = "26" "26" : "Fjompens fjomperi",
"48" : "Fjompens Fjomperi - not in deck",
}
AUTH = None AUTH = None
CARD_STYLE = 6 # Fjompens's card style CARD_STYLE = 6 # Fjompens's card style
@ -22,11 +24,11 @@ CARD_STYLE = 6 # Fjompens's card style
## return family ## return family
##urllib3_cn.allowed_gai_family = allowed_gai_family ##urllib3_cn.allowed_gai_family = allowed_gai_family
def get_card_ids(): def get_card_ids(): # generator
resp = requests.get("https://www.pvv.ntnu.no/~andreasd/cards/command.php?cmd=get_all_cards", auth=AUTH) resp = requests.get("https://www.pvv.ntnu.no/~andreasd/cards/command.php?cmd=get_all_cards", auth=AUTH)
for collection in resp.json(): for collection in resp.json():
if collection["name"] == MY_COLLECTION and collection["id"] == MY_ID: if MY_COLLECTIONS.get(collection["id"]) == collection["name"]:
return collection["cards"] yield from collection["cards"]
def get_card_xml(card_id:int): def get_card_xml(card_id:int):
resp = requests.get(f"https://www.pvv.ntnu.no/~andreasd/cards/command.php?cmd=get_card_xml&id={card_id}", auth=AUTH) resp = requests.get(f"https://www.pvv.ntnu.no/~andreasd/cards/command.php?cmd=get_card_xml&id={card_id}", auth=AUTH)
@ -66,8 +68,8 @@ def list_collections():
resp = requests.get("https://www.pvv.ntnu.no/~andreasd/cards/command.php?cmd=get_all_cards", auth=AUTH) resp = requests.get("https://www.pvv.ntnu.no/~andreasd/cards/command.php?cmd=get_all_cards", auth=AUTH)
print("== All collections: ==\n") print("== All collections: ==\n")
for i in resp.json(): for i in resp.json():
print("id: ", i["id"]) print("collection_id: ", i["id"])
print("name: ", i["name"]) print("collection_name: ", i["name"])
print("cards: ", i["cards"]) print("cards: ", i["cards"])
print("card styles: ", sorted(set(map(int, e.map(get_card_style_id, i["cards"]))))) print("card styles: ", sorted(set(map(int, e.map(get_card_style_id, i["cards"])))))
print() print()
@ -88,14 +90,14 @@ def pull_all():
except: except:
xml = None xml = None
if xml and "ability_card" in xml: #if xml and "ability_card" in xml:
if "yaml_data" in xml["ability_card"]: # if "yaml_data" in xml["ability_card"]:
if len(xml["ability_card"].keys()) == 1: # if len(xml["ability_card"].keys()) == 1:
yaml_data = xml["ability_card"]["yaml_data"] # yaml_data = xml["ability_card"]["yaml_data"]
with open(f"cards/{card_id}.yaml", "w") as f: # with open(f"cards/{card_id}.yaml", "w") as f:
f.write(yaml_data + "\n") # f.write(yaml_data + "\n")
print(f"./cards/{card_id}.yaml written!") # print(f"./cards/{card_id}.yaml written!")
continue # continue
ftype = "xml" if data.strip() else "yaml" ftype = "xml" if data.strip() else "yaml"
with open(f"cards/{card_id}.{ftype}", "w") as f: with open(f"cards/{card_id}.{ftype}", "w") as f:
f.write(data + "\n") f.write(data + "\n")
@ -105,7 +107,7 @@ def push_all():
existing_card_ids = get_card_ids() existing_card_ids = get_card_ids()
for card_id in existing_card_ids: for card_id in existing_card_ids:
if os.path.isfile(f"cards/{card_id}.yaml"): if os.path.isfile(f"cards/{card_id}.yaml"): # deprecated, will no be written anymore
fname = f"cards/{card_id}.yaml" fname = f"cards/{card_id}.yaml"
with open(fname) as f: with open(fname) as f:
yaml_data = f.read() yaml_data = f.read()