Started work on getting owned collection

This commit is contained in:
Yarne Coppens 2024-07-31 16:03:24 +02:00
parent fa90875ad0
commit ecaea2222a
2 changed files with 70 additions and 34 deletions

View file

@ -13,42 +13,60 @@ def url_to_xml_object(url: HttpUrl) -> ET.Element:
root = ET.fromstring(r.content) root = ET.fromstring(r.content)
return root return root
#Requires single boardgame XML 'item' from bgg api #Requires single boardgame XML 'item' from bgg api on /thing
def convert_xml_to_boardgame(boardgame_xml: ET.Element) -> BoardGame: def convert_xml_to_boardgame(boardgame_xml: ET.Element) -> BoardGame:
boardgame_type = boardgame_xml.get('type') boardgame_type = boardgame_xml.get('type')
boardgame_dict = {
"id" : boardgame_xml.get('id'),
"name" : boardgame_xml.find('name').get('value'),
"description" : boardgame_xml.find('description').text,
"image_url" : boardgame_xml.find('image').text,
"thumbnail_url" : boardgame_xml.find('thumbnail').text,
"year_published" : int(boardgame_xml.find('yearpublished').get('value')),
"min_players" : int(boardgame_xml.find('minplayers').get('value')),
"max_players" : int(boardgame_xml.find('maxplayers').get('value')),
"min_playing_time" : int(boardgame_xml.find('minplaytime').get('value')),
"max_playing_time" : int(boardgame_xml.find('maxplaytime').get('value')),
"min_age" : int(boardgame_xml.find('minage').get('value')),
"all_expansion_ids" : [0,1,2,3]
}
match boardgame_type: match boardgame_type:
case "boardgame": case "boardgame":
boardgame = BoardGame( boardgame = BoardGame(**boardgame_dict)
id = boardgame_xml.get('id'),
name = boardgame_xml.find('name').get('value'),
description = boardgame_xml.find('description').text,
image_url = boardgame_xml.find('image').text,
thumbnail_url = boardgame_xml.find('thumbnail').text,
year_published = int(boardgame_xml.find('yearpublished').get('value')),
min_players = int(boardgame_xml.find('minplayers').get('value')),
max_players = int(boardgame_xml.find('maxplayers').get('value')),
min_playing_time = int(boardgame_xml.find('minplaytime').get('value')),
max_playing_time = int(boardgame_xml.find('maxplaytime').get('value')),
min_age = int(boardgame_xml.find('minage').get('value')),
all_expansion_ids = [0,1,2,3]
)
case "boardgameexpansion": case "boardgameexpansion":
boardgame = BoardGameExpansion( boardgame = BoardGameExpansion(**boardgame_dict)
id = boardgame_xml.get('id'),
name = boardgame_xml.find('name').get('value'),
description = boardgame_xml.find('description').text,
image_url = boardgame_xml.find('image').text, return boardgame
thumbnail_url = boardgame_xml.find('thumbnail').text,
year_published = int(boardgame_xml.find('yearpublished').get('value')), def convert_collection_xml_to_boardgame(boardgame_xml: ET.Element) -> BoardGame:
min_players = int(boardgame_xml.find('minplayers').get('value')),
max_players = int(boardgame_xml.find('maxplayers').get('value')), boardgame_type = boardgame_xml.get('subtype')
min_playing_time = int(boardgame_xml.find('minplaytime').get('value')),
max_playing_time = int(boardgame_xml.find('maxplaytime').get('value')), boardgame_dict = {
min_age = int(boardgame_xml.find('minage').get('value')), "id" : boardgame_xml.get('objectid'),
all_expansion_ids = [0,1,2,3] "name" : boardgame_xml.find('name').get('value'),
) "description" : boardgame_xml.find('description').text,
"image_url" : boardgame_xml.find('image').text,
"thumbnail_url" : boardgame_xml.find('thumbnail').text,
"year_published" : int(boardgame_xml.find('yearpublished').get('value')),
"min_players" : int(boardgame_xml.find('minplayers').get('value')),
"max_players" : int(boardgame_xml.find('maxplayers').get('value')),
"min_playing_time" : int(boardgame_xml.find('minplaytime').get('value')),
"max_playing_time" : int(boardgame_xml.find('maxplaytime').get('value')),
"min_age" : int(boardgame_xml.find('minage').get('value')),
"all_expansion_ids" : [0,1,2,3]
}
match boardgame_type:
case "boardgame":
boardgame = BoardGame(**boardgame_dict)
case "boardgameexpansion":
boardgame = BoardGameExpansion(**boardgame_dict)
@ -62,6 +80,21 @@ def get_boardgame(boardgame_id: int) -> BoardGame:
return requested_boardgame return requested_boardgame
def get_user_owned_collection() -> list[BoardGame]:
url = 'https://boardgamegeek.com/xmlapi2/collection?username={}&own=1&stats=1&excludesubtype=boardgameexpansion&showprivate=1&version=1'.format(auth_manager.username)
collection_xml = url_to_xml_object(url)
print(collection_xml.tag)
owned_collection_list: list[BoardGame] = []
for boardgame_item in collection_xml:
boardgame = convert_xml_to_boardgame(boardgame_item)
owned_collection_list.append(boardgame)
return owned_collection_list
def load_authenticated_bgg_session(username: str, password: str) -> requests.Session: def load_authenticated_bgg_session(username: str, password: str) -> requests.Session:
global authenticated_session global authenticated_session
@ -80,7 +113,5 @@ def load_authenticated_bgg_session(username: str, password: str) -> requests.Ses
login_response = authenticated_session.post(login_url, json=post_data) login_response = authenticated_session.post(login_url, json=post_data)
assert login_response.status_code == 204, "Login failed!" assert login_response.status_code == 204, "Login failed!"
load_authenticated_bgg_session(auth_manager.username, auth_manager.password) load_authenticated_bgg_session(auth_manager.username, auth_manager.password)

View file

@ -2,7 +2,7 @@ from typing import Union
from fastapi import FastAPI from fastapi import FastAPI
from classes.boardgame import BoardGame, BoardGameExpansion from classes.boardgame import BoardGame, BoardGameExpansion
from bgg_connection import get_boardgame from bgg_connection import get_boardgame, get_user_owned_collection
app = FastAPI() app = FastAPI()
@ -19,4 +19,9 @@ def read_item(item_id: int, q: Union[str, None] = None):
@app.get("/boardgame/{boardgame_id}", response_model=BoardGame) @app.get("/boardgame/{boardgame_id}", response_model=BoardGame)
def get_boardgame_by_id(boardgame_id: int): def get_boardgame_by_id(boardgame_id: int):
requested_boardgame: BoardGame = get_boardgame(boardgame_id) requested_boardgame: BoardGame = get_boardgame(boardgame_id)
return requested_boardgame return requested_boardgame
@app.get("/collection", response_model=list[BoardGame])
def get_owned_collection():
requested_collection: list[BoardGame] = get_user_owned_collection()
return requested_collection