Extract the image from Bing search result
<?php
class Gambar extends H2o_Node {
var $search_term;
function __construct($argstring, $parser, $pos=0) {
list($this->term, $this->hack) = explode(' ', $argstring);
}
function get_api($search_term = 'hello world', $hack = ""){
$search_term .= " ".$hack ;
$search_term = urlencode($search_term);
return "http://www.bing.com/images/search?q=$search_term";
}
function filter($feed)
{
global $_settings;
$bad_urls = $_settings->bad_urls;
$filtered_feed = array();
foreach ($feed as $item) {
$found = false;
foreach ($bad_urls as $bad_url) {
if(stripos($item->mediaurl, $bad_url) !== false){
$found = true;
}
}
if(!$found){
$filtered_feed[] = $item;
}
}
return $filtered_feed;
}
function fetch($context,$url) {
$this->url = $url;
$doc = @file_get_contents($this->url);
phpQuery::newDocument($doc);
$images = array();
foreach(pq('div.item') as $item){
$image['mediaurl'] = pq('a.thumb', $item)->attr('href');
$image['link'] = pq('a.tit', $item)->attr('href');
$image['title'] = pq('div.des', $item)->html();
$image['size'] = pq('div.fileInfo', $item)->html();
$images[] = $image;
}
return $images;
}
function render($context, $stream) {
$cache = h2o_cache($context->options);
$search_term = $context->resolve(':term');
$hack = $context->resolve(':hack');
$url = $this->get_api($search_term, $hack);
$feed = @$this->fetch($context, $url);
$feed = @$this->filter($feed);
$context->set("images", $feed);
}
}
h2o::addTag('gambar');