20February2018

Crosshound was born: Receive newly indexed Crossref items to your feed reader

It's been a while again since my last post and I do intend to catch up on it and to tell you where am I getting with my PhD application and proposal. But that's for another story.

I've been busy in the past few months doing some bibliographical research in the fields of Pentecostal studies, philosophical hermeneutics and biblical interpretation. It seemed to never end, loads of articles and books were threatening to bury me under an unceasing avalanche. Now after several months, I somehow managed to bite through it. However, now that I'm done with scrolling though TOCs and search results, I realized that my research will be growing obsolete with every second, because seconds are the approximate time frame in which new paper is published somewhere.

To prevent this from happening (well, at least partly), I've written a simple script which connects to Crossref API, grabs the most recently indexed items (articles, chapters, monographs) and makes it into a RSS 2.0 feed. The simple PHP magic is below. If you just want to try it, you can type https://selah.diet/hound/crosshound.php?hermeneutics, replacing “hermeneutics” with your own keyword. So if you want to exploit Corssref's newest updates on missiology, just type https://selah.diet/hound/crosshound.php?missiology. Then paste this address into your feed reader. I've debugged it specifically for use with Feedly, but it should be ready to use with any RSS feed reader.

You should know that basically the same thing can be done with Journal TOCs. Jost replace my keyword “pentecostal” in the following string and you will get updates from their database: http://www.journaltocs.ac.uk/api/articles/pentecostal. However, things were much less smooth with this tool than with that of mine. Journal TOCs database is also by all means smaller than Crossref's. But anyway, give it a try!

Should you register a major difficulty with my “Crosshound,” please do not hesitate to write me or to comment below!

Here comes the code. If you want to contemplate better options for API requests, you can consult Crossref API Documentation.

<?php

header('Content-Type: application/rss+xml; charset=UTF-8');

function fetchcr($q)
{

    $opts = array(
        'http' => array(
            'header' => array(
                "Referer: $referer\r\nUser-Agent: Mozilla/5.0 (Macintosh; U; Intel Mac OS X; en) AppleWebKit/522.11.1 (KHTML, like Gecko) Version/3.0.3 Safari/522.12.1\r\n"
            )
        )
    );

    $context = stream_context_create($opts);

    $json = file_get_contents('https://api.crossref.org/works?query.bibliographic=' . urlencode($q) . '&rows=100&order=desc&sort=indexed', false, $context);

    $json = json_decode($json, true);

    if ($json['total-results'] !== 0) {
        $output = $json['message']['items'];

        foreach ($output as $k => $val) {

            $arval = $val;

            $published = @implode('/', $val['published-print']['date-parts'][0]);

            $author = trim($val['author'][0]['family'] . ', ' . $val['author'][0]['given'], ', ');

            $out .= '<item>
  <title>' . strip_tags($arval['title'][0]) . '</title>
  <pubDate>' . date("D, d M Y H:i:s O", strtotime($arval['indexed']['date-time'])) . '</pubDate>
  <guid>' . $arval['URL'] . '</guid>
  <link>' . $arval['URL'] . '</link>
  <description><![CDATA[Author: ' . $author . '<br />In Print: ' . $published . '<br />DOI: <a href="https://sci-hub.tw/' . $arval['DOI'] . '">' . $arval['DOI'] . '</a><br />Type: ' . $arval['type'] . '<br />Category: ' . $arval['category'][0] . '<br />Link: ' . $arval['link'][0]['URL'] . ']]></description>
</item> ';

        }

        return $out;
    } else {
        return '';
    }

}

$qa = str_replace('+', ' ', urldecode($_SERVER['QUERY_STRING']));

echo '<rss version="2.0">
<channel>
    <title>Crossref: ' . $qa . '</title>
    <link>https://selah.diet/hound/crosshound.php?' . $qa . '</link>
    <description>Newest Crossref updates for search query ' . $qa . ' served by Crossref API.</description>';
echo fetchcr($qa);
echo '</channel>
</rss>';

?>

Comment here