浏览代码

[videos] Add management command and fix lookup of series

Colin Powell 1 周之前
父节点
当前提交
6f7f739ca6

+ 0 - 0
vrobbler/apps/videos/management/__init__.py


+ 0 - 0
vrobbler/apps/videos/management/commands/__init__.py


+ 27 - 0
vrobbler/apps/videos/management/commands/find_video.py

@@ -0,0 +1,27 @@
+from django.core.management.base import BaseCommand, CommandError
+from django.core import serializers
+from videos.models import Video, Series, Channel
+import json
+
+class Command(BaseCommand):
+    help = "Find or create a Video by ID and output it as JSON"
+
+    def add_arguments(self, parser):
+        parser.add_argument("video_id", type=str, help="The video ID to find or create")
+
+    def handle(self, *args, **options):
+        instance = Video.find_or_create(options.get("video_id", ""), overwrite=True)
+        data = json.loads(serializers.serialize("json", [instance]))[0]
+
+        # --- Enrich with series model ---
+        if instance.tv_series_id:
+            series_instance = instance.tv_series
+            series_json = json.loads(serializers.serialize("json", [series_instance]))[0]
+            data["series"] = series_json  # new nested field
+
+        if instance.channel_id:
+            channel_instance = instance.channel
+            channel_json = json.loads(serializers.serialize("json", [channel_instance]))[0]
+            data["channel"] = channel_json  # new nested field
+
+        self.stdout.write(json.dumps(data, indent=2))

+ 5 - 3
vrobbler/apps/videos/models.py

@@ -348,8 +348,10 @@ class Video(ScrobblableMixin):
                 setattr(video, k, v)
                 setattr(video, k, v)
             video.save()
             video.save()
 
 
-            video.save_image_from_url(cover)
-            video.genre.add(*genres)
+            if cover:
+                video.save_image_from_url(cover)
+            if genres:
+                video.genre.add(*genres)
         return video
         return video
 
 
     @classmethod
     @classmethod
@@ -369,7 +371,7 @@ class Video(ScrobblableMixin):
                 setattr(video, k, v)
                 setattr(video, k, v)
 
 
             if series_id:
             if series_id:
-                video.tv_series = Series.find_or_create(imdb_id=series_id)
+                video.tv_series = Series.find_or_create(imdb_id=series_id, overwrite=overwrite)
 
 
             video.save()
             video.save()
 
 

+ 4 - 3
vrobbler/apps/videos/sources/imdb.py

@@ -24,11 +24,12 @@ def lookup_video_from_imdb(imdb_id: str) -> VideoMetadata:
         )
         )
         return None
         return None
 
 
+    if imdb_result.type_id.lower() in ["movie", "tvepisode"]:
+        video_metadata.base_run_time_seconds = (
+            imdb_result.runtime * 60
+        )
     video_metadata.imdb_id = imdb_id
     video_metadata.imdb_id = imdb_id
     video_metadata.title = imdb_result.title
     video_metadata.title = imdb_result.title
-    video_metadata.base_run_time_seconds = (
-        imdb_result.runtime * 60
-    )
     video_metadata.year = imdb_result.year
     video_metadata.year = imdb_result.year
     video_metadata.plot = imdb_result.plot.get("en-US", "")
     video_metadata.plot = imdb_result.plot.get("en-US", "")
     video_metadata.imdb_rating = imdb_result.rating
     video_metadata.imdb_rating = imdb_result.rating