From 9f60411c5ee78f7299977607054baa9c9a9ff55b Mon Sep 17 00:00:00 2001 From: Colin Powell Date: Wed, 22 Mar 2023 22:57:18 -0400 Subject: [PATCH] Add scaper to podcast model --- ...cast_cover_podcast_cover_image_and_more.py | 29 +++++++++++++++++++ vrobbler/apps/podcasts/models.py | 25 ++++++++++++++-- 2 files changed, 52 insertions(+), 2 deletions(-) create mode 100644 vrobbler/apps/podcasts/migrations/0010_remove_podcast_cover_podcast_cover_image_and_more.py diff --git a/vrobbler/apps/podcasts/migrations/0010_remove_podcast_cover_podcast_cover_image_and_more.py b/vrobbler/apps/podcasts/migrations/0010_remove_podcast_cover_podcast_cover_image_and_more.py new file mode 100644 index 0000000..87f9b89 --- /dev/null +++ b/vrobbler/apps/podcasts/migrations/0010_remove_podcast_cover_podcast_cover_image_and_more.py @@ -0,0 +1,29 @@ +# Generated by Django 4.1.5 on 2023-03-23 02:57 + +from django.db import migrations, models + + +class Migration(migrations.Migration): + + dependencies = [ + ("podcasts", "0009_podcast_cover"), + ] + + operations = [ + migrations.RemoveField( + model_name="podcast", + name="cover", + ), + migrations.AddField( + model_name="podcast", + name="cover_image", + field=models.ImageField( + blank=True, null=True, upload_to="podcasts/covers/" + ), + ), + migrations.AddField( + model_name="podcast", + name="description", + field=models.TextField(blank=True, null=True), + ), + ] diff --git a/vrobbler/apps/podcasts/models.py b/vrobbler/apps/podcasts/models.py index 9ac5f1a..0f7103c 100644 --- a/vrobbler/apps/podcasts/models.py +++ b/vrobbler/apps/podcasts/models.py @@ -2,13 +2,16 @@ import logging from typing import Dict, Optional from uuid import uuid4 +import requests from django.conf import settings +from django.core.files.base import ContentFile from django.db import models from django.utils.translation import gettext_lazy as _ from django_extensions.db.models import TimeStampedModel - from scrobbles.mixins import ScrobblableMixin +from vrobbler.apps.podcasts.scrapers import scrape_data_from_google_podcasts + logger = logging.getLogger(__name__) BNULL = {"blank": True, "null": True} @@ -27,13 +30,31 @@ class Podcast(TimeStampedModel): producer = models.ForeignKey( Producer, on_delete=models.DO_NOTHING, **BNULL ) + description = models.TextField(**BNULL) active = models.BooleanField(default=True) url = models.URLField(**BNULL) - cover = models.ImageField(upload_to="pdocasts/covers/", **BNULL) + cover_image = models.ImageField(upload_to="podcasts/covers/", **BNULL) def __str__(self): return f"{self.name}" + def scrape_google_podcasts(self, force=False): + podcast_dict = {} + if not self.cover or force: + podcast_dict = scrape_data_from_google_podcasts(self.name) + if podcast_dict: + if not self.producer: + self.producer = podcast_dict.get("producer") + self.description = podcast_dict.get("description") + self.save(update_fields=["producer", "description"]) + + cover_url = podcast_dict.get("image_url") + if (not self.cover_image or force) and cover_url: + r = requests.get(cover_url) + if r.status_code == 200: + fname = f"{self.name}_{self.uuid}.jpg" + self.cover_image.save(fname, ContentFile(r.content), save=True) + class Episode(ScrobblableMixin): COMPLETION_PERCENT = getattr(settings, "PODCAST_COMPLETION_PERCENT", 90)