Can now get multiple board games at once

This commit is contained in:
Yarne Coppens 2024-08-01 09:50:55 +02:00
parent ecaea2222a
commit cf59b7e24b

View file

@ -10,16 +10,51 @@ authenticated_session: requests.Session = requests.Session()
def url_to_xml_object(url: HttpUrl) -> ET.Element: def url_to_xml_object(url: HttpUrl) -> ET.Element:
r = authenticated_session.get(url) r = authenticated_session.get(url)
assert r.status_code == 200
root = ET.fromstring(r.content) root = ET.fromstring(r.content)
return root return root
def get_boardgame(boardgame_id: int) -> BoardGame:
url : str = "https://boardgamegeek.com/xmlapi2/thing?id={}&stats=true".format(boardgame_id)
boardgame_xml_object : ET.Element = url_to_xml_object(url)
requested_boardgame = convert_xml_to_boardgame(boardgame_xml_object.find('item'))
return requested_boardgame
def get_multiple_boardgames(boardgame_ids: list[int]) -> list[BoardGame]:
def divide_list_in_chunks(list_to_divide: list[int], chunk_size: int = 20):
for i in range(0, len(list_to_divide), chunk_size):
yield list_to_divide[i:i + chunk_size]
boardgame_list_to_return: list[BoardGame] = []
#Boardgamegeek only allows chunks of 20 boardgames at a time
boardgame_ids_divided = list(divide_list_in_chunks(boardgame_ids))
for boardgame_id_list_20 in boardgame_ids_divided:
boardgame_id_list_commas: str = ','.join(boardgame_id_list_20)
url : str = "https://boardgamegeek.com/xmlapi2/thing?id={}&stats=true".format(boardgame_id_list_commas)
print(url)
boardgame_xml_object : ET.Element = url_to_xml_object(url)
for boardgame in boardgame_xml_object:
requested_boardgame = convert_xml_to_boardgame(boardgame_xml_object.find('item'))
boardgame_list_to_return.append(requested_boardgame)
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:
boardgame_type = boardgame_xml.get('type') boardgame_type = boardgame_xml.get('type')
boardgame_dict = { boardgame_dict = {
"id" : boardgame_xml.get('id'), "id" : int(boardgame_xml.get('id')),
"name" : boardgame_xml.find('name').get('value'), "name" : boardgame_xml.find('name').get('value'),
"description" : boardgame_xml.find('description').text, "description" : boardgame_xml.find('description').text,
"image_url" : boardgame_xml.find('image').text, "image_url" : boardgame_xml.find('image').text,
@ -45,20 +80,22 @@ def convert_xml_to_boardgame(boardgame_xml: ET.Element) -> BoardGame:
def convert_collection_xml_to_boardgame(boardgame_xml: ET.Element) -> BoardGame: def convert_collection_xml_to_boardgame(boardgame_xml: ET.Element) -> BoardGame:
boardgame_extra: BoardGame = get_boardgame(boardgame_xml.get('objectid'))
boardgame_type = boardgame_xml.get('subtype') boardgame_type = boardgame_xml.get('subtype')
boardgame_dict = { boardgame_dict = {
"id" : boardgame_xml.get('objectid'), "id" : boardgame_extra.id,
"name" : boardgame_xml.find('name').get('value'), "name" : boardgame_extra.name,
"description" : boardgame_xml.find('description').text, "description" : boardgame_extra.description,
"image_url" : boardgame_xml.find('image').text, "image_url" : boardgame_extra.image_url,
"thumbnail_url" : boardgame_xml.find('thumbnail').text, "thumbnail_url" : boardgame_extra.thumbnail_url,
"year_published" : int(boardgame_xml.find('yearpublished').get('value')), "year_published" : boardgame_extra.year_published,
"min_players" : int(boardgame_xml.find('minplayers').get('value')), "min_players" : boardgame_extra.min_players,
"max_players" : int(boardgame_xml.find('maxplayers').get('value')), "max_players" : boardgame_extra.max_players,
"min_playing_time" : int(boardgame_xml.find('minplaytime').get('value')), "min_playing_time" : boardgame_extra.min_playing_time,
"max_playing_time" : int(boardgame_xml.find('maxplaytime').get('value')), "max_playing_time" : boardgame_extra.max_playing_time,
"min_age" : int(boardgame_xml.find('minage').get('value')), "min_age" : boardgame_extra.min_age,
"all_expansion_ids" : [0,1,2,3] "all_expansion_ids" : [0,1,2,3]
} }
@ -72,14 +109,6 @@ def convert_collection_xml_to_boardgame(boardgame_xml: ET.Element) -> BoardGame:
return boardgame return boardgame
def get_boardgame(boardgame_id: int) -> BoardGame:
url : str = "https://boardgamegeek.com/xmlapi2/thing?id={}&stats=true".format(boardgame_id)
boardgame_xml_object : ET.Element = url_to_xml_object(url)
requested_boardgame = convert_xml_to_boardgame(boardgame_xml_object.find('item'))
return requested_boardgame
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&excludesubtype=boardgameexpansion&showprivate=1&version=1'.format(auth_manager.username) url = 'https://boardgamegeek.com/xmlapi2/collection?username={}&own=1&stats=1&excludesubtype=boardgameexpansion&showprivate=1&version=1'.format(auth_manager.username)
collection_xml = url_to_xml_object(url) collection_xml = url_to_xml_object(url)
@ -89,7 +118,7 @@ def get_user_owned_collection() -> list[BoardGame]:
owned_collection_list: list[BoardGame] = [] owned_collection_list: list[BoardGame] = []
for boardgame_item in collection_xml: for boardgame_item in collection_xml:
boardgame = convert_xml_to_boardgame(boardgame_item) boardgame = convert_collection_xml_to_boardgame(boardgame_item)
owned_collection_list.append(boardgame) owned_collection_list.append(boardgame)
return owned_collection_list return owned_collection_list