2022-11-19

DENO - Mastodon RSS Feed

Mastodon, a decentralized microblogging platform, provides every account with RSS feed updates. For instance, you can get the RSS Feed attached to my Mastodon profile https://fosstodon.org/@seve_py by appending .rss to the profile's URL https://fosstodon.org/@seve_py.rss.

Scraping Mastodon RSS Feed

The Python script below can generate an HTML page from a Mastodon RSS channel (XML), it can be automated with a scheduled task to regenerate periodically the HTML file.


    #!/usr/bin/env python3
    # filename (server): rss2html2.py
    import requests
    from bs4 import BeautifulSoup
    url = "https://fosstodon.org/@seve_py.rss"
    resp=requests.get(url)
    soup=BeautifulSoup(resp.content,features="xml")
    items=soup.findAll('item')
    feed_items=[]
    for item in items:
        feed_item={}
        feed_item['description']=item.description.text
        feed_item['link']=item.link.text
        feed_item['pubDate']=item.pubDate.text
        feed_items.append(feed_item)

    html = []
    html.append("""
    <html>
    <head>
    </head>
    <body>
            """)
    template = u"""
    <h2 class='title'>
    <a class='link' href='{link}'>{link}</a>
    </h2>
    <span class='description'>Description: {description}</span>
    """
    for i in feed_items:
        html.append(template.format(link = i["link"], description = i["description"]))

    html.append("</body></html>")

    with open("/home/tessarinseve/staticweb/mastodon.html", 'w') as file_object:
        file_object.write("\n".join(html))

DENO Remote Javascript

This Javascript file can also be located on a remote server's static directory and run locally from the command line as shown in the video below. The fetch() method initiates a network request to the same server and returns a promise that resolves after the response is available. The text method then resolves the HTML code produced by the previous Python script. Print to the standard output is obtained by a string literal with interpolation expressions.

    // filename (server staticdeno): rssmastodon.ts
    import { setup, tw } from "https://esm.sh/twind@0.16.16";
    import { getStyleTag, virtualSheet } from "https://esm.sh/twind@0.16.16/sheets";

    const sheet = virtualSheet();

    setup({
      theme: {
        fontFamily: {
          sans: ["Helvetica", "sans-serif"],
          serif: ["Times", "serif"],
        },
      },
      sheet,
    });

    function renderBody() {
      return `
        <h1 class="${tw`text(3xl blue-500)`}">My Mastodon RSS Feed</h1>
      `;
    }

    function ssr(html) {
      sheet.reset();
      const body = renderBody();
      const styleTag = getStyleTag(sheet);

      return `<!DOCTYPE html>
        <html lang="en">
          <head>
            <title>My Mastodon RSS Feed</title>
            ${styleTag}
          </head>
          <body>
            ${body}
            ${html}
          </body>
        </html>`;
    }
    const resp = await fetch("https://tessarinseve.pythonanywhere.com/staticweb/mastodon.html")
    const html = await resp.text()
    console.log(ssr(html))

Vim Filter (2024-W27-7)

In Deno, you can use the --quiet flag to suppress downloading progress messages and module printouts to stdout. This flag helps keep the output clean by only displaying essential information.

It can also be use as a filter from Vim's command line.

   :r!  deno.exe run --quiet -A -r https://tessarinseve.pythonanywhere.com/staticdeno/rssmastodon.ts