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
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

10
main.py
View file

@ -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