~thirdplace/feeds

thirdplace feeds - for easier rss feed discovery
feat: add revue
add pixiv feed
fix vimeo channel feed

refs

main
browse  log 

clone

read-only
https://git.sr.ht/~thirdplace/feeds
read/write
git@git.sr.ht:~thirdplace/feeds

You can also use your local clone with git send-email.

#Thirdplace Feeds

This repository contains a bunch of feed (RSS) urls.

They can be used for more pleasant feed discovery.

Patches for missing feeds are very welcomed!

See http://discovery.thirdplace.no for a demo.

#Tutorial

How to install:

This is not a library so just download the file:

wget https://git.sr.ht/~thirdplace/feeds/blob/main/feeds.php

Basic usage:

Here is a snippet which shows how to use feeds.php:

<?php

/**
 * Discover rss feed from url
 */
function discover_feed(string $url)
{
    $feeds = require __DIR__ . '/feeds.php';

    foreach ($feeds as $feed) {

        // This pattern is a ready-made regex OR a url with placeholders (%s)
        $pattern = $feed['pattern'];

        // This is a placeholder url so let's transform it into a regex
        if (strpos($pattern, '%s') !== false) {
            // Escape regex metacharacters
            $pattern = preg_quote($pattern);

            // Replace the %s with a regex capture group
            $pattern = str_replace('%s', '([\w:-]+)', $pattern);
        } else {
            // The pattern is already a regex, so do nothing
        }

        if (preg_match("#^$pattern#i", $url, $m)) {
            array_shift($m);

            // Interpolate the captured group(s) into the feed url
            return sprintf($feed['feed'], ...$m);
        }
    }
    return null;
}

// We want to find the rss feed for this url
$url = 'https://www.youtube.com/channel/UCLzE7jLdn7dqxK-0OdXeuaw';
$result = discover_feed($url);

if ($result) {
    print "$result\n";
} else {
    print "Unable to discover feed url\n";
}

Execute:

$ php ./test.php
https://www.youtube.com/feeds/videos.xml?channel_id=UCLzE7jLdn7dqxK-0OdXeuaw

#Explanation

This repository basically contains a bunch of regexes for which the feed url can be directly produced. Here is an example of what they look like:

[
    'name'          => 'Reddit',
    'description'   => 'This feed contains subreddit submissions',
    'pattern'       => 'https://www.reddit.com/r/%s',
    'feed'          => 'https://www.reddit.com/r/%s.rss',
    'example'       => 'https://www.reddit.com/r/PHP/',
]

The pattern is a regex which captures the relevant string and can be interpolated into feed.

Here is another example for youtube channels:

[
    'name'          => 'Youtube',
    'description'   => 'This feed contains channel videos',
    'pattern'       => 'https://www.youtube.com/channel/%s',
    'feed'          => 'https://www.youtube.com/feeds/videos.xml?channel_id=%s',
    'example'       => 'https://www.youtube.com/channel/UCLG7twDweqlHpyv0EDOjrgw',
]

#How-to

#How to use in javascript

Convert the php array to json and do whatever you like with it:

php -r "print json_encode(require 'feeds.php', JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES);"

#Reference