Can now retrieve extra info about collection board games

This commit is contained in:
Yarne Coppens 2024-08-01 12:34:55 +02:00
parent 77472e8ff4
commit 188f4d2806
2 changed files with 36 additions and 12 deletions

View file

@ -2,8 +2,9 @@ import requests
import xml.etree.ElementTree as ET import xml.etree.ElementTree as ET
from pydantic import HttpUrl from pydantic import HttpUrl
import requests 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 import auth_manager
authenticated_session: requests.Session = requests.Session() 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 return boardgame_list_to_return
#Requires single boardgame XML 'item' from bgg api on /thing #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:
@ -89,6 +89,29 @@ def convert_collection_xml_to_boardgame(boardgame_extra_info: BoardGame, collect
boardgame_type = collection_boardgame_xml.get('subtype') 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 = { boardgame_dict = {
"id" : boardgame_extra_info.id, "id" : boardgame_extra_info.id,
"name" : boardgame_extra_info.name, "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, "min_playing_time" : boardgame_extra_info.min_playing_time,
"max_playing_time" : boardgame_extra_info.max_playing_time, "max_playing_time" : boardgame_extra_info.max_playing_time,
"min_age" : boardgame_extra_info.min_age, "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: match boardgame_type:
case "boardgame": case "boardgame":
boardgame = BoardGame(**boardgame_dict) boardgame = CollectionBoardGame(**boardgame_dict)
case "boardgameexpansion": 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 return collection_list
def get_user_owned_collection() -> list[BoardGame]: 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) 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) owned_boardgames = get_boardgames_from_collection_url(url_no_expansions)
return owned_boardgames return owned_boardgames

10
main.py
View file

@ -1,7 +1,7 @@
from typing import Union from typing import Union
from fastapi import FastAPI 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 from bgg_connection import get_boardgame, get_user_owned_collection, get_user_wishlist_collection
app = FastAPI() app = FastAPI()
@ -21,13 +21,13 @@ 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]) @app.get("/collection", response_model=list[CollectionBoardGame])
def get_owned_collection(): def get_owned_collection():
requested_collection: list[BoardGame] = get_user_owned_collection() requested_collection: list[CollectionBoardGame] = get_user_owned_collection()
return requested_collection return requested_collection
@app.get("/wishlist", response_model=list[BoardGame]) @app.get("/wishlist", response_model=list[WishlistBoardGame])
def get_wishlist_collection(): def get_wishlist_collection():
requested_collection: list[BoardGame] = get_user_wishlist_collection() requested_collection: list[WishlistBoardGame] = get_user_wishlist_collection()
return requested_collection return requested_collection