Add scaper to podcast model

This commit is contained in:
2023-03-22 22:57:18 -04:00
parent dd66774bda
commit 9f60411c5e
2 changed files with 52 additions and 2 deletions

View File

@ -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),
),
]

View File

@ -2,13 +2,16 @@ import logging
from typing import Dict, Optional from typing import Dict, Optional
from uuid import uuid4 from uuid import uuid4
import requests
from django.conf import settings from django.conf import settings
from django.core.files.base import ContentFile
from django.db import models from django.db import models
from django.utils.translation import gettext_lazy as _ from django.utils.translation import gettext_lazy as _
from django_extensions.db.models import TimeStampedModel from django_extensions.db.models import TimeStampedModel
from scrobbles.mixins import ScrobblableMixin from scrobbles.mixins import ScrobblableMixin
from vrobbler.apps.podcasts.scrapers import scrape_data_from_google_podcasts
logger = logging.getLogger(__name__) logger = logging.getLogger(__name__)
BNULL = {"blank": True, "null": True} BNULL = {"blank": True, "null": True}
@ -27,13 +30,31 @@ class Podcast(TimeStampedModel):
producer = models.ForeignKey( producer = models.ForeignKey(
Producer, on_delete=models.DO_NOTHING, **BNULL Producer, on_delete=models.DO_NOTHING, **BNULL
) )
description = models.TextField(**BNULL)
active = models.BooleanField(default=True) active = models.BooleanField(default=True)
url = models.URLField(**BNULL) url = models.URLField(**BNULL)
cover = models.ImageField(upload_to="pdocasts/covers/", **BNULL) cover_image = models.ImageField(upload_to="podcasts/covers/", **BNULL)
def __str__(self): def __str__(self):
return f"{self.name}" 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): class Episode(ScrobblableMixin):
COMPLETION_PERCENT = getattr(settings, "PODCAST_COMPLETION_PERCENT", 90) COMPLETION_PERCENT = getattr(settings, "PODCAST_COMPLETION_PERCENT", 90)