2024-07-25 19:58:28 +02:00
|
|
|
import requests
|
|
|
|
|
import xml.etree.ElementTree as ET
|
|
|
|
|
from pydantic import HttpUrl
|
2024-07-31 14:54:31 +02:00
|
|
|
import requests
|
2024-07-25 19:58:28 +02:00
|
|
|
|
2024-07-25 21:52:49 +02:00
|
|
|
from classes.boardgame import BoardGame, BoardGameExpansion
|
2024-07-31 15:22:30 +02:00
|
|
|
import auth_manager
|
|
|
|
|
|
2024-07-31 15:43:33 +02:00
|
|
|
authenticated_session: requests.Session = requests.Session()
|
2024-07-25 21:52:49 +02:00
|
|
|
|
2024-07-25 19:58:28 +02:00
|
|
|
def url_to_xml_object(url: HttpUrl) -> ET.Element:
|
2024-07-31 15:22:30 +02:00
|
|
|
r = authenticated_session.get(url)
|
2024-08-01 09:50:55 +02:00
|
|
|
assert r.status_code == 200
|
2024-07-25 19:58:28 +02:00
|
|
|
root = ET.fromstring(r.content)
|
|
|
|
|
return root
|
2024-07-25 21:52:49 +02:00
|
|
|
|
2024-08-01 09:50:55 +02:00
|
|
|
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)
|
2024-08-01 09:51:49 +02:00
|
|
|
boardgames_xml_object : ET.Element = url_to_xml_object(url)
|
2024-08-01 09:50:55 +02:00
|
|
|
|
2024-08-01 09:51:49 +02:00
|
|
|
for boardgame_xml_object in boardgames_xml_object:
|
2024-08-01 09:50:55 +02:00
|
|
|
requested_boardgame = convert_xml_to_boardgame(boardgame_xml_object.find('item'))
|
|
|
|
|
boardgame_list_to_return.append(requested_boardgame)
|
|
|
|
|
|
|
|
|
|
return boardgame_list_to_return
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2024-07-31 16:03:24 +02:00
|
|
|
#Requires single boardgame XML 'item' from bgg api on /thing
|
2024-07-26 11:41:34 +02:00
|
|
|
def convert_xml_to_boardgame(boardgame_xml: ET.Element) -> BoardGame:
|
2024-07-26 12:01:12 +02:00
|
|
|
|
|
|
|
|
boardgame_type = boardgame_xml.get('type')
|
|
|
|
|
|
2024-07-31 16:03:24 +02:00
|
|
|
boardgame_dict = {
|
2024-08-01 09:50:55 +02:00
|
|
|
"id" : int(boardgame_xml.get('id')),
|
2024-07-31 16:03:24 +02:00
|
|
|
"name" : boardgame_xml.find('name').get('value'),
|
|
|
|
|
"description" : boardgame_xml.find('description').text,
|
|
|
|
|
"image_url" : boardgame_xml.find('image').text,
|
|
|
|
|
"thumbnail_url" : boardgame_xml.find('thumbnail').text,
|
|
|
|
|
"year_published" : int(boardgame_xml.find('yearpublished').get('value')),
|
|
|
|
|
"min_players" : int(boardgame_xml.find('minplayers').get('value')),
|
|
|
|
|
"max_players" : int(boardgame_xml.find('maxplayers').get('value')),
|
|
|
|
|
"min_playing_time" : int(boardgame_xml.find('minplaytime').get('value')),
|
|
|
|
|
"max_playing_time" : int(boardgame_xml.find('maxplaytime').get('value')),
|
|
|
|
|
"min_age" : int(boardgame_xml.find('minage').get('value')),
|
|
|
|
|
"all_expansion_ids" : [0,1,2,3]
|
|
|
|
|
}
|
|
|
|
|
|
2024-07-26 12:01:12 +02:00
|
|
|
match boardgame_type:
|
|
|
|
|
case "boardgame":
|
2024-07-31 16:03:24 +02:00
|
|
|
boardgame = BoardGame(**boardgame_dict)
|
2024-07-26 12:01:12 +02:00
|
|
|
case "boardgameexpansion":
|
2024-07-31 16:03:24 +02:00
|
|
|
boardgame = BoardGameExpansion(**boardgame_dict)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
return boardgame
|
|
|
|
|
|
|
|
|
|
def convert_collection_xml_to_boardgame(boardgame_xml: ET.Element) -> BoardGame:
|
|
|
|
|
|
2024-08-01 09:50:55 +02:00
|
|
|
boardgame_extra: BoardGame = get_boardgame(boardgame_xml.get('objectid'))
|
|
|
|
|
|
2024-07-31 16:03:24 +02:00
|
|
|
boardgame_type = boardgame_xml.get('subtype')
|
|
|
|
|
|
|
|
|
|
boardgame_dict = {
|
2024-08-01 09:50:55 +02:00
|
|
|
"id" : boardgame_extra.id,
|
|
|
|
|
"name" : boardgame_extra.name,
|
|
|
|
|
"description" : boardgame_extra.description,
|
|
|
|
|
"image_url" : boardgame_extra.image_url,
|
|
|
|
|
"thumbnail_url" : boardgame_extra.thumbnail_url,
|
|
|
|
|
"year_published" : boardgame_extra.year_published,
|
|
|
|
|
"min_players" : boardgame_extra.min_players,
|
|
|
|
|
"max_players" : boardgame_extra.max_players,
|
|
|
|
|
"min_playing_time" : boardgame_extra.min_playing_time,
|
|
|
|
|
"max_playing_time" : boardgame_extra.max_playing_time,
|
|
|
|
|
"min_age" : boardgame_extra.min_age,
|
2024-07-31 16:03:24 +02:00
|
|
|
"all_expansion_ids" : [0,1,2,3]
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
match boardgame_type:
|
|
|
|
|
case "boardgame":
|
|
|
|
|
boardgame = BoardGame(**boardgame_dict)
|
|
|
|
|
case "boardgameexpansion":
|
|
|
|
|
boardgame = BoardGameExpansion(**boardgame_dict)
|
2024-07-26 12:01:12 +02:00
|
|
|
|
|
|
|
|
|
2024-07-26 11:41:34 +02:00
|
|
|
|
|
|
|
|
return boardgame
|
2024-07-25 21:52:49 +02:00
|
|
|
|
2024-07-31 16:03:24 +02:00
|
|
|
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)
|
|
|
|
|
collection_xml = url_to_xml_object(url)
|
|
|
|
|
|
|
|
|
|
print(collection_xml.tag)
|
|
|
|
|
|
|
|
|
|
owned_collection_list: list[BoardGame] = []
|
|
|
|
|
|
|
|
|
|
for boardgame_item in collection_xml:
|
2024-08-01 09:50:55 +02:00
|
|
|
boardgame = convert_collection_xml_to_boardgame(boardgame_item)
|
2024-07-31 16:03:24 +02:00
|
|
|
owned_collection_list.append(boardgame)
|
|
|
|
|
|
|
|
|
|
return owned_collection_list
|
|
|
|
|
|
|
|
|
|
|
2024-07-31 15:22:30 +02:00
|
|
|
def load_authenticated_bgg_session(username: str, password: str) -> requests.Session:
|
|
|
|
|
global authenticated_session
|
|
|
|
|
|
2024-07-31 14:54:31 +02:00
|
|
|
login_url = "https://boardgamegeek.com/login/api/v1"
|
|
|
|
|
|
|
|
|
|
post_data = {
|
|
|
|
|
"credentials":{
|
|
|
|
|
"username": username,
|
|
|
|
|
"password": password
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2024-07-31 15:43:33 +02:00
|
|
|
|
|
|
|
|
assert len(authenticated_session.cookies) == 0, 'Session already exists'
|
|
|
|
|
|
2024-07-31 14:54:31 +02:00
|
|
|
login_response = authenticated_session.post(login_url, json=post_data)
|
|
|
|
|
|
2024-07-31 15:43:33 +02:00
|
|
|
assert login_response.status_code == 204, "Login failed!"
|
2024-07-31 16:03:24 +02:00
|
|
|
|
2024-07-31 15:22:30 +02:00
|
|
|
load_authenticated_bgg_session(auth_manager.username, auth_manager.password)
|