Created
February 8, 2023 02:54
-
-
Save dougstrouth/3b4f9cea6c99277a032dfd5dffe1dd3c to your computer and use it in GitHub Desktop.
Lorcana API call for CSV
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# import libraries | |
import requests | |
import csv | |
# create base urls for most api calls | |
base_list_url = "https://api.lorcana-api.com/lists/" | |
base_card_url = "https://api.lorcana-api.com/strict/" | |
names_list_url = base_list_url+"names" | |
sets_list_url = base_list_url+"sets" | |
colors_list_url = base_list_url+"colors" | |
# perform initial request for cards listing | |
response = requests.get(names_list_url) | |
# save the card names | |
if response.status_code == 200: | |
card_names = response.json() | |
# print(data) | |
else: | |
print("Request failed with status code:", response.status_code) | |
# create storage for card details | |
card_data = [] | |
# iterate through each card | |
for card_name in card_names: | |
# create specific card url and fetch json | |
specific_card_url = base_card_url+card_name | |
response = requests.get(specific_card_url) | |
if response.status_code == 200: | |
# add found card data to list | |
card_data.append(response.json()) | |
print(response.json()) | |
else: | |
# specify which card failed and then move to the next one | |
print("Request failed with status code:", | |
response.status_code, "for card name ", card_name) | |
continue | |
# Write the data to a CSV file | |
with open('cards.csv', 'w', newline='') as csvfile: | |
''' | |
This may need to be updated as more is learned about card types. Also might want to add further loops to enable search for specific fields that have multiple attributes | |
''' | |
fieldnames = ['name', 'ink-cost', 'color', 'type', 'effect', 'lore', 'artist', 'set', 'set-code', 'subtypes', | |
'power', 'toughness', 'abilities', 'subtitle', 'diamond-amount', 'traits', "ink", "diamond-cost"] | |
writer = csv.DictWriter(csvfile, fieldnames=fieldnames) | |
writer.writeheader() | |
for card in card_data: | |
writer.writerow(card) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment