GoogleSearchBridge.php 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  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='
  26. . urlencode($this->getInput('q'))
  27. .'&num=100&complete=0&tbs=qdr:y,sbd:1')
  28. or returnServerError('No results for this query.');
  29. $emIsRes = $html->find('div[id=ires]', 0);
  30. if(!is_null($emIsRes)) {
  31. foreach($emIsRes->find('div[class=g]') as $element) {
  32. $item = array();
  33. // Extract direct URL from google href (eg. /url?q=...)
  34. $t = $element->find('a[href]', 0)->href;
  35. $item['uri'] = '' . $t;
  36. parse_str(parse_url($t, PHP_URL_QUERY), $parameters);
  37. if(isset($parameters['q'])) {
  38. $item['uri'] = $parameters['q'];
  39. }
  40. $item['title'] = $element->find('h3', 0)->plaintext;
  41. $item['content'] = $element->find('span[class=st]', 0)->plaintext;
  42. $this->items[] = $item;
  43. }
  44. }
  45. }
  46. public function getName(){
  47. if(!is_null($this->getInput('q'))) {
  48. return $this->getInput('q') . ' - Google search';
  49. }
  50. return parent::getName();
  51. }
  52. }