Mar 30 2010

PHP google images mini bot

Category: Php,Spiders & web botsGiulio Pons @ 1:34 pm

This simply php function will retrieve the links of the images from Google Images searched with a keyword. The function just calls images.google.it and parse the html to find the url of the images, in this case the urls are stored in the javascript, so the parsing doesn’t use any html tag to find data. It builds an array of urls and return it.

function getGoogleImg($k) {
	$url = "http://images.google.it/images?as_q=##query##&hl=it&imgtbs=z&btnG=Cerca+con+Google&as_epq=&as_oq=&as_eq=&imgtype=&imgsz=m&imgw=&imgh=&imgar=&as_filetype=&imgc=&as_sitesearch=&as_rights=&safe=images&as_st=y";
	$web_page = file_get_contents( str_replace("##query##",urlencode($k), $url ));

	$tieni = stristr($web_page,"dyn.setResults(");
	$tieni = str_replace( "dyn.setResults(","", str_replace(stristr($tieni,");"),"",$tieni) );
	$tieni = str_replace("[]","",$tieni);
	$m = preg_split("/[\[\]]/",$tieni);
	$x = array();
	for($i=0;$i<count($m);$i++) {
		$m[$i] = str_replace("/imgres?imgurl\\x3d","",$m[$i]);
		$m[$i] = str_replace(stristr($m[$i],"\\x26imgrefurl"),"",$m[$i]);
		$m[$i] = preg_replace("/^\"/i","",$m[$i]);
		$m[$i] = preg_replace("/^,/i","",$m[$i]);
		if ($m[$i]!="") array_push($x,$m[$i]);
	}
	return $x;
}

This function will be included in the next version of Mini Bot Class.

Share

Tags: , , , , , ,


Dec 26 2009

Decimal Degrees conversion and distance of two points on google map

Category: Google Maps,JavascriptGiulio Pons @ 11:06 pm

Those two functions are usefull when you’re making Google Maps applications:
When you show the coordinates of a point, it’s sometimes better to show them as degrees and not as deciaml (even if decimal is simpler). Each of the two coordinates can be converted with the same function. In the function call the “tipo” is the type of the “v” value: if you call the function without the type, then the default type is “N”, that means “NORTH”, it means that you’re converting a Latitude value (Latitude is the angular distance of a point north or south of the Equator). Values for latitude type are “N” for NORTH and “S” for SOUTH.
If you specify “E” or “W” than you’re converting a Longitude value (Longitude is the angular distance of a point east or west of the Greenwich Meridian).

function convertDecDeg(v,tipo) {
	if (!tipo) tipo='N';
	var deg;
	deg = v;
	if (!deg){
		return "";
	} else if (deg > 180 || deg < 0){
		// convert coordinate from north to south or east to west if wrong tipo
		return convertDecDeg(-v,(tipo=='N'?'S': (tipo=='E'?'W':tipo) ));
	} else {
		var gpsdeg = parseInt(deg);
		var remainder = deg - (gpsdeg * 1.0);
		var gpsmin = remainder * 60.0;
		var D = gpsdeg;
		var M = parseInt(gpsmin);
		var remainder2 = gpsmin - (parseInt(gpsmin)*1.0);
		var S = parseInt(remainder2*60.0);
		return D+"&deg; "+M+"' "+S+"'' "+tipo;
	}
}

This function calculates distance with the Haversine formula, this formula assumes that our Earth is spherical, but it isn’t since it’s more like a big orange fruit also compresses at poles. I’ve read that this function has an error about + o – 3 meters (it depends), but it’s a small error for many purposes, I’ve used it on my google maps ruler:

function distance(lat1,lon1,lat2,lon2) {
	var R = 6371; // km (change this constant to get miles)
	var dLat = (lat2-lat1) * Math.PI / 180;
	var dLon = (lon2-lon1) * Math.PI / 180;
	var a = Math.sin(dLat/2) * Math.sin(dLat/2) +
		Math.cos(lat1 * Math.PI / 180 ) * Math.cos(lat2 * Math.PI / 180 ) *
		Math.sin(dLon/2) * Math.sin(dLon/2);
	var c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1-a));
	var d = R * c;
	if (d>1) return Math.round(d)+"km";
	else if (d<=1) return Math.round(d*1000)+"m";
	return d;
}
Share

Tags: , , , , , , , ,


Dec 24 2009

PHP bot to grab meteo information from Google

Category: Php,Spiders & web botsGiulio Pons @ 3:37 pm

Google has many usefull functions that give you data fast, such as cinema infos, or for meteo forecasts. I think that Google grabs those informations from the many sites indexed with his bots.

As I did on a previous post for words spelling you can retrieve those informations with some mini bots. The mini bot I’ve made for meteo retrieves informations from italian Google servers about weather forecast of a specified city (not only italian cities).

Since google gives only 4 days meteo if you ask for a date too much in the future it will return an empty string.

Below you can find the PHP source code, and here is a working demo!

This function is included in the Mini Bots Class.

function dayadd($days,$date=null , $format="d/m/Y"){
	//add days to a date function
	return date($format,strtotime($days." days",strtotime( $date ? $date : date($format) )));
}

function attr($s,$attrname) {
	//get the attribute value of an html tag
	preg_match_all('#\s*('.$attrname.')\s*=\s*["|\']([^"\']*)["|\']\s*#i', $s, $x);
	if (count($x)>=3) return $x[2][0];
	return "";
}

function doGoogleMeteo($q,$date) {
	if ($date>dayadd(3,date("Y-m-d"),"Y-m-d"))return "";

	// grab google page with meteo query
	$web_page = file_get_contents( "http://www.google.it/search?q=meteo+" . urlencode($q) );

	//parse html to find data, and store them in an array
	preg_match_all('#<div class=e>(.*)</table>#Us', $web_page, $m);
	if (count($m)>0) {
		$p = array();
		preg_match_all('#<img([^>]*)?>#Us', $m[0][0], $img);
		for ($i=0;$i<count($img[0]);$i++) {
			$tag = str_replace("src=\"/","src=\"http://www.google.it/",$img[0][$i]);
			$p[dayadd($i,date("Y-m-d"),"Y-m-d")]["title"] = attr($tag,"title");
			$p[dayadd($i,date("Y-m-d"),"Y-m-d")]["img"] = attr($tag,"src");
		}
		preg_match_all('#<nobr>(.*)</nobr>#Uis', $m[0][0], $nobr);
		for ($i=0;$i<count($nobr[1]);$i++) {
			$temp= explode("|",$nobr[1][$i]);
			$p[dayadd($i,date("Y-m-d"),"Y-m-d")]["min"] = trim($temp[1]);
			$p[dayadd($i,date("Y-m-d"),"Y-m-d")]["max"] = trim($temp[0]);
		}
		return $p[$date];
	}

	return "nada.";
}

print_r ( doGoogleMeteo("milano","2009-12-25") );
//Array (
// [title] => Rovesci
// [img] => http://www.google.it/images/weather/rain.gif
// [min] => -4°C
// [max] => 7°C
//)
Share

Tags: , , , , , , , , ,


Next Page »