diff --git a/vrobbler/apps/scrobbles/constants.py b/vrobbler/apps/scrobbles/constants.py index 8089b4e..984f1b7 100644 --- a/vrobbler/apps/scrobbles/constants.py +++ b/vrobbler/apps/scrobbles/constants.py @@ -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 = { diff --git a/vrobbler/apps/scrobbles/scrobblers.py b/vrobbler/apps/scrobbles/scrobblers.py index 83f56ad..d70606a 100644 --- a/vrobbler/apps/scrobbles/scrobblers.py +++ b/vrobbler/apps/scrobbles/scrobblers.py @@ -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}) diff --git a/vrobbler/apps/scrobbles/views.py b/vrobbler/apps/scrobbles/views.py index b91c1d5..3647ed8 100644 --- a/vrobbler/apps/scrobbles/views.py +++ b/vrobbler/apps/scrobbles/views.py @@ -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 )