Clean up board game scrobbling
This commit is contained in:
@ -1,8 +1,11 @@
|
|||||||
|
import logging
|
||||||
from typing import Optional
|
from typing import Optional
|
||||||
|
|
||||||
import requests
|
import requests
|
||||||
from bs4 import BeautifulSoup
|
from bs4 import BeautifulSoup
|
||||||
|
|
||||||
|
logger = logging.getLogger(__name__)
|
||||||
|
|
||||||
SEARCH_ID_URL = (
|
SEARCH_ID_URL = (
|
||||||
"https://boardgamegeek.com/xmlapi/search?search={query}&exact=1"
|
"https://boardgamegeek.com/xmlapi/search?search={query}&exact=1"
|
||||||
)
|
)
|
||||||
@ -51,11 +54,14 @@ def lookup_boardgame_from_bgg(lookup_id: str) -> dict:
|
|||||||
|
|
||||||
try:
|
try:
|
||||||
bgg_id = int(lookup_id)
|
bgg_id = int(lookup_id)
|
||||||
|
logger.debug(f"Using BGG ID {bgg_id} to find board game")
|
||||||
except ValueError:
|
except ValueError:
|
||||||
title = lookup_id
|
title = lookup_id
|
||||||
|
logger.debug(f"Using title {title} to find board game")
|
||||||
|
|
||||||
if not bgg_id:
|
if not bgg_id:
|
||||||
bgg_id = lookup_boardgame_id_from_bgg(title)
|
bgg_id = lookup_boardgame_id_from_bgg(title)
|
||||||
|
|
||||||
url = GAME_ID_URL.format(id=bgg_id)
|
url = GAME_ID_URL.format(id=bgg_id)
|
||||||
r = requests.get(url, headers=headers)
|
r = requests.get(url, headers=headers)
|
||||||
if r.status_code == 200:
|
if r.status_code == 200:
|
||||||
@ -68,6 +74,7 @@ def lookup_boardgame_from_bgg(lookup_id: str) -> dict:
|
|||||||
seconds_to_play = int(minutes) * 60
|
seconds_to_play = int(minutes) * 60
|
||||||
|
|
||||||
game_dict = {
|
game_dict = {
|
||||||
|
"bggeek_id": bgg_id,
|
||||||
"title": take_first(soup.findAll("name", primary="true")),
|
"title": take_first(soup.findAll("name", primary="true")),
|
||||||
"description": take_first(soup.findAll("description")),
|
"description": take_first(soup.findAll("description")),
|
||||||
"year_published": take_first(soup.findAll("yearpublished")),
|
"year_published": take_first(soup.findAll("yearpublished")),
|
||||||
|
|||||||
@ -11,6 +11,7 @@ from django.db import models
|
|||||||
from django.urls import reverse
|
from django.urls import reverse
|
||||||
from django_extensions.db.models import TimeStampedModel
|
from django_extensions.db.models import TimeStampedModel
|
||||||
from scrobbles.mixins import ScrobblableMixin
|
from scrobbles.mixins import ScrobblableMixin
|
||||||
|
from vrobbler.apps.boardgames.bgg import lookup_boardgame_id_from_bgg
|
||||||
|
|
||||||
logger = logging.getLogger(__name__)
|
logger = logging.getLogger(__name__)
|
||||||
BNULL = {"blank": True, "null": True}
|
BNULL = {"blank": True, "null": True}
|
||||||
@ -83,10 +84,10 @@ class BoardGame(ScrobblableMixin):
|
|||||||
|
|
||||||
def fix_metadata(self, data: dict = {}, force_update=False) -> None:
|
def fix_metadata(self, data: dict = {}, force_update=False) -> None:
|
||||||
|
|
||||||
if not self.bggeek_id or force_update:
|
if not self.published_date or force_update:
|
||||||
|
|
||||||
if not data:
|
if not data:
|
||||||
data = get_game_by_id_from_bgg(self.bggeek_id)
|
data = lookup_boardgame_from_bgg(str(self.bggeek_id))
|
||||||
|
|
||||||
cover_url = data.pop("cover_url")
|
cover_url = data.pop("cover_url")
|
||||||
year = data.pop("year_published")
|
year = data.pop("year_published")
|
||||||
|
|||||||
@ -228,6 +228,10 @@ def manual_scrobble_book(openlibrary_id: str, user_id: int):
|
|||||||
def manual_scrobble_board_game(bggeek_id: str, user_id: int):
|
def manual_scrobble_board_game(bggeek_id: str, user_id: int):
|
||||||
boardgame = BoardGame.find_or_create(bggeek_id)
|
boardgame = BoardGame.find_or_create(bggeek_id)
|
||||||
|
|
||||||
|
if not boardgame:
|
||||||
|
logger.error(f"No board game found for ID {bggeek_id}")
|
||||||
|
return
|
||||||
|
|
||||||
scrobble_dict = {
|
scrobble_dict = {
|
||||||
"user_id": user_id,
|
"user_id": user_id,
|
||||||
"timestamp": timezone.now(),
|
"timestamp": timezone.now(),
|
||||||
|
|||||||
@ -43,7 +43,6 @@
|
|||||||
</div>
|
</div>
|
||||||
<div class="row">
|
<div class="row">
|
||||||
<p>{{object.scrobble_set.count}} scrobbles</p>
|
<p>{{object.scrobble_set.count}} scrobbles</p>
|
||||||
<p>{{object.scrobble_set.last.long_play_seconds|natural_duration}}{% if object.scrobble_set.last.long_play_complete %} and completed{% else %} spent playing{% endif %}</p>
|
|
||||||
<p>
|
<p>
|
||||||
{% if object.scrobble_set.last.long_play_complete == True %}
|
{% if object.scrobble_set.last.long_play_complete == True %}
|
||||||
<a href="">Play again</a>
|
<a href="">Play again</a>
|
||||||
@ -60,18 +59,14 @@
|
|||||||
<thead>
|
<thead>
|
||||||
<tr>
|
<tr>
|
||||||
<th scope="col">Date</th>
|
<th scope="col">Date</th>
|
||||||
<th scope="col">Completed</th>
|
<th scope="col">Publisher</th>
|
||||||
<th scope="col">Duration</th>
|
|
||||||
<th scope="col">Platforms</th>
|
|
||||||
</tr>
|
</tr>
|
||||||
</thead>
|
</thead>
|
||||||
<tbody>
|
<tbody>
|
||||||
{% for scrobble in object.scrobble_set.all|dictsortreversed:"timestamp" %}
|
{% for scrobble in object.scrobble_set.all|dictsortreversed:"timestamp" %}
|
||||||
<tr>
|
<tr>
|
||||||
<td>{{scrobble.timestamp}}</td>
|
<td>{{scrobble.timestamp}}</td>
|
||||||
<td>{% if scrobble.long_play_complete == True %}Yes{% endif %}</td>
|
<td>{{scrobble.media_obj.publisher}}</td>
|
||||||
<td>{% if scrobble.in_progress %}Now playing{% else %}{{scrobble.playback_position_seconds|natural_duration}}{% endif %}</td>
|
|
||||||
<td>{% for platform in scrobble.video_game.platforms.all %}<a href="{{platform.get_absolute_url}}">{{platform}}</a>{% if not forloop.last %}, {% endif %}{% endfor %}</td>
|
|
||||||
</tr>
|
</tr>
|
||||||
{% endfor %}
|
{% endfor %}
|
||||||
</tbody>
|
</tbody>
|
||||||
|
|||||||
@ -45,7 +45,9 @@
|
|||||||
</div>
|
</div>
|
||||||
<div class="row">
|
<div class="row">
|
||||||
<p>{{object.scrobble_set.count}} scrobbles</p>
|
<p>{{object.scrobble_set.count}} scrobbles</p>
|
||||||
|
{% if object.scrobble_set.last.long_play_seconds %}
|
||||||
<p>{{object.scrobble_set.last.long_play_seconds|natural_duration}}{% if object.scrobble_set.last.long_play_complete %} and completed{% else %} spent playing{% endif %}</p>
|
<p>{{object.scrobble_set.last.long_play_seconds|natural_duration}}{% if object.scrobble_set.last.long_play_complete %} and completed{% else %} spent playing{% endif %}</p>
|
||||||
|
{% endif %}
|
||||||
<p>
|
<p>
|
||||||
{% if object.scrobble_set.last.long_play_complete == True %}
|
{% if object.scrobble_set.last.long_play_complete == True %}
|
||||||
<a href="">Play again</a>
|
<a href="">Play again</a>
|
||||||
|
|||||||
Reference in New Issue
Block a user