Esempio n. 1
0
    def _add_item_to_the_feed(self, feed, item):
        """Performs an 'in-place' update of existing 'published' articles
        in ``feed`` by creating a new entry using the contents from the
        ``item`` being passed.
        This method is invoked by pelican's core.

        :param feed: A PodcastFeed instance.
        :param item: An article (pelican's Article object).

        """
        siteurl = self.settings.get('PODCAST_FEED_SITE_URL', item.settings['SITEURL'])
        if siteurl.startswith('//'):
            siteurl = 'http:' + siteurl

        # Local copy of iTunes attributes to add to the feed.
        items = DEFAULT_ITEM_ELEMENTS.copy()

        # Link to the new article.
        #  http://example.com/episode-01
        items['link'] = '{0}/{1}'.format(self.site_url, item.url)

        # Title for the article.
        #  ex: <title>Episode Title</title>
        items['title'] = Markup(item.title).striptags()

        # Summary for the article. This can be obtained either from
        # a ``:description:`` or a ``:summary:`` directive.
        #  ex: <itunes:summary>In this episode... </itunes:summary>
        if hasattr(item, 'description'):
            items['itunes:summary'] = item.description
        else:
            items['itunes:summary'] = Markup(item.summary).striptags()

        items['description'] = "<![CDATA[{}]]>".format(
            '<img src="{}{}" title="{}" alt="{}" />'.format(
                siteurl, item.image_wide, item.title,
                item.title
            )
        )
        description = unicode(Markup(item.summary)).replace("<html><body>", "")
        description = description.replace("</body></html>", "")
        description += 'Leia o restante do show notes no <a href="{}">site</a>.'.format(
            siteurl + '/' + item.url
        )
        items['description'] += "<![CDATA[{}]]>".format(description)

        # Date the article was last modified.
        #  ex: <pubDate>Fri, 13 Jun 2014 04:59:00 -0300</pubDate>
        items['pubDate'] = rfc2822_date(
            set_date_tzinfo(
                item.modified if hasattr(item, 'modified') else item.date,
                self.settings.get('TIMEZONE', None))
            )

        # Name(s) for the article's author(s).
        #  ex: <itunes:author>John Doe</itunes:author>
        if hasattr(item, 'author'):
            items['itunes:author'] = item.author.name

        # Subtitle for the article.
        #  ex: <itunes:subtitle>My episode subtitle</itunes:subtitle>
        if hasattr(item, 'subtitle'):
            items['itunes:subtitle'] = Markup(item.subtitle).striptags()

        # Ex:
        #  <itunes:image href="http://example.com/Episodio1.jpg" />
        if hasattr(item, 'image'):
            items['itunes:image'] = {
                'href': '{0}{1}'.format(self.site_url, item.image)}

        # Information about the episode audio.
        #  ex: <enclosure url="http://example.com/episode.m4a"
        #   length="872731" type="audio/x-m4a" />
        if hasattr(item, 'podcast'):
            url = "http://archive.org/download/{}/{}.mp3".format(item.podcast, item.podcast)
            enclosure = {'url': url}
            # Include the file size if available.
            if hasattr(item, 'length'):
                enclosure['length'] = item.length
            # Include the audio mime type if available...
            if hasattr(item, 'mimetype'):
                enclosure['type'] = item.mimetype
            else:
                # ... or default to 'audio/mpeg'.
                enclosure['type'] = 'audio/mpeg'
            items['enclosure'] = enclosure

        # Duration for the audio file.
        #  <itunes:duration>7:04</itunes:duration>
        if hasattr(item, 'duration'):
            items['itunes:duration'] = item.duration

        # Unique identifier for the episode.
        # Use a 'guid' if available...
        #  ex: <guid>http://example.com/aae20050615.m4a</guid>
        if hasattr(item, 'guid'):
            items['guid'] = item.guid
        # ... else, use the article's link instead.
        #  ex: <guid>http://example.com/episode-01</guid>
        else:
            items['guid'] = items['link']
        # Add the new article to the feed.
        feed.add_item(**items)
Esempio n. 2
0
    def _add_item_to_the_feed(self, feed, item):
        """Performs an 'in-place' update of existing 'published' articles
        in ``feed`` by creating a new entry using the contents from the
        ``item`` being passed.
        This method is invoked by pelican's core.

        :param feed: A PodcastFeed instance.
        :param item: An article (pelican's Article object).

        """
        # Local copy of iTunes attributes to add to the feed.
        items = DEFAULT_ITEM_ELEMENTS.copy()

        # Link to the new article.
        #  http://example.com/episode-01
        items['link'] = '{0}/{1}'.format(self.site_url, item.url)

        # Title for the article.
        #  ex: <title>Episode Title</title>
        items['title'] = Markup(item.title).striptags()

        # Summary for the article. This can be obtained either from
        # a ``:description:`` or a ``:summary:`` directive.
        #  ex: <itunes:summary>In this episode... </itunes:summary>
        if hasattr(item, 'description'):
            items['itunes:summary'] = item.description
        else:
            items['itunes:summary'] = Markup(item.summary).striptags()

        items['description'] = Markup(item.summary).striptags()

        # Hack: Spotify treats line feeds (LF) as HTML line breaks (<br>)
        # Minify rendered content to avoid this.
        content = Markup(
            htmlmin.minify(item.content,
                           remove_optional_attribute_quotes=False))
        items['content:encoded'] = Markup("<![CDATA[{}]]>").format(content)

        # Date the article was last modified.
        #  ex: <pubDate>Fri, 13 Jun 2014 04:59:00 -0300</pubDate>
        items['pubDate'] = rfc2822_date(
            set_date_tzinfo(
                item.modified if hasattr(item, 'modified') else item.date,
                self.settings.get('TIMEZONE', None)))

        # Name(s) for the article's author(s).
        #  ex: <itunes:author>John Doe</itunes:author>
        if hasattr(item, 'author'):
            items['itunes:author'] = item.author.name

        # Subtitle for the article.
        #  ex: <itunes:subtitle>My episode subtitle</itunes:subtitle>
        if hasattr(item, 'subtitle'):
            items['itunes:subtitle'] = Markup(item.subtitle).striptags()

        # Ex:
        #  <itunes:image href="http://example.com/Episodio1.jpg" />
        if hasattr(item, 'image'):
            items['itunes:image'] = {
                'href': '{0}{1}'.format(self.site_url, item.image)
            }

        # Information about the episode audio.
        #  ex: <enclosure url="http://example.com/episode.m4a"
        #   length="872731" type="audio/x-m4a" />
        if hasattr(item, 'podcast'):
            enclosure = {'url': item.podcast}
            # Include the file size if available.
            if hasattr(item, 'length'):
                enclosure['length'] = item.length
            # Include the audio mime type if available...
            if hasattr(item, 'mimetype'):
                enclosure['type'] = item.mimetype
            else:
                # ... or default to 'audio/mpeg'.
                enclosure['type'] = 'audio/mpeg'
            items['enclosure'] = enclosure

        # Duration for the audio file.
        #  <itunes:duration>7:04</itunes:duration>
        if hasattr(item, 'duration'):
            items['itunes:duration'] = item.duration

        # Unique identifier for the episode.
        # Use a 'guid' if available...
        #  ex: <guid>http://example.com/aae20050615.m4a</guid>
        if hasattr(item, 'guid'):
            items['guid'] = item.guid
        # ... else, use the article's link instead.
        #  ex: <guid>http://example.com/episode-01</guid>
        else:
            items['guid'] = items['link']
        # Add the new article to the feed.
        feed.add_item(**items)
Esempio n. 3
0
    def _add_item_to_the_feed(self, feed, item):
        """Performs an 'in-place' update of existing 'published' articles
        in ``feed`` by creating a new entry using the contents from the
        ``item`` being passed.
        This method is invoked by pelican's core.

        :param feed: A PodcastFeed instance.
        :param item: An article (pelican's Article object).

        """
        # Local copy of iTunes attributes to add to the feed.
        article = DEFAULT_ITEM_ELEMENTS.copy()

        article_values_map = [
            [
                "link",
                lambda calee, item, article: "{0}/{1}".format(
                    calee.site_url, item.url),
            ],
            [
                "title",
                lambda calee, item, article: Markup(item.title).striptags(),
            ],  # NOQA E231
            [
                "itunes:summary",
                lambda calee, item, article: item.description if hasattr(
                    item, "description") else Markup(item.summary).striptags(),
            ],
            [
                "description",
                lambda calee, item, article: "<![CDATA[{}]]>".format(
                    Markup(item.summary)),
            ],
            [
                "pubDate",
                lambda calee, item, article: rfc2822_date(
                    set_date_tzinfo(
                        item.modified
                        if hasattr(item, "modified") else item.date,
                        self.settings.get("TIMEZONE", None),
                    )),
            ],
            ["itunes:author", lambda calee, item, article: item.author.name],
            [
                "itunes:subtitle",
                lambda calee, item, article: Markup(item.subtitle).striptags()
                if hasattr(item, "subtitle") else None,
            ],
            [
                "itunes:image",
                lambda calee, item, article: {
                    "href": "{0}{1}".format(self.site_url, item.image)
                } if hasattr(item, "image") else None,
            ],
            [
                "guid",
                lambda calee, item, article: item.guid
                if hasattr(item, "guid") else article["link"],
            ],
        ]

        def update_article(item, article, *args):
            if len(args) == 2:
                args = args + (None, )
            _key, value, default = args
            try:
                val = value(self, item, article)
                if val:
                    article[_key] = val
            except Exception as e:
                logger.warning("Exception %s", e)

        for article_value_map in article_values_map:
            update_article(item, article, *article_value_map)

        # get file path to podcast attachment file.
        def get_attachment_filepath(settings):
            siteurl = self.settings.get("SITEURL", None)
            content_path = self.settings.get("PATH", None)
            if item.podcast.startswith(siteurl):
                return f"{content_path}/{item.podcast[len(siteurl):]}"

        def get_attachment_url(settings):
            return item.podcast

        enclosure = {"url": get_attachment_url(self.settings)}

        if hasattr(item, "length"):
            enclosure["length"] = item.length
        if hasattr(item, "duration"):
            article["itunes:duration"] = item.duration

        filepath = get_attachment_filepath(self.settings)

        if filepath and os.path.exists(filepath):
            enclosure["length"] = str(os.path.getsize(filepath))
            audiofile = mutagen.File(filepath)

            (enclosure["type"], ) = set(
                audiofile.mime) & set(SUPPORTED_MIME_TYPES)

            article["itunes:duration"] = str(int(audiofile.info.length))

        article["enclosure"] = enclosure

        # Add the new article to the feed.
        feed.add_item(**article)