Sunday 29 November 2009

Poiplaza full country POI download spider

Siadłem dziś rano do kompa odrobinę "poandrzejkowy" chcąc zaktualizować sobie poi'e z PoiPlaza i z wrodzonego mi twórczego lenistwa postanowiłem że nie będę jak ten ostatni osioł klikać link po linku ściągając po kolei te poi'e. Napisałem więc kawałek kodu w php który buszuje trochę po PoiPlaza i podaje to prawie gotowe jak na tacy :lol:

Jeśli ktoś chce to sobie odpalić na windzie z linii poleceń to można np. tak:

c:\PHP>php-cgi.exe poiplaza.php >poiplaza.htm

później otworzyć gotowe poiplaza.htm w przeglądarce albo też wystawić sobie na stronkę pod apacza czy iis'a ten skrypt poiplaza.php i kliknąć przeglądarą ;-)

W aktualnym kodzie zasysam Polskę i poi'e dla iGo8 niemniej dwoma kliknięciami można zmienić na dowolny inny kraj czy inną wersję poi na inna nawigację.

Poniższy kod jest trochę zniekształcony przez koloryzator składni i zacytowałem go tutaj wyłacznie poglądowo dlatego też działający kod trzeba pobrać z mojego chomika: http://chomikuj.pl/saphireee/nawigacja/programiki/poiplaza.zip
<?php
// (c)2009 Bartek 'saphire' Siebab
//
// remember: increasese enough php execution time in php.ini
// this script lasts loooongggg... 
// becouse plenty pages has to be opened and parsed...
//
  $country=32; // 32=Poland - get this from url of choosen country 
  $target='[iI][gG][oO]8';//igo8 partly regexp

  $download='http://www.poiplaza.com/';
  $url='http://poiplaza.com/index.php';

  $countryurl=$url . '?p=dc&c=' . $country;
  $poipageurl=$countryurl . '&lpg=&d=&pg=';

  $crlf=chr(13).chr(10);  

// now lets do this  
  $countrybody=http_parse_message(http_get($countryurl))->body; 
  
  preg_match('/ \d*[ ]*[\/][ ]*\d* [ ]*/',$countrybody,$matches );
  $pages=explode("/",str_replace(' ','',str_replace(' ','',$matches[0])));
  $minpage = $pages[0];
  $maxpage = $pages[1];
  
  $n=0;
  for ($pg=$minpage; $pg<=$maxpage;$pg++) {
    $poipagebody=http_parse_message(http_get($poipageurl . $pg))->body;
    preg_match_all('/\<a href\=\"\?p\=sdb&d\=\d*\&lstpg\=ds\&lsts\=\_LSTS\_\"\>.*\<\/a\>/',$poipagebody,$matches );

    $i=0;
    foreach ($matches[0] as $key => $value) {
      preg_match_all('/\d*/',$value,$poiurl);
      
      $poiid = $poiurl[0][18]; // in 18th field is poi id
      $poiadr = $url . '?p=download&d=' . $poiid . '&lstpg2=sdb&lstpg=ds&lsts=_LSTS_';
      
      $n++; // lets create table with poiid and poiurl
      $poi[$n][0] = $poiid;
      $poi[$n][1] = $poiadr; 
      //print $poi[$n][0] . ' # ' . $poi[$n][1] . ' <br> ';
      
      $i++;      
    } // end of foreach loop
  } // end of for loop 

// now clear some useless variables
unset($poiid);
unset($poiadr);
unset($countrybody);
unset($poipagebody);
unset($matches);

// *************************************************************************
print $crlf;
print '<html><title>Poiplaza full country POI download spider (c)2009 Bartek \'saphire\' Siebab</title><body>'.$crlf;
print '<b>Poiplaza full country POI download spider (c)2009 Bartek \'saphire\' Siebab</b><br><br>' . $crlf;
print 'Country id: ' . $country . ' found '.$n.' POI\'s<br><br>'. $crlf;

  // now lets loop and open every poi page
  for ($pg=1; $pg<=$n;$pg++) {
    //print $poi[$pg][1];
    $poipagebody=http_parse_message(http_get($poi[$pg][1]))->body;
    $linkregexp='/download\/zipfiles\/\d*\/.*' . $target . '[.][zZ][iI][pP]/';
    preg_match($linkregexp,$poipagebody,$matches);
    $poireadyurls[$pg]=$matches[0];
  } // end of for loop

  // now lets make a plain text show
  print 'Ready copy & paste box:<br><br><code>'.$crlf;
  for ($pg=1; $pg<=$n;$pg++) {    
    print($download . $poireadyurls[$pg] . '<br>'. $crlf);
  } // end of for loop
  print '</code><br><br>'.$crlf;

  // now lets make a href show
  print 'Clickable url\'s box:<br><br>'.$crlf;
  for ($pg=1; $pg<=$n;$pg++) {    
    print('<a href="'.$download . $poireadyurls[$pg] .'">'.$download . $poireadyurls[$pg].'</a><br>'. $crlf);
  } // end of for loop
  print '<br>'.$crlf;
  
print '</body></html>'.$crlf;
// again clear some variables
unset($poipagebody);
unset($matches);
unset($poireadyurls)
?>