[videos] Clean up utilities
This commit is contained in:
@ -30,7 +30,7 @@ YOUTUBE_VIDEO_URL = "https://www.youtube.com/watch?v="
|
||||
YOUTUBE_CHANNEL_URL = "https://www.youtube.com/channel/"
|
||||
YOUTUBE_ID_PATTERN = re.compile(r'^[A-Za-z0-9_-]{11}$')
|
||||
|
||||
IMDB_VIDEO_URL = "https://www.imdb.com/title/tt"
|
||||
IMDB_VIDEO_URL = "https://www.imdb.com/title/"
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
BNULL = {"blank": True, "null": True}
|
||||
@ -143,7 +143,7 @@ class Series(TimeStampedModel):
|
||||
if not self.cover_image or (force_update and url):
|
||||
r = requests.get(url)
|
||||
if r.status_code == 200:
|
||||
fname = f"{self.title}_{self.uuid}.jpg"
|
||||
fname = f"{self.name}_{self.uuid}.jpg"
|
||||
self.cover_image.save(fname, ContentFile(r.content), save=True)
|
||||
|
||||
def scrobbles_for_user(self, user_id: int, include_playing=False):
|
||||
@ -198,7 +198,8 @@ class Series(TimeStampedModel):
|
||||
def find_or_create(cls, imdb_id: str, overwrite: bool = True):
|
||||
series, created = cls.objects.get_or_create(imdb_id=imdb_id)
|
||||
|
||||
if not (created or overwrite):
|
||||
if not created and not overwrite:
|
||||
logger.info("Series not created and overwrite=False, returning")
|
||||
return series
|
||||
|
||||
vdict, _, cover, genres = lookup_video_from_imdb(
|
||||
@ -382,9 +383,9 @@ class Video(ScrobblableMixin):
|
||||
@classmethod
|
||||
def find_or_create(cls, source_id: str, overwrite: bool = True) -> "Video":
|
||||
if "tt" in source_id:
|
||||
return cls.get_from_imdb_id(source_id)
|
||||
return cls.get_from_imdb_id(source_id, overwrite)
|
||||
if bool(YOUTUBE_ID_PATTERN.match(source_id)):
|
||||
return cls.get_from_youtube_id(source_id)
|
||||
return cls.get_from_youtube_id(source_id, overwrite)
|
||||
|
||||
#TODO scrobble but without a video obj?
|
||||
logger.warning("Video ID not recognized, not scrobbling")
|
||||
|
||||
@ -1,51 +1,24 @@
|
||||
import logging
|
||||
|
||||
from scrobbles.utils import convert_to_seconds
|
||||
from videos.imdb import lookup_video_from_imdb
|
||||
from videos.models import Series, Video
|
||||
from videos.skatevideosite import lookup_video_from_skatevideosite
|
||||
from videos.models import Video
|
||||
from django.db import IntegrityError
|
||||
#from videos.skatevideosite import lookup_video_from_skatevideosite
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
|
||||
def get_or_create_video(data_dict: dict, post_keys: dict, force_update=False):
|
||||
name_or_id = data_dict.get(post_keys.get("IMDB_ID"), "") or data_dict.get(
|
||||
post_keys.get("VIDEO_TITLE"), ""
|
||||
)
|
||||
def clean_up_videos():
|
||||
videos = Video.objects.filter(imdb_id__isnull=False).exclude(imdb_id__icontains="tt")
|
||||
|
||||
video = Video.objects.filter(imdb_id=name_or_id).first()
|
||||
if video:
|
||||
return video
|
||||
for video in videos:
|
||||
logger.info(f"Fixing imdb_id for {video}")
|
||||
video.imdb_id = "tt" + video.imdb_id
|
||||
try:
|
||||
video.save(update_fields=["imdb_id"])
|
||||
except IntegrityError:
|
||||
new_video = Video.objects.filter(imdb_id="tt" + video.imdb_id).first()
|
||||
video.scrobble_set.all().update(video=new_video)
|
||||
video.delete()
|
||||
|
||||
imdb_metadata = lookup_video_from_imdb(name_or_id)
|
||||
# skatevideosite_metadata = lookup_video_from_skatevideosite(name_or_id)
|
||||
# youtube_metadata = {} # TODO lookup_video_from_youtube(name_or_id)
|
||||
|
||||
video_dict = imdb_metadata
|
||||
if not video_dict:
|
||||
logger.info(
|
||||
"No video found on imdb, skatevideosite or youtube, cannot scrobble",
|
||||
extra={"name_or_id": name_or_id},
|
||||
)
|
||||
return
|
||||
|
||||
video = Video.find_or_create(video_dict.get("imdb_id"))
|
||||
|
||||
if not "overview" in video_dict.keys():
|
||||
video_dict["overview"] = data_dict.get(
|
||||
post_keys.get("OVERVIEW"), None
|
||||
)
|
||||
if not "tagline" in video_dict.keys():
|
||||
video_dict["tagline"] = data_dict.get(
|
||||
post_keys.get("TAGLINE"), None
|
||||
)
|
||||
if not "tmdb_id" in video_dict.keys():
|
||||
video_dict["tmdb_id"] = data_dict.get(
|
||||
post_keys.get("TMDB_ID"), None
|
||||
)
|
||||
|
||||
return video
|
||||
|
||||
|
||||
def get_or_create_video_from_skatevideosite(title: str, force_update: bool=True):
|
||||
return
|
||||
videos = Video.objects.filter(scrobble__isnull=True)
|
||||
videos.delete()
|
||||
|
||||
Reference in New Issue
Block a user