diff --git a/bgg_connection.py b/bgg_connection.py index 66086b3..775df68 100644 --- a/bgg_connection.py +++ b/bgg_connection.py @@ -13,42 +13,60 @@ def url_to_xml_object(url: HttpUrl) -> ET.Element: root = ET.fromstring(r.content) 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: 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: case "boardgame": - boardgame = BoardGame( - 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] - ) + boardgame = BoardGame(**boardgame_dict) case "boardgameexpansion": - boardgame = BoardGameExpansion( - 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] - ) + boardgame = BoardGameExpansion(**boardgame_dict) + + + + return boardgame + +def convert_collection_xml_to_boardgame(boardgame_xml: ET.Element) -> BoardGame: + + boardgame_type = boardgame_xml.get('subtype') + + boardgame_dict = { + "id" : boardgame_xml.get('objectid'), + "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 +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: 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) assert login_response.status_code == 204, "Login failed!" - - - + load_authenticated_bgg_session(auth_manager.username, auth_manager.password) \ No newline at end of file diff --git a/main.py b/main.py index 97993e8..2675452 100644 --- a/main.py +++ b/main.py @@ -2,7 +2,7 @@ from typing import Union from fastapi import FastAPI from classes.boardgame import BoardGame, BoardGameExpansion -from bgg_connection import get_boardgame +from bgg_connection import get_boardgame, get_user_owned_collection 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) def get_boardgame_by_id(boardgame_id: int): requested_boardgame: BoardGame = get_boardgame(boardgame_id) - return requested_boardgame \ No newline at end of file + 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 \ No newline at end of file