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
|
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
|
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,19 +124,21 @@ 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)
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
return boardgame
|
return boardgame
|
||||||
|
|
||||||
|
#Creates list of board games from a collection '/collection' URL
|
||||||
def get_boardgames_from_collection_url(collection_url: str) -> list[BoardGame]:
|
def get_boardgames_from_collection_url(collection_url: str) -> list[BoardGame]:
|
||||||
collection_xml = url_to_xml_object(collection_url)
|
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] = []
|
collection_id_list: list[int] = []
|
||||||
|
|
||||||
|
#Get all board game ID's from the collection
|
||||||
for boardgame_item in collection_xml:
|
for boardgame_item in collection_xml:
|
||||||
collection_id_list.append(boardgame_item.get('objectid'))
|
collection_id_list.append(boardgame_item.get('objectid'))
|
||||||
|
|
||||||
|
#Request extra info about the ID's
|
||||||
boardgame_extras = get_multiple_boardgames(collection_id_list)
|
boardgame_extras = get_multiple_boardgames(collection_id_list)
|
||||||
|
|
||||||
assert len(boardgame_extras) == len(collection_xml)
|
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
|
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)
|
||||||
|
|
||||||
|
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
|
return owned_boardgames
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -1,4 +1,14 @@
|
||||||
from pydantic import BaseModel, HttpUrl
|
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):
|
class BoardGame(BaseModel):
|
||||||
id: int
|
id: int
|
||||||
|
|
@ -13,7 +23,20 @@ class BoardGame(BaseModel):
|
||||||
max_playing_time: int
|
max_playing_time: int
|
||||||
min_age: int
|
min_age: int
|
||||||
all_expansion_ids: list[int]
|
all_expansion_ids: list[int]
|
||||||
|
type: BoardgameType = BoardgameType.BOARDGAME
|
||||||
|
|
||||||
class BoardGameExpansion(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 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()
|
||||||
|
|
@ -16,13 +16,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
|
||||||
Loading…
Reference in a new issue