GoogleSearchBridge.php 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. <?php
  2. /**
  3. * Returns the 100 most recent links in results in past year, sorting by date (most recent first).
  4. * Example:
  5. * http://www.google.com/search?q=sebsauvage&num=100&complete=0&tbs=qdr:y,sbd:1
  6. * complete=0&num=100 : get 100 results
  7. * qdr:y : in past year
  8. * sbd:1 : sort by date (will only work if qdr: is specified)
  9. */
  10. class GoogleSearchBridge extends BridgeAbstract{
  11. const MAINTAINER = "sebsauvage";
  12. const NAME = "Google search";
  13. const URI = "https://www.google.com/";
  14. const CACHE_TIMEOUT = 1800; // 30min
  15. const DESCRIPTION = "Returns most recent results from Google search.";
  16. const PARAMETERS = array( array(
  17. 'q'=>array(
  18. 'name'=>"keyword",
  19. 'required'=>true
  20. )
  21. ));
  22. public function collectData(){
  23. $html = '';
  24. $html = getSimpleHTMLDOM(self::URI
  25. .'search?q=' . urlencode($this->getInput('q'))
  26. .'&num=100&complete=0&tbs=qdr:y,sbd:1')
  27. or returnServerError('No results for this query.');
  28. $emIsRes = $html->find('div[id=ires]',0);
  29. if( !is_null($emIsRes) ){
  30. foreach($emIsRes->find('li[class=g]') as $element) {
  31. $item = array();
  32. // Extract direct URL from google href (eg. /url?q=...)
  33. $t = $element->find('a[href]',0)->href;
  34. $item['uri'] = ''.$t;
  35. parse_str(parse_url($t, PHP_URL_QUERY),$parameters);
  36. if (isset($parameters['q'])) { $item['uri'] = $parameters['q']; }
  37. $item['title'] = $element->find('h3',0)->plaintext;
  38. $item['content'] = $element->find('span[class=st]',0)->plaintext;
  39. $this->items[] = $item;
  40. }
  41. }
  42. }
  43. public function getName(){
  44. return $this->getInput('q') .' - Google search';
  45. }
  46. }