Christian Heilmann

How I built icant.co.uk – source code

Wednesday, June 3rd, 2009 at 3:24 pm

After my talk at FOWA in Cambridge yesterday I showed off that http://icant.co.uk is fully driven by YUI and YQL and maintained elsewhere. I’ve recorded a screencast about this earlier which is also available for download as a M4V but hadn’t released the code yet.

So here goes – this is the PHP source of icant.co.uk (without the HTML which is more or less done with YUI grids builder


// get all the feeds to grab the data
$feeds = array(
‘http://feeds2.feedburner.com/wait-till-i/gwZf’,
‘http://feeds.delicious.com/v2/rss/codepo8/myvideos?count=15’,
‘http://feeds.delicious.com/v2/rss/codepo8/sandbox’,
‘http://feeds.delicious.com/v2/rss/codepo8/icantarticles’,
‘http://www.slideshare.net/rss/user/cheilmann’
);

// assemble the YQL statement
$yql = ‘select meta.views,content.thumbnail,content.description,title,’.
‘link,description from rss where url in ‘;
$yql .= “(‘” . join($feeds,”’,’”) . “’)”;

// assemble the request url
$root = ‘http://query.yahooapis.com/v1/public/yql?q=’;
$url = $root . urlencode($yql) . ‘&format=json’;

// get the feeds and populate the data to echo out in the HTML
$feeds = renderFeeds($url);
$blog = $feeds[‘blog’];
$videos = $feeds[‘videos’];
$articles = $feeds[‘articles’];
$presentations = $feeds[‘slides’];

// this function loads all the feeds and turns them into HTML
function renderFeeds($url){

// grab the content from YQL via cURL
$c = getStuff($url);

// as the content comes back as JSON, turn it into PHP objects
$x = json_decode($c);

// reset counter for videos and presentations
$count = 0;
$vidcount = 0;

// start new array to return
$out = array();

// loop over YQL results, if they exist.
if($x->query->results->item){
foreach($x->query->results->item as $i){

// if the link comes from the blog, add to the blog HTML
if(strstr($i->link,’wait-till-i’)){
$out[‘blog’] .= ‘

  • link . ‘”>’ . $i->title .

    ’ . html_entity_decode($i->description) .

  • ‘;
    $vidcount++;
    }

    // for interviews and articles, add to the articles section
    if(strstr($i->title,’Interview’) ||
    strstr($i->title,’Article:’)){
    $out[‘articles’].= ‘

  • link . ‘”>’ . $i->title .

    YQL doesn’t send a diagnostics part

    // grab the books from my blog
    $yql = ‘select * from html where url=’.
    ‘”http://wait-till-i.com/books/”’.
    ’ and xpath=”//div[@class=’entry’]”’;
    $books = renderHTML($root.urlencode($yql).’&format=xml&diagnostics=false’);

    // this is a quick and dirty solution for the HTML output
    function renderHTML($url){
    // pull the information from YQL
    $c = getStuff($url);
    // check that something came back
    if(strstr($c,’<’)){
    // remove all the XML parts
    $c = preg_replace(“/.*|.*/”,’‘,$c);
    $c = preg_replace(“/ ” encoding=”UTF-8”?>/”,’‘,$c);
    // remove all comments
    $c = preg_replace(“//”,’‘,$c);
    }

    // send it back
    return $c;
    }

    // a simple cURL function to get information
    function getstuff($url){
    $curl_handle = curl_init();
    curl_setopt($curl_handle, CURLOPT_URL, $url);
    curl_setopt($curl_handle, CURLOPT_CONNECTTIMEOUT, 2);
    curl_setopt($curl_handle, CURLOPT_RETURNTRANSFER, 1);
    $buffer = curl_exec($curl_handle);
    curl_close($curl_handle);
    if (empty($buffer)){
    return ‘Error retrieving data, please try later.’;
    } else {
    return $buffer;
    }

    }

  • Tags: , , , , ,

    Share on Mastodon (needs instance)

    Share on Twitter

    Newsletter

    Check out the Dev Digest Newsletter I write every week for WeAreDevelopers. Latest issues:

    Dev Digest 146: 🥱 React fatigue 📊 Query anything with SQL 🧠 AI News

    Why it may not be needed to learn React, why Deepfake masks will be a big problem and your spirit animal in body fat! 

    Dev Digest 147: Free Copilot! Panel: AI and devs! RTO is bad! Pi plays!

    Free Copilot! Experts discuss what AI means for devs. Don't trust containers. Mandated RTO means brain drain. And Pi plays Pokemon!

    Dev Digest 148: Behind the scenes of Dev Digest & end of the year reports.

    In 50 editions of Dev Digest we gave you 2081 resources. Join us in looking back and learn about all the trends this year.

    Dev Digest 149: Wordpress break, VW tracking leak, ChatGPT vs Google.

    Slowly starting 2025 we look at ChatGPT vs Google, Copilot vs. Cursor and the state of AI crawlers to replace web search…

    Dev Digest 150: Shifting manually to AI.

    Manual coding is becoming less of a skill. How can we ensure the quality of generated code? Also, unpacking an APK can get you an AI model.

    My other work: