Welcome to Software Development on Codidact!
Will you help us build our independent community of developers helping developers? We're small and trying to grow. We welcome questions about all aspects of software development, from design to code to QA and more. Got questions? Got answers? Got code you'd like someone to review? Please join us.
Post History
✅ This will create a fully Moxfield-compatible CSV with all cards from a Scryfall search. import requests import csv import time QUERY = "f:standard f:penny usd<=1" BASE_URL = "https://ap...
#1: Initial revision
✅ This will create a fully Moxfield-compatible CSV with all cards from a Scryfall search.
```python
import requests
import csv
import time
QUERY = "f:standard f:penny usd<=1"
BASE_URL = "https://api.scryfall.com/cards/search"
PARAMS = {
"q": QUERY,
"unique": "cards",
"format": "json"
}
OUTPUT_FILE = "moxfield_import.csv"
FIELDNAMES = [
"Count",
"Tradelist Count",
"Name",
"Edition",
"Condition",
"Language",
"Foil",
"Tags",
"Last Modified",
"Collector Number",
"Alter",
"Proxy",
"Purchase Price"
]
def fetch_all_cards():
url = BASE_URL
params = PARAMS.copy()
while True:
resp = requests.get(url, params=params)
resp.raise_for_status()
data = resp.json()
for card in data.get("data", []):
yield card
if not data.get("has_more"):
break
url = data["next_page"]
params = None
time.sleep(0.2)
def write_cards_to_csv(filename):
with open(filename, "w", newline="", encoding="utf-8") as f:
writer = csv.DictWriter(f, fieldnames=FIELDNAMES)
writer.writeheader()
for card in fetch_all_cards():
row = {
"Count": 1,
"Tradelist Count": "",
"Name": card.get("name"),
"Edition": card.get("set"),
"Condition": "",
"Language": card.get("lang"),
"Foil": "Yes" if card.get("foil") else "No",
"Tags": "",
"Last Modified": "",
"Collector Number": card.get("collector_number"),
"Alter": "",
"Proxy": "",
"Purchase Price": ""
}
writer.writerow(row)
if __name__ == "__main__":
write_cards_to_csv(OUTPUT_FILE)
print(f"Saved all cards to {OUTPUT_FILE}")
```
