bgg_api/bgg_connection.py
2024-07-26 10:46:46 +02:00

34 lines
No EOL
1.1 KiB
Python

import requests
import xml.etree.ElementTree as ET
from pydantic import HttpUrl
from classes.boardgame import BoardGame, BoardGameExpansion
def url_to_xml_object(url: HttpUrl) -> ET.Element:
r = requests.get(url)
root = ET.fromstring(r.content)
return root
def get_boardgame(boardgame_id: int) -> BoardGame:
url : str = "https://boardgamegeek.com/xmlapi2/thing?id={}&stats=true".format(boardgame_id)
boardgame_xml_object : ET.Element = url_to_xml_object(url)
boardgame_name = boardgame_xml_object.find('item').find('name').get('value')
requested_boardgame = BoardGame(
id = boardgame_id,
name = boardgame_name,
description = 'Some description',
image_url = 'https://bordspellen.yarnecoppens.com/static/favicon.ico',
thumbnail_url = 'https://bordspellen.yarnecoppens.com/static/favicon.ico',
year_published = 9999,
min_players = 0,
max_players = 99,
min_playing_time = 0,
max_playing_time = 99,
min_age = 0,
all_expansion_ids = [0,1,2,3]
)
return requested_boardgame