Friday 3 July 2009

Some START progress

Well it seems I've finished the majority of the back end work for this project, successfully integrating Twitter, Picassa and Blogger into one page using, PHP & XML... Which I'm quite happy with seeing as prior to this project I've not coded anything in php! As part of me trying to maintain the whole creative commons thing and trying to share and share alike thingy here's the code I've used:

Blogness.php - Grabs the xml from the rss feed and displays all posts:
Adapted from: http://www.hiash.com/blogger.php

<?php
define('MAGPIE_DIR', '../../magpierss/');
require_once(MAGPIE_DIR.'rss_fetch.inc');

//RSS feed has to be of ATOM format
$rss = fetch_rss( 'http://www.blogger.com/feeds/8911594570691911602/posts/default' );

//display latest blog content:
foreach ($rss->items as $item) {
$title = $item['title'];
$content = $item['atom_content'];
echo "<h1>$title</h1><p>$content</p>";
}
?>


Twitterness.php - grabs the rss from your public twitter feed and displays the last 5 posts...
Adapted from http://workbench.cadenhead.org/twitter-rss-to-html/


<?php

define('MAGPIE_DIR', '../../magpierss/');
require_once(MAGPIE_DIR.'rss_fetch.inc');

$tweets_to_display = 5;
// the page where the tweet(s) will be displayed
$page = "";
// the user's Twitter username
$username = "StartPlymouth";


$rss = fetch_rss( 'http://twitter.com/statuses/user_timeline/49595711.rss' );

// start buffering output
ob_start();

$count = 1;
$now = time();


foreach ($rss->items as $item) {
// limit the number of tweets displayed
if ($tweets_to_display > 0) {
if ($count > $tweets_to_display) {
continue;
}
}
$tweet = $item['description'];
// ignore tweets linking back to page (if it is defined)
if ($page != "") {
if (!strpos($tweet, $page) === false) {
continue;
}
}
// figure out how recently the tweet was posted
$when = ($now - strtotime($item['pubdate']));
$posted = "";
if ($when < 60) {
$posted = $when . " seconds ago";
}
if (($posted == "") & ($when < 3600)) {
$posted = "about " . (floor($when / 60)) . " minutes ago";
}
if (($posted == "") & ($when < 7200)) {
$posted = "about 1 hour ago";
}
if (($posted == "") & ($when < 86400)) {
$posted = "about " . (floor($when / 3600)) . " hours ago";
}
if (($posted == "") & ($when < 172800)) {
$posted = "about 1 day ago";
}
if ($posted == "") {
$posted = (floor($when / 86400)) . " days ago";
}
// filter the user's username out of tweets
$tweet = str_replace($username . ": ", "", $tweet);
// turn URLs into hyperlinks
$tweet = preg_replace("/(http:\/\/)(.*?)\/([\w\.\/\&\=\?\-\,\:\;\#\_\~\%\+]*)/", "<a href=\"\\0\">Link</a>", $tweet);
// link to users in replies
$tweet = preg_replace("(@([a-zA-Z0-9\_]+))", "<a href=\"http://www.twitter.com/\\1\">\\0</a>", $tweet);
echo '<p class="Tweets"> <strong>'.$username.':</strong> '.$tweet.'</p><p class="TweetInfo">'.$posted.'</p>';
$count++;

}

// display output
ob_end_flush();
?>

Picassaness.php - This one is the most complex but doesn't require Magpierss which is cool, does the same grabs public xml, I've got it to store all the images in an array and select a random 5 and display them. (to change on each refresh):
(I adapted from - http://code.lawpower.net/2008/01/21/picasa-gallery-googles-cruel-rss-feed/)

<?php
$DisplayNumberofPhotos = 5;

function AddXMLtoItems($xmlurl){
//get the xml from the feed provided in $xmlurl
$session = curl_init($xmlurl);
curl_setopt($session, CURLOPT_HEADER, false);
curl_setopt($session, CURLOPT_RETURNTRANSFER, true);
$response = curl_exec($session);
curl_close($session);
$xml = simplexml_load_string($response);
$items = $xml->channel;

//now we have each item from the XML file into $items we now need to split them up so their more useful to us
foreach($items->item as $item){
$title = $item->title;
$content = $item->description;

//pull the images from the *descripotion* section
$quotes = array('"', "'", "\n");
$imgContents = str_replace($quotes, '', $content); #Strip " and ' as well as \n from input string
$imgContents = stristr($imgContents, 'src='); #Drop everything before the 'src'
$endTagPosition = stripos($imgContents, 'alt'); #Position of the end tag '>'
$img = substr($imgContents, 4, $endTagPosition - 4); #Get everything from src to end tag --> 'src="path" something>'

//Change the dimentions of the images, Picasa sets images sizes at specific sizes within the url
$ImageURL = str_replace('/s288', '/s220', $img);
$ImageURLs[] = $ImageURL;
}
return $ImageURLs;
}

//Grab the images from the array and merge them all into one big array...
$ImageURLs = AddXMLtoItems("http://picasaweb.google.com/data/feed/base/user/StartPlymouth/albumid/5354140273775432289?alt=rss&kind=photo&authkey=Gv1sRgCOGggY2Y8baMLw&hl=en_US");
//$ImageURLs2 = AddXMLtoItems("");
//$ImageURLs3 = AddXMLtoItems("");
//$ImageURLs4 = AddXMLtoItems("");
//$ImageURLs = array_merge((array)$ImageURLs1, (array)$ImageURLs2, (array)$ImageURLs3, (array)$ImageURLs4);

//Now we need to grab random images from this array to be displayed on the home page
for ( $counter = 0; $counter <= ($DisplayNumberofPhotos - 1); $counter += 1) {
//get ImageToDisplay to be a exact image number
$ImageToDisplay = sizeof($ImageURLs);
$ImageToDisplay = rand(0, $ImageToDisplay);
$ImageToDisplay = round($ImageToDisplay);
$ArrayofImagesBeenDisplayed[] = $ImageToDisplay;
}
//add image displayed to an array to ensure we don't get duplications
$ArrayofImagesBeenDisplayed = array_unique($ArrayofImagesBeenDisplayed);

//Display these random images and link them to a larger image
for ($counter2 = 0; $counter2 < sizeof($ArrayofImagesBeenDisplayed); $counter2 += 1) {
$ImageDisplayURL = $ImageURLs[$ArrayofImagesBeenDisplayed[$counter2]];
$ImageLinkURL = str_replace('/s220', '/s1024', $ImageURLs[$ArrayofImagesBeenDisplayed[$counter2]]);
echo "<a href='" . $ImageLinkURL . "'><img src='" . $ImageDisplayURL . "' /></a>";
}
?>


Obviously I'm no coder and I'm sure this will make some of you spit your coffee out at your computer screen, but I'm sure some of you will find it useful! Any feedback would be good. I've made these seporate files to be called into the main page by using Include('file.php'); so should hopefully work with you, for the picassa one you have to declare how many images you want displayed: $DisplayNumberofPhotos = 5; include('Picassaness.php') In the picassa one I would quite like it to find all rss feeds related to that piccassa user rather than having to list each individual album manually so if anyone knows how to do that, that would be great. Otherwise shall try and work it out myself and post it here.

No comments: