[scrobbles] Allow scrobbling any content via URLs

This commit is contained in:
2024-09-26 21:58:23 -04:00
parent 5a9292e10a
commit 87c078f47d
3 changed files with 39 additions and 1 deletions

View File

@ -22,6 +22,13 @@ MEDIA_END_PADDING_SECONDS = {
"Video": 3600, # 60 min
}
SCROBBLE_CONTENT_URLS = {
"-v": "https://www.imdb.com/title/",
"-s": "https://www.thesportsdb.com/event/",
"-g": "https://boardgamegeek.com/boardgame/",
"-b": "https://www.amazon.com/",
}
EXCLUDE_FROM_NOW_PLAYING = ("GeoLocation",)
MANUAL_SCROBBLE_FNS = {

View File

@ -1,4 +1,4 @@
import json
import re
import logging
from typing import Optional
@ -22,6 +22,10 @@ from sports.thesportsdb import lookup_event_from_thesportsdb
from videogames.howlongtobeat import lookup_game_from_hltb
from videogames.models import VideoGame
from videos.models import Video
from vrobbler.apps.scrobbles.constants import (
MANUAL_SCROBBLE_FNS,
SCROBBLE_CONTENT_URLS,
)
from webpages.models import WebPage
logger = logging.getLogger(__name__)
@ -258,6 +262,24 @@ def manual_scrobble_board_game(bggeek_id: str, user_id: int):
return Scrobble.create_or_update(boardgame, user_id, scrobble_dict)
def manual_scrobble_from_url(url: str, user_id: int) -> Scrobble:
content_key = ""
for key, content_url in SCROBBLE_CONTENT_URLS.items():
if content_url in url:
content_key = key
if not content_key:
return
try:
item_id = re.findall("\d+", url)[0]
except IndexError:
item_id = None
scrobble_fn = MANUAL_SCROBBLE_FNS[content_key]
return eval(scrobble_fn)(item_id, user_id)
def manual_scrobble_webpage(url: str, user_id: int):
webpage = WebPage.find_or_create({"url": url})

View File

@ -32,6 +32,7 @@ from scrobbles.constants import (
LONG_PLAY_MEDIA,
MANUAL_SCROBBLE_FNS,
PLAY_AGAIN_MEDIA,
SCROBBLE_CONTENT_URLS,
)
from scrobbles.export import export_scrobbles
from scrobbles.forms import ExportScrobbleForm, ScrobbleForm
@ -97,6 +98,14 @@ class RecentScrobbleList(ListView):
user = self.request.user
if user.is_authenticated:
if scrobble_url := self.request.GET.get("scrobble_url"):
for content_url in SCROBBLE_CONTENT_URLS.values():
if content_url in scrobble_url:
scrobble = manual_scrobble_from_url(
scrobble_url, self.request.user.id
)
return HttpResponseRedirect(
scrobble.redirect_url(user.id)
)
scrobble = manual_scrobble_webpage(
scrobble_url, self.request.user.id
)