def build_entry(article, blog_uri):
    title = article["title"]["rendered"]
    slug = article["slug"]
    author = article["_embedded"]["author"][0]
    description = article["excerpt"]["rendered"]
    content = article["content"]["rendered"]
    published = f'{article["date_gmt"]} GMT'
    updated = f'{article["modified_gmt"]} GMT'
    link = f"{blog_uri}/{slug}"

    categories = []

    if "wp:term" in article["_embedded"]:
        for category in article["_embedded"]["wp:term"][1]:
            categories.append(
                dict(term=category["slug"], label=category["name"]))

    entry = FeedEntry()
    entry.title(title)
    entry.description(description)
    entry.content(content)
    entry.author(name=author["name"], email=author["name"])
    entry.link(href=link)
    entry.category(categories)
    entry.published(published)
    entry.updated(updated)

    return entry
    def _build_feed(
        self, blog_url, feed_url, feed_title, feed_description, articles
    ):
        """
        Build the content for the feed
        :blog_url: string blog url
        :feed_url: string url
        :feed_title: string title
        :feed_description: string description
        :param articles: Articles to create feed from
        """
        feed = FeedGenerator()
        feed.generator("Python Feedgen")
        feed.title(feed_title)
        feed.description(feed_description)
        feed.link(href=feed_url, rel="self")

        for article in articles:
            title = article["title"]["rendered"]
            slug = article["slug"]
            author = article["_embedded"]["author"][0]
            description = article["excerpt"]["rendered"]
            content = article["content"]["rendered"]
            published = f'{article["date_gmt"]} GMT'
            updated = f'{article["modified_gmt"]} GMT'
            link = f"{blog_url}/{slug}"

            categories = []

            if "wp:term" in article["_embedded"]:
                for category in article["_embedded"]["wp:term"][1]:
                    categories.append(
                        dict(term=category["slug"], label=category["name"])
                    )

            entry = FeedEntry()
            entry.title(title)
            entry.description(description)
            entry.content(content)
            entry.author(name=author["name"], email=author["name"])
            entry.link(href=link)
            entry.category(categories)
            entry.published(published)
            entry.updated(updated)
            feed.add_entry(entry, order="append")

        return feed
Exemple #3
0
def make_entry(f, yargs, html):
    """Construct a (datetime, FeedEntry)..."""
    from feedgen.entry import FeedEntry

    uri = yargs.feed_link + (str(f.parent) + "/").replace("./", "") + str(
        f.stem) + ".html"
    print(uri)

    title = str(f.stem).replace('_', ' ').title()
    updated = datetime.datetime.fromtimestamp(os.path.getmtime(f),
                                              datetime.timezone.utc)

    # anything YAML based to get better metadata goes here too, I suppose

    y = getyaml(f)
    print(y)

    if "title" in y:
        title = y["title"]

    e = FeedEntry()
    e.link(href=uri)
    e.id(uri)
    e.content(html)
    e.updated(updated)
    e.title(title)
    if "date" in y:
        d = y["date"]  # anything other than the below is super messy
        e.published(
            datetime.datetime(d.year,
                              d.month,
                              d.day,
                              tzinfo=datetime.timezone.utc))

    if "keywords" in y:
        for k in y["keywords"]:
            e.category(category={'term': k, 'scheme': '', 'label': k})

    if "subtitle" in y:
        # close enough
        e.summary(y["subtitle"])
    if "abstract" in y:
        # but this is even better, if it exists
        e.summary(y["abstract"])

    return (updated, e)