소스 검색

[scrobbles] Allow scrobbling any content via URLs

Colin Powell 9 달 전
부모
커밋
87c078f47d
3개의 변경된 파일39개의 추가작업 그리고 1개의 파일을 삭제
  1. 7 0
      vrobbler/apps/scrobbles/constants.py
  2. 23 1
      vrobbler/apps/scrobbles/scrobblers.py
  3. 9 0
      vrobbler/apps/scrobbles/views.py

+ 7 - 0
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 = {

+ 23 - 1
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})
 

+ 9 - 0
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
                 )