GoogleSearchBridge.php 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  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. private $request;
  12. public function loadMetadatas() {
  13. $this->maintainer = "sebsauvage";
  14. $this->name = "Google search";
  15. $this->uri = "https://www.google.com/";
  16. $this->description = "Returns most recent results from Google search.";
  17. $this->parameters[] = array(
  18. 'q'=>array('name'=>"keyword")
  19. );
  20. }
  21. public function collectData(array $param){
  22. $html = '';
  23. if (isset($param['q'])) { /* keyword search mode */
  24. $this->request = $param['q'];
  25. $html = $this->getSimpleHTMLDOM('https://www.google.com/search?q=' . urlencode($this->request) . '&num=100&complete=0&tbs=qdr:y,sbd:1') or $this->returnServerError('No results for this query.');
  26. }
  27. else{
  28. $this->returnClientError('You must specify a keyword (?q=...).');
  29. }
  30. $emIsRes = $html->find('div[id=ires]',0);
  31. if( !is_null($emIsRes) ){
  32. foreach($emIsRes->find('li[class=g]') as $element) {
  33. $item = array();
  34. // Extract direct URL from google href (eg. /url?q=...)
  35. $t = $element->find('a[href]',0)->href;
  36. $item['uri'] = ''.$t;
  37. parse_str(parse_url($t, PHP_URL_QUERY),$parameters);
  38. if (isset($parameters['q'])) { $item['uri'] = $parameters['q']; }
  39. $item['title'] = $element->find('h3',0)->plaintext;
  40. $item['content'] = $element->find('span[class=st]',0)->plaintext;
  41. $this->items[] = $item;
  42. }
  43. }
  44. }
  45. public function getName(){
  46. return (!empty($this->request) ? $this->request .' - ' : '') .'Google search';
  47. }
  48. public function getCacheDuration(){
  49. return 1800; // 30 minutes
  50. }
  51. }