Skip to main content

Command Palette

Search for a command to run...

Hashnode tag filtered RSS

Updated
2 min read
P

PHP & JavaScript developer, working in Drupal.

If you just want the code, here it is: https://github.com/BirkAndMe/hashnoderss.
It should be straight forward, check the index.php file to get the gist of things.

Intro

A while back Hashnode released a new version of their public API, and one of the great things about this, is the ability to filter posts by tags. This will allow you to easily make an RSS feed that only show posts with a given tag.
This is important if you write about multiple topics, but want specific posts to appear in a feed aggregator.

I’ve previously written about this, but that code outdated with the new API. I’ve made the code simpler and less abstract, so it’s easier to use.

Quick code breakdown

The code is intentionally kept simple, with 2 core steps in a single file:

  • Fetch the code from the Hashnode API.

    • Using the Symfony Cache (symfony/cache) to cache the request, so the script don’t swarm the API.

    • And Guzzle (guzzlehttp/guzzle) to call the API.

  • Restructure it into RSS, using FeedWriter (mibe/feedwriter) to easily get a valid feed.

Code notes

  • I set a $scheme variable in the settings.php, the $_SERVER[‘REQUEST_SCHEME’] variable is server dependent, so set this to https (or http) to match your configuration.
    The $scheme is used to set the atom link.

  • I’m not using any GraphQL library abstraction, so I’ve simply pasted the entire query into the code as clean text.
    And to get tags working, I’ve injected them directly into the query (I did not escape anything, because it’s all just calling the public Hashnode API anyway).

Outro

I’m not a fan of GQL, but I’ve worked with much worse query languages, and it’s a matter of taste. However the Hashnode API is a breeze to use, and it allows for the exact filtering features I’ve missed previously.

As an added bonus the FeedWriter will (unlike the normal Hashnode RSS) produce RSS that validates without reccomendations.