APIs for Librarians

Helping you to help yourself in helping your patrons. Or something.

RSS Feed Creation for Repository Items


This page courtesy of: Andrew Lechlak

Description

The goal was to create an RSS feed from CONTENTdm using a CSV list of images that were timed based on popular cultural events. I.E. Hispanic Heritage month, New Years Day, Spring, etc. So why use a CSV? Well a database requires someone with IT knowledge to build. A CSV is created inside a basic spreadsheet software program like Excel. If we can build the application so it is accessible to more people, then more people can create and iterate on this idea for their collections.

We used the ContentDM APIs and IIIF (International Image Interoperability Framework) for building our RSS Feed. We were then able to leverage our RSS feed to work with Zapier to automatically share items to various social media platforms!

Screenshot

RSS Feed Creation for Repository Items screenshot

More details

This guide focuses on using the ContentDM API with a spreadsheet to enable less technical users to maintain collections. For more information, and how to work with Zapier to automate item promotion, please see the complete instructions from Open Toledo.

The Code

CSV

1
2
3
4
  Date,CDM# MD,CDM# Image,Notes
  10/4/2018,304,304,
  10/5/2018,12642,12641,
  10/6/2018,12644,12643

PHP

Notes for implementation:
The code itself:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
 <?php
//csv file
$csvfile = "ImageShareSchedule.csv"; //change this

//cdm data
$cdmcollection = "p16007coll33"; //change this
$cdmserver = "server16007"; //change this
$basecdmurl = "http://ohiomemory.org"; //change this - probably too https://$cdmserver.contentdm.oclc.org/ - you can view this by navigating to your collection and looking at the URL

//bit.ly
$login = "bitly-login"; //change this
$appkey = "bitly-app-key"; //change this
$format = "txt";

function get_bitly_short_url($url, $login, $appkey, $format = 'txt')
{
    $connectURL = 'http://api.bit.ly/v3/shorten?login=' . $login . '&apiKey=' . $appkey . '&uri=' . urlencode($url) . '&format=' . $format;
    //echo $connectURL;

    $ch2 = curl_init();
    curl_setopt($ch2, CURLOPT_SSL_VERIFYPEER, false);
    curl_setopt($ch2, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch2, CURLOPT_URL, $connectURL);
    $result = curl_exec($ch2);
    curl_close($ch2);

    $obj = json_decode($result, true);
    //echo $result;
    return $result;

}

$today = getdate();
$d = $today['mday'];
$m = $today['mon'];
$y = $today['year'];
$currentdate = "$m/$d/$y";
//echo $currentdate;
// echo "<br/>";

//$row = 4;
//$column = 2;
//echo $csv[$row][$column];

$csv = array();
$x = 0;

if (($handle = fopen($csvfile, "r")) !== false) {
    while (($data = fgetcsv($handle, 1000, ",")) !== false) {
        $csv[] = $data;
        $rdate = $csv[$x][0];
        //echo "x = $x | date = $rdate";
        //echo "</br>";

        if ($currentdate == $rdate) {
            //echo "x = $x | date = $rdate";
            //echo "</br>";
            $cdmmdpointer = $csv[$x][1];
            $cdmimgpointer = $csv[$x][2];
            $cdmimg = "$basecdmurl/digital/iiif/$cdmcollection/$cdmimgpointer/full/400,200/0/default.jpg";
            $cdmmd = "https://$cdmserver.contentdm.oclc.org/dmwebservices/index.php?q=dmGetItemInfo/$cdmcollection/$cdmmdpointer/json";

            $ch2 = curl_init();
            curl_setopt($ch2, CURLOPT_SSL_VERIFYPEER, false);
            curl_setopt($ch2, CURLOPT_RETURNTRANSFER, true);
            curl_setopt($ch2, CURLOPT_URL, "https://$cdmserver.contentdm.oclc.org/dmwebservices/index.php?q=dmGetItemInfo/$cdmcollection/$cdmmdpointer/json");
            $result2 = curl_exec($ch2);
            curl_close($ch2);

            $obj2 = json_decode($result2, true);
//echo $ojb2;
            $title = $obj2['title'];
//echo $title;
            //$title = mysql_real_escape_string($title);

            $cdmlink = "$basecdmurl/cdm/singleitem/collection/$cdmcollection/id/$cdmimgpointer/rec/1";

            //Keep bit.ly
            $bitlycdmlink = get_bitly_short_url($cdmlink, $login, $appkey);

            //Remove bit.ly
            //$bitlycdmlink = $cdmlink;

//RSS FEED CREATION
            echo '<?xml version="1.0" encoding="UTF-8" ?>
<rss version="2.0">
<channel>
<title>' . $title . '</title>
<link>' . $bitlycdmlink . '</link>
<description>' . $cdmimg . '</description>
<item>
<title>' . $title . '</title>
<link>' . $bitlycdmlink . '</link>
<description>' . $cdmimg . '</description>
</item>
</channel>
</rss>';

        }
        $x++;
    }
}

fclose($handle);