Explorar o código

Clean up code for manaul scrobblign

Colin Powell %!s(int64=2) %!d(string=hai) anos
pai
achega
7db98f0979

+ 7 - 0
vrobbler/apps/scrobbles/constants.py

@@ -8,6 +8,13 @@ LONG_PLAY_MEDIA = {
     "books": "Book",
 }
 
+MANUAL_SCROBBLE_FNS = {
+    "-v": "manual_scrobble_video_game",
+    "-b": "manual_scrobble_book",
+    "-s": "manual_scrobble_sport_event",
+    "-i": "manual_scrobble_video",
+}
+
 
 class AsTsvColumn(Enum):
     ARTIST_NAME = 0

+ 2 - 2
vrobbler/apps/scrobbles/models.py

@@ -639,9 +639,9 @@ class Scrobble(TimeStampedModel):
         scrobble_data.pop("mopidy_status", None)
         scrobble_data.pop("jellyfin_status", None)
         source = scrobble_data["source"]
+        mtype = media.__class__.__name__
         logger.info(
-            f"Creating for {media.id} - {source}",
-            {"scrobble_data": scrobble_data, "media": media},
+            f"[scrobbling] creating for {mtype} {media.id} from {source}"
         )
         return cls.create(scrobble_data)
 

+ 11 - 6
vrobbler/apps/scrobbles/scrobblers.py

@@ -17,6 +17,9 @@ from sports.models import SportEvent
 from videos.models import Video
 from videogames.models import VideoGame
 from books.models import Book
+from vrobbler.apps.books.openlibrary import lookup_book_from_openlibrary
+from vrobbler.apps.sports.thesportsdb import lookup_event_from_thesportsdb
+from vrobbler.apps.videogames.howlongtobeat import lookup_game_from_hltb
 
 logger = logging.getLogger(__name__)
 
@@ -166,7 +169,7 @@ def jellyfin_scrobble_video(data_dict: dict, user_id: Optional[int]):
 
 
 def manual_scrobble_video(imdb_id: str, user_id: int):
-    video = Video.find_or_create({"imdb_id": imdb_id})
+    video = Video.find_or_create(imdb_id)
 
     # When manually scrobbling, try finding a source from the series
     if video.tv_series:
@@ -182,15 +185,16 @@ def manual_scrobble_video(imdb_id: str, user_id: int):
     return Scrobble.create_or_update(video, user_id, scrobble_dict)
 
 
-def manual_scrobble_event(data_dict: dict, user_id: Optional[int]):
-    event = SportEvent.find_or_create(data_dict)
+def manual_scrobble_event(thesportsdb_id: str, user_id: int):
+    data_dict = lookup_event_from_thesportsdb(thesportsdb_id)
 
+    event = SportEvent.find_or_create(data_dict)
     scrobble_dict = build_scrobble_dict(data_dict, user_id)
-
     return Scrobble.create_or_update(event, user_id, scrobble_dict)
 
 
-def manual_scrobble_video_game(data_dict: dict, user_id: Optional[int]):
+def manual_scrobble_video_game(hltb_id: str, user_id: int):
+    data_dict = lookup_game_from_hltb(hltb_id)
     game = VideoGame.find_or_create(data_dict)
 
     scrobble_dict = {
@@ -205,7 +209,8 @@ def manual_scrobble_video_game(data_dict: dict, user_id: Optional[int]):
     return Scrobble.create_or_update(game, user_id, scrobble_dict)
 
 
-def manual_scrobble_book(data_dict: dict, user_id: Optional[int]):
+def manual_scrobble_book(openlibrary_id: str, user_id: int):
+    data_dict = lookup_book_from_openlibrary(openlibrary_id)
     book = Book.find_or_create(data_dict)
 
     scrobble_dict = {

+ 6 - 33
vrobbler/apps/scrobbles/views.py

@@ -33,6 +33,7 @@ from scrobbles.constants import (
     JELLYFIN_AUDIO_ITEM_TYPES,
     JELLYFIN_VIDEO_ITEM_TYPES,
     LONG_PLAY_MEDIA,
+    MANUAL_SCROBBLE_FNS,
 )
 from scrobbles.export import export_scrobbles
 from scrobbles.forms import ExportScrobbleForm, ScrobbleForm
@@ -47,9 +48,6 @@ from scrobbles.scrobblers import (
     jellyfin_scrobble_track,
     jellyfin_scrobble_video,
     manual_scrobble_book,
-    manual_scrobble_event,
-    manual_scrobble_video,
-    manual_scrobble_video_game,
     mopidy_scrobble_podcast,
     mopidy_scrobble_track,
 )
@@ -58,10 +56,6 @@ from scrobbles.tasks import (
     process_lastfm_import,
     process_tsv_import,
 )
-from sports.thesportsdb import lookup_event_from_thesportsdb
-from videogames.howlongtobeat import lookup_game_from_hltb
-
-from books.openlibrary import lookup_book_from_openlibrary
 from scrobbles.utils import (
     get_long_plays_completed,
     get_long_plays_in_progress,
@@ -209,32 +203,12 @@ class ManualScrobbleView(FormView):
     template_name = "scrobbles/manual_form.html"
 
     def form_valid(self, form):
-
         item_str = form.cleaned_data.get("item_id")
-        key = item_str[:2]
-        item_id = item_str[3:]
-        data_dict = None
-
-        if key == "-v":
-            logger.debug(f"Looking for video game with ID {item_id}")
-            data_dict = lookup_game_from_hltb(item_id.replace("-v", ""))
-            if data_dict:
-                manual_scrobble_video_game(data_dict, self.request.user.id)
-
-        if key == "-b":
-            logger.debug(f"Looking for book with ID {item_id}")
-            data_dict = lookup_book_from_openlibrary(item_id.replace("-b", ""))
-            if data_dict:
-                manual_scrobble_book(data_dict, self.request.user.id)
-
-        if key == "-s":
-            logger.debug(f"Looking for sport event with ID {item_id}")
-            data_dict = lookup_event_from_thesportsdb(item_id)
-            if data_dict:
-                manual_scrobble_event(data_dict, self.request.user.id)
-
-        if key == "-i":
-            manual_scrobble_video(item_id, self.request.user.id)
+        logger.debug(f"Looking for scrobblable media with input {item_str}")
+
+        key, item_id = item_str[:2], item_str[3:]
+        scrobble_fn = MANUAL_SCROBBLE_FNS[key]
+        eval(scrobble_fn)(item_id, self.request.user.id)
 
         return HttpResponseRedirect(self.request.META.get("HTTP_REFERER"))
 
@@ -349,7 +323,6 @@ def mopidy_webhook(request):
     try:
         data_dict = json.loads(request.data)
     except TypeError:
-        logger.warning("Received Mopidy data as dict, rather than a string")
         data_dict = request.data
 
     # For making things easier to build new input processors

+ 5 - 1
vrobbler/apps/videos/imdb.py

@@ -10,7 +10,11 @@ logger = logging.getLogger(__name__)
 
 
 def lookup_video_from_imdb(name_or_id: str, kind: str = "movie") -> dict:
-    name_or_id = name_or_id.strip("tt")
+
+    # Very few video titles start with tt, but IMDB IDs often come in with it
+    if name_or_id.startswith("tt"):
+        name_or_id = name_or_id[2:]
+
     video_dict = {}
     imdb_id = None