GoogleSearchBridge.php 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. <?php
  2. /**
  3. * RssBridgeGoogleMostRecent
  4. * Search Google for most recent pages regarding a specific topic.
  5. * Returns the 100 most recent links in results in past year, sorting by date (most recent first).
  6. * Example:
  7. * http://www.google.com/search?q=sebsauvage&num=100&complete=0&tbs=qdr:y,sbd:1
  8. * complete=0&num=100 : get 100 results
  9. * qdr:y : in past year
  10. * sbd:1 : sort by date (will only work if qdr: is specified)
  11. * 2014-05-25
  12. *
  13. * @name Google search
  14. * @homepage https://www.google.com/
  15. * @description Returns most recent results from Google search.
  16. * @maintainer sebsauvage
  17. * @use1(q="keyword")
  18. */
  19. class GoogleSearchBridge extends BridgeAbstract{
  20. private $request;
  21. public function collectData(array $param){
  22. $html = '';
  23. if (isset($param['q'])) { /* keyword search mode */
  24. $this->request = $param['q'];
  25. $html = file_get_html('https://www.google.com/search?q=' . urlencode($this->request) . '&num=100&complete=0&tbs=qdr:y,sbd:1') or $this->returnError('No results for this query.', 404);
  26. }
  27. else{
  28. $this->returnError('You must specify a keyword (?q=...).', 400);
  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 = new Item();
  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 getURI(){
  49. return 'http://google.com';
  50. }
  51. public function getCacheDuration(){
  52. return 1800; // 30 minutes
  53. }
  54. }