Displaying Multiple RSS Feeds with PHP

I would like to make a webpage that displays the titles of the most recent posts from multiple blogs in a single list, ordered by the date from newest to oldest. There exists a nice tutorial about reading the information from an RSS feed and displaying it using PHP.[1]c.bavota: An Easy Way to Display an RSS Feed with PHP. bavotasan.com. 15 Jul 2010. However, that only works with a single RSS feed. It is possible to display multiple feeds by just copying the code from the tutorial multiple times, then combine the content of those lists into a single list and sort the list items by date. Luckily, it’s easy to do this with PHP’s foreach loop.

$rss = new DOMDocument(); 
$feed = array();
$urlArray = array(array('name' => 'WordPress', 'url' => 'http://wordpress.org/news/feed/'),
                  array('name' => 'Duolingo',  'url' => 'http://blog.duolingo.com/rss'),
                  array('name' => 'Twitter',   'url' => 'https://blog.twitter.com/api/blog.rss?name=company'),
                  array('name' => 'Wikimedia', 'url' => 'http://blog.wikimedia.org/c/global/feed/')
                  );

foreach ($urlArray as $url) {
    $rss->load($url['url']);

    foreach ($rss->getElementsByTagName('item') as $node) {
        $item = array ( 
            'site'  => $url['name'],
            'title' => $node->getElementsByTagName('title')->item(0)->nodeValue,
            'desc'  => $node->getElementsByTagName('description')->item(0)->nodeValue,
            'link'  => $node->getElementsByTagName('link')->item(0)->nodeValue,
            'date'  => $node->getElementsByTagName('pubDate')->item(0)->nodeValue,
            );
        array_push($feed, $item);
    }
}

Now that we have all the information from the feeds in a single array, we can sort the items by date (newest first) using the usort function.

usort($feed, function($a, $b) {
    return strtotime($b['date']) - strtotime($a['date']);
});

You can do the printing however you want. For example, the following prints an html list of 30 most recent post titles with their links and date information. It’s also possible to print every item in $feed by using a foreach loop instead of the for loop and $limit.

$limit = 30;
echo '<ul>';
for ($x = 0; $x < $limit; $x++) {
    $site = $feed[$x]['site'];
    $title = str_replace(' & ', ' &amp; ', $feed[$x]['title']);
    $link = $feed[$x]['link'];
    $description = $feed[$x]['desc'];
    $date = date('l F d, Y', strtotime($feed[$x]['date']));
    
    echo '<li>';
    echo '<strong>'.$site.': <a href="'.$link.'" title="'.$title.'" target="_blank">'.$title.'</a></strong> ('.$date.')';
    echo '</li>';
}
echo '</ul>';

At the time of writing this post, the above code produces the following output:

References   [ + ]

1. c.bavota: An Easy Way to Display an RSS Feed with PHP. bavotasan.com. 15 Jul 2010.
Posted in Computers Tagged with: , ,

Inverted Earth with the same volume of oceans

Everyone knows about the inverted Earth where seas become land and land becomes seas, but that’s quite boring. Last April there was an interesting question posted at the Finnish Wikipedia’s “ask anything” discussion page.

Just like the basic inverted Earth, also this hypothetical planet, which we call Aam (the English equivalent might be something like Htrae), is formed of the Earth by flipping the contour lines upside down, so that the peak of Mount Everest would become the deepest point and the Challenger Deep the highest. But the interesting thing is that the volume of the oceans (about 1.3 × 109 km3) is kept unchanged.

EarthHypso

Earth’s Hypsography.

The question is, what is Aam’s mean sea level compared to the current sea level of the Earth. From the Earth’s hypsography diagram (above), we can see that it is approximately 4,500 meters below (or above when inverted) the Earth’s sea level. Last April I made a simple image of the map of Aam by manually editing an elevation map of the Earth in the Paint software. It’s a little rough and shows only three different levels of elevation. Now that I found an application that visually calculates changes in the sea level,[1]Sébastien Merkel. Effect of sea level on topography. The application is in French, but you don’t really need to know any French to be able to use it. I made a better image that shows more than three elevations, this time also below Aam’s sea level. After setting the sea level to −4,500 meters, the colors of course were the opposite of what I wanted, but I changed them using the Gradient Map tool in Photoshop.

aam2

Aam might look something like this.

References   [ + ]

1. Sébastien Merkel. Effect of sea level on topography. The application is in French, but you don’t really need to know any French to be able to use it.
Posted in Science Tagged with: , , ,

Perspective rectification

This is a cool perspective rectification method that I learnt last week. It probably has also a lot of other useful applications in addition to this one.

norseland1 Figure 1.

When cataloging uncommon books at LibraryThing, one can often notice that there may be no cover image available for the correct edition of the book. It can be quite irritating if you want to keep your virtual bookshelf as close to the original as possible. Because scanning the book covers and uploading them to LibraryThing is often too troublesome (my scanner is in the other room!), it’s more convenient to search for the correct image from the Internet. Sometimes it is possible to find only photographs like the one in Figure 1 on the right,[1]Mara L. Pratt (ed.). Legends of Norseland. Educational Publishing Company, Boston, 1894. which are not suitable LT cover images as such, but need some editing in order to look nice on the book list. If this is the case and you happen to be somewhat familiar with MATLAB, you can always use a thing called homography to do a perspective rectification on the image. There is a good planar homography lecture by Peter Corke on YouTube. He probably explains the concept better than me, so I recommend to watch the video instead of trying to introduce the thing myself.

For this method, you will need MATLAB and Corke’s Machine Vision Toolbox (MVTB). I needed also the MATLAB Functions for Multiple View Geometry even though I didn’t notice it in MVTB’s installation instructions but I may have failed to install the toolbox correctly.

When you are ready, run the following script in MATLAB. (You can of course change the details like filenames and the X matrix if needed and if you know what you’re doing.) It should open a new window with the image of your book. You will need to tell MATLAB where the corners of the book are by clicking them in counterclockwise order (or depending on your X matrix) starting from the top left corner. MVTB then uses the magic that was explained in Corke’s video lecture and straightens the image’s perspective.

norseland2 Figure 2.
im = imread('input.jpg');
image(im);
P = ginput(4)';
X = [min(P(1,:)), min(P(2,:)); 
     max(P(1,:)), min(P(2,:)); 
     max(P(1,:)), max(P(2,:)); 
     min(P(1,:)), max(P(2,:))]';
H = homography(P, X);
output = homwarp(H, im);
imwrite(output, 'output.jpg');

The most relevant things in these lines are the homography and homwarp functions that came with MVTB. First, homography calculates the homography matrix from P to X, and then homwarp applies the homography matrix to every pixel in the image. The output will be something like the image in Figure 2.

norseland3 Figure 3.

Now it should be easy to crop the image and do the possibly necessary width/height scaling using your favourite image editing program. For example, if you are on Windows, Paint is comfortably simple software for this step. If you don’t want to do the scaling manually, you can also modify the X matrix suitably, but that might be more impractical. The final product will be similar to Figure 3. The quality of the image is a little worse compared to scanning but it’s a good alternative if a scanner is not available, and it’s still good enough for using at LibraryThing.

References   [ + ]

1. Mara L. Pratt (ed.). Legends of Norseland. Educational Publishing Company, Boston, 1894.
Posted in Books, Science Tagged with: , , ,