Merge branch 'new_boardgame_classes' into fastapi
This commit is contained in:
commit
8d37ecd4d4
3 changed files with 69 additions and 14 deletions
|
|
@ -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
|
||||
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,19 +124,21 @@ 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)
|
||||
|
||||
|
||||
|
||||
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 +146,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)
|
||||
|
|
@ -139,8 +166,13 @@ 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)
|
||||
|
||||
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
|
||||
|
||||
|
|
|
|||
|
|
@ -1,4 +1,14 @@
|
|||
from pydantic import BaseModel, HttpUrl
|
||||
from datetime import date
|
||||
from enum import Enum
|
||||
|
||||
class BoardgameType(Enum):
|
||||
BOARDGAME = 'boardgame'
|
||||
BOARDGAMEEXPANSION = 'boardgameexpansion'
|
||||
COLLECTIONBOARDGAME = 'collectionboardgame'
|
||||
COLLECTIONBOARDGAMEEXPANSION = 'collectionboardgameexpansion'
|
||||
WISHLISTBOARDGAME = 'wishlistboardgame'
|
||||
|
||||
|
||||
class BoardGame(BaseModel):
|
||||
id: int
|
||||
|
|
@ -13,7 +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):
|
||||
type: BoardgameType = BoardgameType.COLLECTIONBOARDGAMEEXPANSION
|
||||
|
||||
class WishlistBoardGame(BoardGame):
|
||||
wishlist_priority: int
|
||||
type: BoardgameType = BoardgameType.WISHLISTBOARDGAME
|
||||
10
main.py
10
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()
|
||||
|
|
@ -16,13 +16,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
|
||||
Loading…
Reference in a new issue