This article has 61 positive reactions and 24 comments

TL;DR

Using the DEV API the title of this article gets automatically updated every 60 seconds.

Addicted to numbers

I started blogging on DEV only some months ago. You could say, I am quite new to all of this. After writing an article I find myself frequently checking the numbers of reactions and comments. It seems like I am a bit of an addict. And I bet some of you are too.

We share an addiction. We're approval junkies.

Jake Green, Revolver

It is not why I started this and I am sure it is not healthy. So I will try to stop and instead make this a bit of fun. Let's play around with the numbers.

APIs are the future

Back in 2010, I saw a tweet from Smashing Magazine asking about the future of the web. And I answered »APIs«, which is the same answer I would give today — 11 years later.

Let's have fun

It is so much fun working with well-implemented APIs and I was happy to find the DEV API as one of those.

My idea was simple:

  1. Get the properties of this article.
  2. Update the title using two of the properties (positive_reactions_count and comments_count).

The source

I use PHP, which is one of my favorite programming languages.

Get article properties

function getArticleProperties($articleId)
{
    // Prepare URL
    $url = 'https://dev.to/api/articles/' . $articleId;

    // Prepare headers
    $headers = [
        'api-key: 1234567890abcdef',
    ];

    // Prepare method
    $method = 'GET';

    // Execute request
    $curlHandle = curl_init();
    curl_setopt($curlHandle, CURLOPT_URL, $url);
    curl_setopt($curlHandle, CURLOPT_HTTPHEADER, $headers);
    curl_setopt($curlHandle, CURLOPT_CUSTOMREQUEST, $method);
    curl_setopt($curlHandle, CURLOPT_RETURNTRANSFER, true);
    $response  = curl_exec($curlHandle);
    curl_close($curlHandle);

    return json_decode($response, true);
}

Update article title

function updateArticleTitle($articleId, $articleTitle)
{
    // Prepare URL
    $url = 'https://dev.to/api/articles/' . $articleId;

    // Prepare payload
    $payload = json_encode(
        [
            'article' => [
                'title' => $articleTitle,
            ],
        ]
    );

    // Prepare headers
    $headers = [
        'Content-Type: application/json',
        'Content-Length: ' . strlen($payload),
        'api-key: 1234567890abcdef',
    ];

    // Prepare method
    $method = 'PUT';

    // Execute request
    $curlHandle = curl_init();
    curl_setopt($curlHandle, CURLOPT_URL, $url);
    curl_setopt($curlHandle, CURLOPT_HTTPHEADER, $headers);
    curl_setopt($curlHandle, CURLOPT_CUSTOMREQUEST, $method);
    curl_setopt($curlHandle, CURLOPT_POSTFIELDS, $payload);
    curl_setopt($curlHandle, CURLOPT_RETURNTRANSFER, true);
    $response  = curl_exec($curlHandle);
    curl_close($curlHandle);
}

Putting it all together

// Prepare article ID
$articleId = 715066;

// Get article properties using the API
$articleProperties = getArticleProperties($articleId);

// Update article title using the API
updateArticleTitle($articleId, 'This article has ' . $articleProperties['positive_reactions_count'] . ' positive reactions and ' . $articleProperties['comments_count'] . ' comments');

A cronjob is executing this as a CLI script every 60 seconds.

Inspiration

This article is heavily inspired by an awesome YouTube video I saw earlier this year.

Yamen Hadla

Effortless Language Redirection - TYPO3 Extension

Introduction Hello fellow TYPO3 enthusiasts! I had the pleasure of cooperating with my...

Discover full article

Dorien Grönwald

Accessible website: 7 steps to better accessibility and SEO performance

Web accessibility enables people with physical and/or mental disabilities to use the Internet with as...

Discover full article

Malte Riechmann

New git guidelines: We have switched to Conventional Commits

Giving teams as much autonomy as possible is a good idea, but having some company-wide guidelines can...

Discover full article
View all articles