Modify sync to only donwload as xml and support multiple collections

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

36
sync.py
View File

@ -9,8 +9,10 @@ import requests.packages.urllib3.util.connection as urllib3_cn
import socket
import xmltodict
MY_COLLECTION = "Fjompens fjomperi"
MY_ID = "26"
MY_COLLECTIONS = {
"26" : "Fjompens fjomperi",
"48" : "Fjompens Fjomperi - not in deck",
}
AUTH = None
CARD_STYLE = 6 # Fjompens's card style
@ -22,11 +24,11 @@ CARD_STYLE = 6 # Fjompens's card style
## return 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)
for collection in resp.json():
if collection["name"] == MY_COLLECTION and collection["id"] == MY_ID:
return collection["cards"]
if MY_COLLECTIONS.get(collection["id"]) == collection["name"]:
yield from collection["cards"]
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)
@ -66,10 +68,10 @@ def list_collections():
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("collection_id: ", i["id"])
print("collection_name: ", i["name"])
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()
def pull_all():
@ -88,14 +90,14 @@ def pull_all():
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
#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
ftype = "xml" if data.strip() else "yaml"
with open(f"cards/{card_id}.{ftype}", "w") as f:
f.write(data + "\n")
@ -105,7 +107,7 @@ 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"):
if os.path.isfile(f"cards/{card_id}.yaml"): # deprecated, will no be written anymore
fname = f"cards/{card_id}.yaml"
with open(fname) as f:
yaml_data = f.read()