From ba0bd44116e3f2f152b37f22d3bcfe171eb663f3 Mon Sep 17 00:00:00 2001 From: Yarne Coppens Date: Thu, 1 Aug 2024 11:48:29 +0200 Subject: [PATCH 1/6] Created new board game classes --- classes/boardgame.py | 15 +++++++++++++-- 1 file changed, 13 insertions(+), 2 deletions(-) diff --git a/classes/boardgame.py b/classes/boardgame.py index 000c204..fb0b9c7 100644 --- a/classes/boardgame.py +++ b/classes/boardgame.py @@ -1,4 +1,5 @@ from pydantic import BaseModel, HttpUrl +from datetime import date class BoardGame(BaseModel): id: int @@ -14,6 +15,16 @@ class BoardGame(BaseModel): min_age: int all_expansion_ids: list[int] - class BoardGameExpansion(BoardGame): - pass \ No newline at end of file + pass + +class CollectionBoardGame(BoardGame): + price_paid: float + acquisition_date: date + acquired_from: str + +class CollectionBoardGameExpansion(CollectionBoardGame): + pass + +class WishlistBoardGame(BoardGame): + wishlist_priority: int \ No newline at end of file From 655b1b5cf0fc9dfa8c5efe3b4e5bf20ee5517415 Mon Sep 17 00:00:00 2001 From: Yarne Coppens Date: Thu, 1 Aug 2024 12:05:51 +0200 Subject: [PATCH 2/6] Created board game type Enum --- bgg_connection.py | 2 +- classes/boardgame.py | 9 +++++++++ 2 files changed, 10 insertions(+), 1 deletion(-) diff --git a/bgg_connection.py b/bgg_connection.py index a58e20b..2ef407a 100644 --- a/bgg_connection.py +++ b/bgg_connection.py @@ -3,7 +3,7 @@ import xml.etree.ElementTree as ET from pydantic import HttpUrl import requests -from classes.boardgame import BoardGame, BoardGameExpansion +from classes.boardgame import BoardGame, BoardGameExpansion, CollectionBoardGame, WishlistBoardGame, BoardgameType import auth_manager authenticated_session: requests.Session = requests.Session() diff --git a/classes/boardgame.py b/classes/boardgame.py index fb0b9c7..ff32ccd 100644 --- a/classes/boardgame.py +++ b/classes/boardgame.py @@ -1,5 +1,14 @@ from pydantic import BaseModel, HttpUrl from datetime import date +from enum import Enum + +class BoardgameType(Enum): + BOARDGAME = 1 + BOARDGAMEEXPANSION = 2 + COLLECTIONBOARDGAME = 3 + COLLECTIONBOARDGAMEEXPANSION = 4 + WISHLISTBOARDGAME = 5 + class BoardGame(BaseModel): id: int From d0fb364808421f66375d97367a38f9ab0cf50626 Mon Sep 17 00:00:00 2001 From: Yarne Coppens Date: Thu, 1 Aug 2024 12:16:41 +0200 Subject: [PATCH 3/6] Added comments --- bgg_connection.py | 3 +++ 1 file changed, 3 insertions(+) diff --git a/bgg_connection.py b/bgg_connection.py index 2ef407a..0ca126f 100644 --- a/bgg_connection.py +++ b/bgg_connection.py @@ -114,6 +114,7 @@ def convert_collection_xml_to_boardgame(boardgame_extra_info: BoardGame, collect return boardgame +#Creates list of board games from a collection '/collection' URL def get_boardgames_from_collection_url(collection_url: str) -> list[BoardGame]: collection_xml = url_to_xml_object(collection_url) @@ -121,9 +122,11 @@ def get_boardgames_from_collection_url(collection_url: str) -> list[BoardGame]: collection_id_list: list[int] = [] + #Get all board game ID's from the collection for boardgame_item in collection_xml: collection_id_list.append(boardgame_item.get('objectid')) + #Request extra info about the ID's boardgame_extras = get_multiple_boardgames(collection_id_list) assert len(boardgame_extras) == len(collection_xml) From 77472e8ff49d60eda3912686e22b626ae10d28cf Mon Sep 17 00:00:00 2001 From: Yarne Coppens Date: Thu, 1 Aug 2024 12:16:50 +0200 Subject: [PATCH 4/6] Gave all board game classes a type --- classes/boardgame.py | 19 +++++++++++-------- 1 file changed, 11 insertions(+), 8 deletions(-) diff --git a/classes/boardgame.py b/classes/boardgame.py index ff32ccd..50ac0ec 100644 --- a/classes/boardgame.py +++ b/classes/boardgame.py @@ -3,11 +3,11 @@ from datetime import date from enum import Enum class BoardgameType(Enum): - BOARDGAME = 1 - BOARDGAMEEXPANSION = 2 - COLLECTIONBOARDGAME = 3 - COLLECTIONBOARDGAMEEXPANSION = 4 - WISHLISTBOARDGAME = 5 + BOARDGAME = 'boardgame' + BOARDGAMEEXPANSION = 'boardgameexpansion' + COLLECTIONBOARDGAME = 'collectionboardgame' + COLLECTIONBOARDGAMEEXPANSION = 'collectionboardgameexpansion' + WISHLISTBOARDGAME = 'wishlistboardgame' class BoardGame(BaseModel): @@ -23,17 +23,20 @@ class BoardGame(BaseModel): max_playing_time: int min_age: int all_expansion_ids: list[int] + type: BoardgameType = BoardgameType.BOARDGAME class BoardGameExpansion(BoardGame): - pass + type: BoardgameType = BoardgameType.BOARDGAMEEXPANSION class CollectionBoardGame(BoardGame): price_paid: float acquisition_date: date acquired_from: str + type: BoardgameType = BoardgameType.COLLECTIONBOARDGAME class CollectionBoardGameExpansion(CollectionBoardGame): - pass + type: BoardgameType = BoardgameType.COLLECTIONBOARDGAMEEXPANSION class WishlistBoardGame(BoardGame): - wishlist_priority: int \ No newline at end of file + wishlist_priority: int + type: BoardgameType = BoardgameType.WISHLISTBOARDGAME \ No newline at end of file From 188f4d280690bfcf303d0d1a165b951b65b0f8b2 Mon Sep 17 00:00:00 2001 From: Yarne Coppens Date: Thu, 1 Aug 2024 12:34:55 +0200 Subject: [PATCH 5/6] Can now retrieve extra info about collection board games --- bgg_connection.py | 38 +++++++++++++++++++++++++++++++------- main.py | 10 +++++----- 2 files changed, 36 insertions(+), 12 deletions(-) diff --git a/bgg_connection.py b/bgg_connection.py index 0ca126f..1ffa8b4 100644 --- a/bgg_connection.py +++ b/bgg_connection.py @@ -2,8 +2,9 @@ import requests import xml.etree.ElementTree as ET from pydantic import HttpUrl import requests +from datetime import datetime -from classes.boardgame import BoardGame, BoardGameExpansion, CollectionBoardGame, WishlistBoardGame, BoardgameType +from classes.boardgame import BoardGame, BoardGameExpansion, CollectionBoardGame, CollectionBoardGameExpansion, WishlistBoardGame, BoardgameType import auth_manager authenticated_session: requests.Session = requests.Session() @@ -46,7 +47,6 @@ def get_multiple_boardgames(boardgame_ids: list[int]) -> list[BoardGame]: return boardgame_list_to_return - #Requires single boardgame XML 'item' from bgg api on /thing def convert_xml_to_boardgame(boardgame_xml: ET.Element) -> BoardGame: @@ -89,6 +89,29 @@ def convert_collection_xml_to_boardgame(boardgame_extra_info: BoardGame, collect boardgame_type = collection_boardgame_xml.get('subtype') + price_paid = collection_boardgame_xml.find('privateinfo').get('pricepaid') + if price_paid == '': + print(boardgame_extra_info.name) + price_paid = 0.0 + else: + price_paid = float(price_paid) + + + date_string = collection_boardgame_xml.find('privateinfo').get('acquisitiondate') + if date_string == '': + date_string = '1970-01-01' + + + acquisition_date = datetime.strptime(date_string, '%Y-%m-%d').date() + + acquired_from = collection_boardgame_xml.find('privateinfo').get('acquiredfrom') + + collection_boardgame_dict = { + "price_paid" : price_paid, + "acquisition_date" : acquisition_date, + "acquired_from" : acquired_from + } + boardgame_dict = { "id" : boardgame_extra_info.id, "name" : boardgame_extra_info.name, @@ -101,14 +124,15 @@ def convert_collection_xml_to_boardgame(boardgame_extra_info: BoardGame, collect "min_playing_time" : boardgame_extra_info.min_playing_time, "max_playing_time" : boardgame_extra_info.max_playing_time, "min_age" : boardgame_extra_info.min_age, - "all_expansion_ids" : boardgame_extra_info.all_expansion_ids + "all_expansion_ids" : boardgame_extra_info.all_expansion_ids, + **collection_boardgame_dict } match boardgame_type: case "boardgame": - boardgame = BoardGame(**boardgame_dict) + boardgame = CollectionBoardGame(**boardgame_dict) case "boardgameexpansion": - boardgame = BoardGameExpansion(**boardgame_dict) + boardgame = CollectionBoardGameExpansion(**boardgame_dict) @@ -142,8 +166,8 @@ def get_boardgames_from_collection_url(collection_url: str) -> list[BoardGame]: return collection_list def get_user_owned_collection() -> list[BoardGame]: - url = 'https://boardgamegeek.com/xmlapi2/collection?username={}&own=1&stats=1&showprivate=1&version=1'.format(auth_manager.username) - owned_boardgames = get_boardgames_from_collection_url(url) + url_no_expansions = 'https://boardgamegeek.com/xmlapi2/collection?username={}&own=1&stats=1&excludesubtype=boardgameexpansion&showprivate=1&version=1'.format(auth_manager.username) + owned_boardgames = get_boardgames_from_collection_url(url_no_expansions) return owned_boardgames diff --git a/main.py b/main.py index 7bf4310..b1ec4e9 100644 --- a/main.py +++ b/main.py @@ -1,7 +1,7 @@ from typing import Union from fastapi import FastAPI -from classes.boardgame import BoardGame, BoardGameExpansion +from classes.boardgame import BoardGame, BoardGameExpansion, CollectionBoardGame, CollectionBoardGameExpansion, WishlistBoardGame from bgg_connection import get_boardgame, get_user_owned_collection, get_user_wishlist_collection app = FastAPI() @@ -21,13 +21,13 @@ def get_boardgame_by_id(boardgame_id: int): requested_boardgame: BoardGame = get_boardgame(boardgame_id) return requested_boardgame -@app.get("/collection", response_model=list[BoardGame]) +@app.get("/collection", response_model=list[CollectionBoardGame]) def get_owned_collection(): - requested_collection: list[BoardGame] = get_user_owned_collection() + requested_collection: list[CollectionBoardGame] = get_user_owned_collection() return requested_collection -@app.get("/wishlist", response_model=list[BoardGame]) +@app.get("/wishlist", response_model=list[WishlistBoardGame]) def get_wishlist_collection(): - requested_collection: list[BoardGame] = get_user_wishlist_collection() + requested_collection: list[WishlistBoardGame] = get_user_wishlist_collection() return requested_collection \ No newline at end of file From c02dc69b64fd8130d86c83b9805717f646cfbfb4 Mon Sep 17 00:00:00 2001 From: Yarne Coppens Date: Thu, 1 Aug 2024 12:39:36 +0200 Subject: [PATCH 6/6] Added expansions to collection retrieval --- bgg_connection.py | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/bgg_connection.py b/bgg_connection.py index 1ffa8b4..190b4d6 100644 --- a/bgg_connection.py +++ b/bgg_connection.py @@ -169,6 +169,11 @@ def get_user_owned_collection() -> list[BoardGame]: url_no_expansions = 'https://boardgamegeek.com/xmlapi2/collection?username={}&own=1&stats=1&excludesubtype=boardgameexpansion&showprivate=1&version=1'.format(auth_manager.username) owned_boardgames = get_boardgames_from_collection_url(url_no_expansions) + url_only_expansions = 'https://boardgamegeek.com/xmlapi2/collection?username={}&own=1&stats=1&subtype=boardgameexpansion&showprivate=1&version=1'.format(auth_manager.username) + owned_boardgame_expansions = get_boardgames_from_collection_url(url_only_expansions) + + owned_boardgames += owned_boardgame_expansions + return owned_boardgames def get_user_wishlist_collection() -> list[BoardGame]: