View Full Version : RSS to Javascript utility...
jgbull
11-13-2006, 05:43 PM
Greetings,
Let me preface this with the following admission; I am an utter newb (or is it noob? You see... the depths of my newbery are so unfathomable I don't even know what I'm supposed to be called).
I am trying to convert an RSS feed (http://www.rightsandtrade.org/regions/russa/blog/rss.xml) into HTML for a page in my site.
I have investigated and found RSS to Javascript utilities that are painfully slow (rss-to-javascript.com), full of annoying adverts ("This feed generated by rss-to-javascript.com) or are too complex for me to understand (CGI scripts that do this for you etc).
Might a kind soul out there offer some advice?
JB
I don't have the answer for you, but are you sure you want it in javascript rather than in php so it becomes part of the page itself even for non-javascript browsers?
Arthur
11-14-2006, 06:25 AM
The RSS feed has to be converted from XML to HTML. You can do this in the browser (JavaScript) or on the server (CGI, PHP, Python, etc.).
I wouldn't recommend relying on JavaScript (non-JS browsers and search engines won't see them). Using server-side scripting may be a bit more complex, but there are some fairly easy to use scripts available, such as for example MagpieRSS (http://magpierss.sourceforge.net/). Doing the conversion on the server gives you much more control over the display of the feed and you can do more things, like polling the feed and caching the result for faster displaying. MagpieRSS is written in PHP and does not require much configuration, you upload the .inc files and with a couple of lines of PHP code you can parse your feed.
There are many scripts on the net, I found MagpieRSS to be one of the easier ones, but perhaps someone here has another recommendation...
-Arthur
jgbull
11-14-2006, 12:00 PM
Thanks guys.
MagpieRSS did the trick quite nicely.
My humble result: http://www.rightsandtrade.org/regions/russia_news.php
JB
jgbull
11-14-2006, 02:31 PM
To follow up, I'm hoping someone might be able to help me with a couple of PHP issues. I've pasted my code below, but here is what I'm trying to do:
1)Get the date to show up below the title of the post
2)Change the MaxCharacter variable (if such a variable exists) so that the post title can be longer.
Rest assured I tried mightily to figure this out, but had no luck.
My code:
<?php
require_once('magpierss/rss_fetch.inc');
require_once('magpierss/rss_utils.inc');
$num_items = 5;
$items = array_slice($rss->items, 0, $num_items);
function displayUnixTimestamp($item) {
$rss_2_date = $item['pubdate'];
$rss_1_date = $item['dc']['date'];
$atom_date = $item['issued'];
if ($atom_date != "") $date = parse_w3cdtf($atom_date);
if ($rss_1_date != "") $date = parse_w3cdtf($rss_1_date);
if ($rss_2_date != "") $date = strtotime($rss_2_date);
if ($date == "") $date = time();
return $date;
}
$url = $_GET['http://www.rightsandtrade.org/regions/russia/blog/rss.xml'];
$rss = fetch_rss('http://www.rightsandtrade.org/regions/russia/blog/rss.xml');
echo;
foreach ($rss->items as $item) {
$href = $item['link'];
$title = $item['title'];
$rss_2_date = $item['pubdate'];
echo "<a href=$href>$title</a><br />";
echo "$pubdate<br />";
}
echo;
?>
kitchin
11-14-2006, 04:05 PM
Change
echo;
foreach ($rss->items as $item) {
$href = $item['link'];
$title = $item['title'];
$rss_2_date = $item['pubdate'];
echo "<a href=$href>$title</a><br />";
echo "$pubdate<br />";
to
echo "\n";
foreach ($rss->items as $item) {
$href = $item['link'];
$title = $item['title'];
$pubdate= displayUnixTimestamp($item);
echo "<a href='$href'>$title</a><br />\n";
echo "$pubdate<br />\n";
jgbull
11-14-2006, 04:42 PM
I think there's a problem with the function "displayUnixTimestamp"
It generates a string like this: 1163536830
You can see the result here: http://www.rightsandtrade.org/regions/russia_news.php
The above string changes very frequently, it seems to be generating the current time in some cryptic format undecipherable to me.
Thanks!
JB
kitchin
11-14-2006, 05:00 PM
D'oh. Yeah, those are unix timestamps, seconds since Jan. 1, 1970. The reason they change is because the program is defaulting to today, and finding no date in the feed, it seems. Try this:
Change
$pubdate= displayUnixTimestamp($item);
to
$pubdate= date('r', displayUnixTimestamp($item));
Up in the function, you can use print statements to verify the feed has no dates. Or just use this to look at the whole $item:
printf("<pre>test: item: %s</pre>\n", htmlspecialchars(print_r($item,1)));
// or in a comment:
printf("<!-- test: item: %s -->\n", htmlspecialchars(print_r($item,1)));
jgbull
11-14-2006, 05:08 PM
Hmm...
Well, the RSS feed from here: http://rightsantrade.org/regions/russia/blog/rss.xml doesn't seem to be creating a time stamp then... but this can't be as some of the clunky JS scripts that I used to aggregate RSS to HTML found the time stamp...
I found this code, thought it might be helpful, although I couldn't get it to work:
if (strlen($rss_1_date) == 10) {
// hack to catch dc:date in format 200x-XX-XX
$pretty_date = date("F d, Y", strtotime($rss_2_date));
} else {
$pretty_date = date($date_format, $in_date);
}
I must apologize... I've never scripted a single line of PHP before.
kitchin
11-14-2006, 05:23 PM
You can read about date formats here:
http://php.net/date
I used 'r' above in date('r',...), after I fixed it to edit a mistake.
Your example uses ""F d, Y". More examples are at that link.
I would stick with the first function "displayUnixTimestamp" instead of the hack, and just look at the feed to see if it has any dates at all.
jgbull
11-14-2006, 05:56 PM
Well... I've done what I can.
I can't possibly believe that Blogger doesn't create time stamps in it's feed, but stranger things have happened. I went through all the Blogger settings, tinkered with the character-set output (Universal vs. Western) because I read somewhere Magpie processes the latter.
function displayUnixTimestamp($item) {
$rss_2_date = $item['pubdate'];
$rss_1_date = $item['dc']['date'];
$atom_date = $item['issued'];
if ($atom_date != "") $date = parse_w3cdtf($atom_date);
if ($rss_1_date != "") $date = parse_w3cdtf($rss_1_date);
if ($rss_2_date != "") $date = strtotime($rss_2_date);
if ($date == "") $date = time();
return $date;
printf("<pre>test: item: %s</pre>\n", htmlspecialchars(print_NMo($item)));
}
I added in the "printf" string into the function section, but things stayed the same; I'm not exactly sure what that was supposed to do. You'll see I changed "r" to "NMo" based on the PHP Manual on date formatting.
I have to admit I'm surprised MagPieRSS doesn't have this built in somewhere...
Thanks for all the help kitchin.
kitchin
11-14-2006, 06:29 PM
Whoops, not that 'r'. The one in date(....).
Also, the print has to go before return. It's just to look at the raw feed, for debugging.
jgbull
11-14-2006, 07:02 PM
Well... with debugging I can see that the feed is creating a time stamp.
http://www.rightsandtrade.org/regions/russia_news.php
Does this provide me with any info that helps? I suspect it just might...
JB
kitchin
11-14-2006, 07:39 PM
Cool! Change this line
$atom_date = $item['issued'];
to
$atom_date=$item['issued'] or $atom_date=$item['updated'] or $atom_date=$item['published'];
jgbull
11-14-2006, 07:48 PM
Sweet! Changing to..
$atom_date = $item['published'];
and
echo "\n";
foreach ($rss->items as $item) {
$href = $item['link'];
$title = $item['title'];
$published= $item['published'];
echo "<a href='$href'>$title</a><br />\n";
echo "$published<br /><br />\n";
Did the trick. Now to figure out how to change the format of the timestamp so it doesn't provide so much superfluous information (ie. the time zone and exact second when the posting was made).
jgbull
11-14-2006, 07:50 PM
Oh.. and the other problem; a limit on the maximum characters allowed in the $item['title']
jgbull
11-14-2006, 08:09 PM
$published = date('j M Y', displayUnixTimestamp($item));
That did the trick... I'm infinitely appreciative kitchin. Btw, cute dogs.
Arthur
11-15-2006, 06:12 AM
Oh.. and the other problem; a limit on the maximum characters allowed in the $item['title'] It doesn't appear there is a limit on the length of the titles in MagpieRSS. But, it does look like Blogger has a 50 character limit on the title.
MagpieRSS is displaying the complete titles from the feed. So, if you want longer titles maybe you can change a setting at Blogger to do that.
-Arthur
jgbull
11-28-2006, 05:57 PM
Blogger didn't seem to have the functionality to pursue a longer-title length; it's probably possible, but not a battle worth fighting.
I am trying to limit the number of items MagpieRSS parses. Here's my code.
<?php
require_once('magpierss/rss_fetch.inc');
$rss = fetch_rss( $url );
function displayUnixTimestamp($item) {
$rss_2_date = $item['pubdate'];
$rss_1_date = $item['dc']['date'];
$atom_date = $item['published'];
if ($atom_date != "") $date = parse_w3cdtf($atom_date);
if ($rss_1_date != "") $date = parse_w3cdtf($rss_1_date);
if ($rss_2_date != "") $date = strtotime($rss_2_date);
if ($date == "") $date = time();
return $date;
}
$items = array_slice($rss->items, 0, 4);
$url = $_GET['http://www.rightsandtrade.org/regions/russia/blog/rss.xml '];
$rss = fetch_rss('http://www.rightsandtrade.org/regions/russia/blog/rss.xml ');
echo "\n";
foreach ($rss->items as $item) {
$href = $item['link'];
$title = $item['title'];
$published = date('j M Y', displayUnixTimestamp($item));
echo "<a href='$href'>$title</a><br />\n";
echo "$published<br /><br />\n";
}
echo;
?>
From all the documentation I can find, $items = array_slice($rss->items, 0, 4); should limit the # of items to 4. It isn't doing the trick however...
Jarrod
11-29-2006, 03:26 AM
Try replacing
foreach ($rss->items as $item)
with
foreach ($items as $item)
Jarrod
jgbull
11-29-2006, 12:51 PM
Hmmm... Thanks for the suggestion; sadly it doesn't seem to do the trick. Indeed, it makes the page not load at all...
I've tried consulting the MagpieRSS FAQ/Cookbook etc. with little luck...
Their suggestion:
$num_items = 10;
$rss = fetch_rss($url);
$items = array_slice($rss->items, 0, $num_items);
I've tried to integrate this into my code, with no success...
Jarrod
11-29-2006, 03:15 PM
I've spent a little longer going through your code. You might want to change the following.
1. Remove the fetch_rss($url) on line 4. It is trying to fetch a feed without having defined the URL.
2. As currently written the code is trying to slice the array before it's set-up. So change the sequence of
$items = array_slice($rss->items, 0, 4);
$url = $_GET['http://www.rightsandtrade.org/regions/russia/blog/rss.xml '];
$rss = fetch_rss('http://www.rightsandtrade.org/regions/russia/blog/rss.xml ');
to
$rss = fetch_rss('http://www.rightsandtrade.org/regions/russia/blog/rss.xml ');
$items = array_slice($rss->items, 0, 4);
I've removed the line $url = $_GET as it doesn't appear to be doing anything?
HTH
Jarrod
jgbull
11-29-2006, 03:41 PM
Woot! It works!
Thanks!
vBulletin® v3.6.8, Copyright ©2000-2009, Jelsoft Enterprises Ltd.