GoogleSearchBridge.php 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  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. *
  12. * @name Google search
  13. * @description Returns most recent results from Google search.
  14. * @maintainer sebsauvage
  15. * @use1(q="keyword")
  16. */
  17. class GoogleSearchBridge extends BridgeAbstract{
  18. private $request;
  19. public function collectData(array $param){
  20. $html = '';
  21. if (isset($param['q'])) { /* keyword search mode */
  22. $this->request = $param['q'];
  23. $html = file_get_html('http://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);
  24. }
  25. else{
  26. $this->returnError('You must specify a keyword (?q=...).', 400);
  27. }
  28. $emIsRes = $html->find('div[id=ires]',0);
  29. if( !is_null($emIsRes) ){
  30. foreach($emIsRes->find('li[class=g]') as $element) {
  31. $item = new Item();
  32. // Extract direct URL from google href (eg. /url?q=...)
  33. $t = $element->find('a[href]',0)->href;
  34. $item->uri = 'http://google.com'.$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 (!empty($this->request) ? $this->request .' - ' : '') .'Google search';
  45. }
  46. public function getURI(){
  47. return 'http://google.com';
  48. }
  49. public function getCacheDuration(){
  50. return 1800; // 30 minutes
  51. }
  52. }