PHP bot for meteo google


/ Published in: PHP
Save to your folder(s)

This is a mini bot I’ve made for meteo that retrieves informations from italian Google servers about weather forecast of a specified city (not only italian cities).


Copy this code and paste it in your HTML
  1. function dayadd($days,$date=null , $format="d/m/Y"){
  2. //add days to a date function
  3. return date($format,strtotime($days." days",strtotime( $date ? $date : date($format) )));
  4. }
  5.  
  6. function attr($s,$attrname) {
  7. //get the attribute value of an html tag
  8. preg_match_all('#\s*('.$attrname.')\s*=\s*["|\']([^"\']*)["|\']\s*#i', $s, $x);
  9. if (count($x)>=3) return $x[2][0];
  10. return "";
  11. }
  12.  
  13. function doGoogleMeteo($q,$date) {
  14. if ($date>dayadd(3,date("Y-m-d"),"Y-m-d"))return "";
  15.  
  16. // grab google page with meteo query
  17. $web_page = file_get_contents( "http://www.google.it/search?q=meteo+" . urlencode($q) );
  18.  
  19. //parse html to find data, and store them in an array
  20. preg_match_all('#<div class=e>(.*)</table>#Us', $web_page, $m);
  21. if (count($m)>0) {
  22. $p = array();
  23. preg_match_all('#<img([^>]*)?>#Us', $m[0][0], $img);
  24. for ($i=0;$i<count($img[0]);$i++) {
  25. $tag = str_replace("src=\"/","src=\"http://www.google.it/",$img[0][$i]);
  26. $p[dayadd($i,date("Y-m-d"),"Y-m-d")]["title"] = attr($tag,"title");
  27. $p[dayadd($i,date("Y-m-d"),"Y-m-d")]["img"] = attr($tag,"src");
  28. }
  29. preg_match_all('#<nobr>(.*)</nobr>#Uis', $m[0][0], $nobr);
  30. for ($i=0;$i<count($nobr[1]);$i++) {
  31. $temp= explode("|",$nobr[1][$i]);
  32. $p[dayadd($i,date("Y-m-d"),"Y-m-d")]["min"] = trim($temp[1]);
  33. $p[dayadd($i,date("Y-m-d"),"Y-m-d")]["max"] = trim($temp[0]);
  34. }
  35. return $p[$date];
  36. }
  37.  
  38. return "nada.";
  39. }
  40.  
  41. print_r ( doGoogleMeteo("milano","2009-12-25") );
  42. //Array (
  43. // [title] => Rovesci
  44. // [img] => http://www.google.it/images/weather/rain.gif
  45. // [min] => -4°C
  46. // [max] => 7°C
  47. //)

URL: http://www.barattalo.it/2009/12/24/php-meteo-bot-for-google/

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.