Ejemplo n.º 1
0
def render_rss_to_file(podcast_id: int) -> str:
    """ Generate rss for Podcast and Episodes marked as "published" """

    logger.info(f"Podcast #{podcast_id}: RSS generation has been started.")
    podcast = Podcast.get_by_id(podcast_id)

    # noinspection PyComparisonWithNone
    episodes = podcast.get_episodes(podcast.created_by).where(
        Episode.status == Episode.STATUS_PUBLISHED,
        Episode.published_at != None,  # noqa: E711
    )
    context = {"episodes": episodes, "settings": settings}
    with open(os.path.join(settings.TEMPLATE_PATH, "rss",
                           "feed_template.xml")) as fh:
        template = Template(fh.read())

    rss_filename = os.path.join(settings.TMP_RSS_PATH,
                                f"{podcast.publish_id}.xml")
    logger.info(
        f"Podcast #{podcast.publish_id}: Generation new file rss [{rss_filename}]"
    )
    with open(rss_filename, "w") as fh:
        result_rss = template.render(podcast=podcast, **context)
        fh.write(result_rss)

    logger.info(f"Podcast #{podcast_id}: RSS generation has been finished.")
    return rss_filename
Ejemplo n.º 2
0
def generate_rss(podcast_id: int) -> Optional[str]:
    """ Allows to download and recreate specific rss (by requested podcast.publish_id) """

    podcast = Podcast.get_by_id(podcast_id)
    logger.info("START rss generation for %s", podcast)

    src_file = podcast_utils.render_rss_to_file(podcast_id)
    filename = os.path.basename(src_file)
    storage = StorageS3()
    result_url = storage.upload_file(src_file,
                                     filename,
                                     remote_path=settings.S3_BUCKET_RSS_PATH)
    if not result_url:
        logger.error("Couldn't upload RSS file to storage. SKIP")
        exit(1)

    podcast.rss_link = result_url
    podcast.save()
    logger.info("RSS file uploaded, podcast record updated")

    if not settings.TEST_MODE:
        logger.info("Removing file [%s]", src_file)
        podcast_utils.delete_file(filepath=src_file)
        src_file = None

    logger.info("FINISH generation")
    return src_file