艁adowanie

Simple automation of TDBNews [ENG 馃嚞馃嚙]

馃嚨馃嚤 Przejd藕 do polskiej wersji tego wpisu / Go to polish version of this post

For several weeks now, I have been publishing a series of Sunday posts called , in which I share interesting things I came across during the week. Manually collecting these, in my opinion, interesting articles would be a very tedious and highly inefficient task that I would have to repeat weekly, dedicating part of my Sunday time to it, which I could spend on other activities. That’s why I decided to automate it in a very simple way. The operating principle is trivial, and for clarity, I will present it in bullet points.

  1. Every day, I browse through my RSS sources (using the Reeder application), my timeline on Mastodon, and Reddit, which are my three main windows to the world of information.
  2. When I come across an article that seems interesting, I save it to the Read Later list, which I usually revisit in the evening.
  3. Then, I share these truly interesting things on my Mastodon profile with a special hashtag .
  4. A PHP script monitoring the RSS feed of my Mastodon profile picks up these toots and saves them to a text file.
  5. At the end of the week (on Sunday), I run a second script that creates a ready-to-use post code from the collected toots, following the notation used by WordPress. In practice, it consists of a static introduction and conclusion, which are the same in every TDBNews post, and a variable part in the middle where I embed the collected toots as iframe frames.
  6. All that’s left is to give the post a title, which simply involves providing the current date, and publish it.

Examples of such posts can be seen by going here. And in this post, I’ll show you behind the scenes how these scripts, which I use to implement this simple automation, look like.

Script collecting my toots tagged with

As usual, I’ll explain its operation through comments placed inside the code. Here, familiarity with the content of the post in which I showed how I made a bot for publishing news from popular portals on Mastodon may also come in handy.

<?php
    header('Content-Type: text/html; charset=utf-8');

    // URL TO MY MASTODON PROFILE
    $url = "https://mastodon.tomaszdunia.pl/@to3k.rss";

    // FILE WITH ALREADY FOUND TOOTS
    $file_all = file_get_contents("tdbnews_all.txt");

    // LOAD A XML FILE (RSS FEED)
    $feeds = simplexml_load_file($url);

    // SETTING TIME ZONE
    date_default_timezone_set("Europe/Warsaw");

    // DATE OF MONDAY THIS WEEK TO FILTER EVERYTHING BEFORE THAT DATE
    $monday = strtotime("Monday this week");
    $monday = date("Y-m-d", $monday);
    
    // IF FEED IS NOT EMPTY
    if(!empty($feeds))
    {
        // SPLIT FEED INTO SEPARATE ITEMS (TOOTS)
        foreach ($feeds->channel->item as $item)
        {
            // CONVERT LINK TO A STRING VARIABLE (WITHOUT THAT STR_CONTAINS GIVES ERROR)
            $link = strval($item->link);

            // GET PUBLISHING DATE AND CHANGE IT'S FORMAT TO YYYY-MM-DD
            $pubDate = $item->pubDate;
            $pubDate = strtotime($pubDate);
            $pubDate = date("Y-m-d", $pubDate);

            // GET DESCRIPTION
            $description = strval($item->description);

            if(str_contains($file_all, $link) OR $pubDate < $monday OR !str_contains($description, "https://mastodon.tomaszdunia.pl/tags/TDBNews"))
            {
                // IF LINK IS IN A FILE (IS ALREADY PROCESSED)
                // OR IF PUBLICATION DATE IS BEFORE MONDAY THIS WEEK (IS TOO OLD)
                // OR IF TOOT DOES NOT CONTAIN HASHTAG TDBNEWS (IS ABOUT SOMETHING ELSE)
                // THEN SKIP IT AND GO TO NEXT ONE
                continue;
            }
            else
            {
                // IF ALL CONDITIONS MET
                // PUSH LINK INTO A VARIABLE WITH QUALIFYING TOOTS
                $file_all .= $pubDate.";".$link."\n";
            }
        }
    }

    // UPDATE THE FILE WITH LIST OF QUALIFYING TOOTS
    file_put_contents("tdbnews_all.txt", $file_all);
?>

Script generating a ready-made WordPress post

Just like in the previous code, everything is explained in the comments here as well.

<?php
    // LOAD FILE WITH ALL TDBNEWS TOOTS
    $file_all = file_get_contents("tdbnews_all.txt");

    // DIVIDE FILE CONTENT INTO SEPARATE LINES
    $explode_file = explode("\n", $file_all);

    // SET TIME ZONE
    date_default_timezone_set("Europe/Warsaw");

    // DATE OF MONDAY THIS WEEK TO FILTER EVERYTHING BEFORE THAT DATE
    $monday = strtotime("Monday this week");
    $monday = date("Y-m-d", $monday);

    // DECLARATION OF AN ARRAY WITH THIS WEEK'S NEWS
    $news_array = array();
    
    // GO THROUGH ALL LINES OF FILE ONE BY ONE
    foreach($explode_file as $line)
    {
        // IF LINE IS NOT EMPTY
        if(!empty($line))
        {
            // SEPARATE PUBLISHING DATE AND LINK
            $explode_line = explode(";", $line);
            // IF PUBLISHING DATE IF NOT BEFORE MONDAT THIS WEEK
            if($explode_line[0] >= $monday)
            {
                // PUSH THIS TOOT INTO ARRAY WITH NEWS FOR THIS WEEK
                array_push($news_array, ['pubDate' => $explode_line[0], 'link' => $explode_line[1]]);
            }
        }
    }
    
    // REVERSE TABLE (SORT TOOTS FROM OLDER TO NEWER)
    $news_array = array_reverse($news_array);
?>

<!-- SECTION WITH RESULT, STATIC INTRODUCTION, PHP CODE WITH FOREACH FUNCTION WHICH RESULT IS PRINT ALL TOOTS EMBEDED WITH IFRAME, STATIC ENDING -->
<!-- PLAINTEXT ELEMENT IS USED TO PRINT HTML CODE IN RAW FORM -->
<plaintext>
<!-- wp:columns -->
<div class="wp-block-columns"><!-- wp:column -->
<div class="wp-block-column"><!-- wp:paragraph -->
<p>馃嚨馃嚤 <em>#TDBNews</em> to nazwa pochodz膮ca od <em>Tomasz Dunia Blog News</em>. Pod tak膮 nazw膮 co niedziel臋 publikuj臋 zbi贸r ciekawych wiadomo艣ci na jakie uda艂o mi si臋 natrafi膰 w ubieg艂ym tygodniu. Zdecydowana wi臋kszo艣膰 linkowanych artyku艂贸w b臋dzie angloj臋zyczna, bo wszystkie 藕r贸d艂a polskoj臋zyczne, kt贸re 艣ledz臋, s膮 za <em>paywall'ami</em>.</p>
<!-- /wp:paragraph --></div>
<!-- /wp:column -->

<!-- wp:column -->
<div class="wp-block-column"><!-- wp:paragraph -->
<p>馃嚞馃嚙 <em>#TDBNews</em> is a name coming from <em>Tomasz Dunia Blog News</em>. Under this name, every Sunday I publish a collection of interesting news that I came across in the previous week. The vast majority of linked articles will be in English, because all the Polish-language sources, I follow, are behind <em>paywalls</em>.</p>
<!-- /wp:paragraph --></div>
<!-- /wp:column --></div>
<!-- /wp:columns -->

<!-- wp:heading -->
<h2 class="wp-block-heading">W tym tygodniu znalaz艂em / This week I found 馃憞</h2>
<!-- /wp:heading -->
<?php
    foreach($news_array as $news)
    {
        echo "
<!-- wp:html -->
<iframe src=\"".$news['link']."/embed\" class=\"mastodon-embed\" style=\"max-width: 100%; border: 0\" width=\"100%\" allowfullscreen=\"allowfullscreen\"></iframe><script src=\"https://mastodon.tomaszdunia.pl/embed.js\" async=\"async\"></script>
<!-- /wp:html -->
        ";
    }
?>

<!-- wp:columns -->
<div class="wp-block-columns"><!-- wp:column -->
<div class="wp-block-column"><!-- wp:paragraph -->
<p>馃嚨馃嚤 Jak widzisz to zestawienie powstaje poprzez osadzenie toot贸w (inaczej post贸w), kt贸re opublikowa艂em w poprzednim tygodniu na <em>Mastodonie</em>. S膮 one oznaczone specjalnym hashtagiem. To oznacza, 偶e informacj臋 o tych tre艣ciach mo偶esz uzyska膰 jeszcze przed publikacj膮 tego zestawienia. Wystarczy 艣ledzi膰 <a href="https://mastodon.tomaszdunia.pl/@to3k">m贸j profil na <em>Mastodonie</em></a> lub sam <a href="https://mastodon.tomaszdunia.pl/tags/TDBNews">hashtag <em>#TDBNews</em></a>.</p>
<!-- /wp:paragraph --></div>
<!-- /wp:column -->

<!-- wp:column -->
<div class="wp-block-column"><!-- wp:paragraph -->
<p>馃嚞馃嚙 As you can see, this compilation is created by embedding toots (also known as posts) that I published last week on <em>Mastodon</em>. They are marked with a special hashtag. This means that you can access information about these contents even before this compilation is published. Just follow <a href="https://mastodon.tomaszdunia.pl/@to3k">my profile on <em>Mastodon</em></a> or the <a href="https://mastodon.tomaszdunia.pl/tags/TDBNews">hashtag <em>#TDBNews</em></a> itself.</p>
<!-- /wp:paragraph --></div>
<!-- /wp:column --></div>
<!-- /wp:columns -->

Summary

Wasn’t it easy? In my opinion, it was not only simple but also worth the effort! How so? I have a principle that any automation of tasks, that are intellectually undemanding yet repetitive, makes sense and ultimately saves a lot of time. Let’s take the example of TDBNews. Creating such a summary manually would take me at least 20 minutes every week, but with the automation described above, it takes me no more than 3 minutes. Let’s consider those 15 minutes per week. In a year, we have 52 weeks, so I gain 780 minutes, which is 13 hours! That’s over half a day that I can spend with my family or even use to create another post for this blog.


If you liked this post then you can support me! 馃檪

Tomasz Dunia

馃嚨馃嚤 Z wykszta艂cenia Mechatronik. Z zawodu G艂贸wny Konstruktor w PAK-PCE Polski Autobus Wodorowy (Neso Bus). Po pracy Ojciec Roku. W nocy Wannabe Programista. Wszystko to daje przepis na zwyk艂ego nerda :) 馃嚞馃嚙 Mechatronics by education. By profession Chief Constructor in PAK-PCE Polish Hydrogen Bus (Neso Bus). After work Father of the Year. At night Wannabe Programmer. All this gives a recipe for an ordinary nerd :)

svg

Co my艣lisz?

Poka偶 komentarze / Napisz komentarz

Odpowiedz

svg
Szybka nawigacja
  • 01

    Simple automation of TDBNews [ENG 馃嚞馃嚙]