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.
How to automatically get the release date of the latest Magic: The Gathering Standard set?
I’m building a tool to check Magic: The Gathering deck legality in custom formats and want to update my formats with the same frequency as the Penny Dreadful format. Penny Dreadful updates when a new Standard-legal set is released but typically ignores smaller supplemental sets.
Is there a programmatic way to fetch the release date of the latest Standard set, ideally via an API or a reliable data source?
1 answer
This code uses the What's in Standard API to fetch all Standard-legal sets, filters to those already released, finds the most recent, and updates a local timestamp to track the latest Standard set. It automates detecting when a new Standard set enters rotation.
import requests
import json
import os
from datetime import datetime, timezone
LAST_UPDATED_FILE = "last_updated.json"
WIS_SETS_API = "https://whatsinstandard.com/api/v6/standard.json"
def fetch_sets_data():
"""Fetch data from What's in Standard API."""
response = requests.get(WIS_SETS_API)
response.raise_for_status()
return response.json()["sets"]
def load_saved_updated_at(file_path=LAST_UPDATED_FILE):
"""Load previously saved updated_at from file."""
if os.path.exists(file_path):
with open(file_path, "r") as f:
data = json.load(f)
return data.get("updated_at")
return None
def save_updated_at(updated_at, file_path=LAST_UPDATED_FILE):
"""Save updated_at to file."""
with open(file_path, "w") as f:
json.dump({"updated_at": updated_at}, f)
def check_for_update(latest_updated_at, saved_updated_at):
"""Return True if update detected, else False. Print 'updating' if True."""
if saved_updated_at is None or latest_updated_at > saved_updated_at:
save_updated_at(latest_updated_at)
return True
return False
def main():
now = datetime.now(timezone.utc)
sets = fetch_sets_data()
# Filter to sets already released
released_sets = [
s
for s in sets
if s.get("enterDate", {}).get("exact") # skip sets with null
and datetime.fromisoformat(s["enterDate"]["exact"]).replace(tzinfo=timezone.utc)
<= now
]
if not released_sets:
print("No sets released yet")
return
# Find the most recent released set
latest_set = max(
released_sets,
key=lambda s: datetime.fromisoformat(s["enterDate"]["exact"]).replace(
tzinfo=timezone.utc
),
)
latest_updated_at = latest_set["enterDate"]["exact"]
saved_updated_at = load_saved_updated_at()
if check_for_update(latest_updated_at, saved_updated_at):
print(f"Updating to latest released set: {latest_set['name']}")
if __name__ == "__main__":
main()

0 comment threads