Pull the top 10 Searches on Technorati?

Discussion in 'Programming' started by Glen, Apr 20, 2007.

  1. #1
    I was recently after a script from a php programmer on here regarding pulling the top 10 searches from technorati and using them in a 'certain way'

    He said it wasnt possible (not saying hes wrong) but just checking here before I give up on the idea...
     
    Glen, Apr 20, 2007 IP
  2. krakjoe

    krakjoe Well-Known Member

    Messages:
    1,795
    Likes Received:
    141
    Best Answers:
    0
    Trophy Points:
    135
    #2
    krakjoe, Apr 20, 2007 IP
  3. nico_swd

    nico_swd Prominent Member

    Messages:
    4,153
    Likes Received:
    344
    Best Answers:
    18
    Trophy Points:
    375
    #3
    ^^ Beat me to it...


    Anyway, here's my solution.

    
    function technorati_search($query, $bucket = 'kwd')
    {
    	$bucket = strtolower($bucket);
    	
    	if (!in_array($bucket, array('kwd', 'tag', 'blog')))
    	{
    		$bucket = 'kwd';
    	}
    	
    	$ch = curl_init('http://technorati.com/search.php');
    	curl_setopt($ch, CURLOPT_POST, true);
    	curl_setopt($ch, CURLOPT_POSTFIELDS, 's=' . rawurlencode($query) . '&bucket=' . $bucket);
    	curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
    	curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    
    	if (!preg_match_all('/<li\s+class="hentry(\s+first-child)?">(.*?)<\/li>/si', curl_exec($ch), $entries))
    	{
    		return false;
    	}
    	
    	curl_close($ch);	
    	$results = array();
    	
    	foreach ($entries[2] AS $key => $entry)
    	{
    		preg_match('/<a\s+href="([^"]+)"[^>]+>([^<]+)/i', $entry, $matches);
    		
    		$results[$key]['itemurl']   = $matches[1];
    		$results[$key]['itemtitle'] = $matches[2];
    		
    		preg_match('/<cite\s+class="bloginfo"><a\s+href="\/blogs\/([^"]+)[^>]+>([^<]+)/i', $entry, $bloginfo);
    		
    		$results[$key]['blogurl']   = rawurldecode($bloginfo[1]);
    		$results[$key]['blogname']  = $bloginfo[2];
    		
    		preg_match('/<blockquote\s+class="entry-summary"[^>]+>(.*?)<\/blockquote>/si', $entry, $summary);
    		
    		$results[$key]['summary']   = strip_tags(trim($summary[1]));
    	}
    	
    	return $results;
    }
    
    
    PHP:

    Usage example:

    
    echo '<pre>';
    
    $results = technorati_search('Company Flow');
    
    if (!$results)
    {
    	echo 'No results found.';
    }
    else
    {
    	print_r($results);
    }
    
    
    PHP:
     
    nico_swd, Apr 20, 2007 IP
  4. krakjoe

    krakjoe Well-Known Member

    Messages:
    1,795
    Likes Received:
    141
    Best Answers:
    0
    Trophy Points:
    135
    #4
    some overkill there nico :D

    
    <?php
    function top_ten( )
    {
    	$return = null;
    	if(!($read = fopen( 'http://technorati.com/', 'r' ))):
    		die("Cannot open technorati");
    		return false;
    	else:
    		preg_match('#<ol>(.*?[^ol>])</ol>#si', file_get_contents( 'http://technorati.com/' ), $top );
    		$data = split( '</li>', $top[1] );
    		if( $data )
    		{
    			$result = array( );
    			$count = 0 ;
    			foreach( $data as $value )
    			{
    				if( trim( $value ) != "" )
    				{
    					preg_match( '#<a href="(/search/.*?[^ ])" .*?>(.*?[^<])</a>#si', $value, $match );
    					$result[$count + 1]["title"] = trim( $match[2] );
    					$result[$count + 1]["url"] = trim( $match[1] );
    					$count++;
    				}
    			}
    			return $result;
    		}
    	endif;
    }
    ?>
    <h1>Technorati top ten - not sure why</h1>
    <?php
    foreach( top_ten( ) as $pos => $data )
    {
    	?>
    	<?php echo $pos ?> - <a href="http://technorati.com<?php echo $data['url'] ?>"><?php echo $data['title'] ?></a><br />
    	<?php
    }
    ?>
    
    PHP:
     
    krakjoe, Apr 20, 2007 IP
    Glen likes this.
  5. nico_swd

    nico_swd Prominent Member

    Messages:
    4,153
    Likes Received:
    344
    Best Answers:
    18
    Trophy Points:
    375
    #5
    Actually, I think I misunderstood his question, lol. Anyway... one more script for my collection. :p
     
    nico_swd, Apr 20, 2007 IP
  6. Glen

    Glen Peon

    Messages:
    1,852
    Likes Received:
    91
    Best Answers:
    0
    Trophy Points:
    0
    #6
    thanks, much appreciated
     
    Glen, Apr 23, 2007 IP
  7. Glen

    Glen Peon

    Messages:
    1,852
    Likes Received:
    91
    Best Answers:
    0
    Trophy Points:
    0
    #7
    krakjoe i have added you to msn :)
     
    Glen, Apr 23, 2007 IP