GoogleSearchBridge.php 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  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. * @use1(q="keyword")
  15. */
  16. class GoogleSearchBridge extends BridgeAbstract{
  17. public function collectData(array $param){
  18. $html = '';
  19. if (isset($param['q'])) { /* keyword search mode */
  20. $html = file_get_html('http://www.google.com/search?q=' . urlencode($param['q']) . '&num=100&complete=0&tbs=qdr:y,sbd:1') or $this->returnError('No results for this query.', 404);
  21. }
  22. else{
  23. $this->returnError('You must specify a keyword (?q=...).', 400);
  24. }
  25. $emIsRes = $html->find('div[id=ires]',0);
  26. if( !is_null($emIsRes) ){
  27. foreach($emIsRes->find('li[class=g]') as $element) {
  28. $item = new Item();
  29. // Extract direct URL from google href (eg. /url?q=...)
  30. $t = $element->find('a[href]',0)->href;
  31. $item->uri = 'http://google.com'.$t;
  32. parse_str(parse_url($t, PHP_URL_QUERY),$parameters);
  33. if (isset($parameters['q'])) { $item->uri = $parameters['q']; }
  34. $item->title = $element->find('h3',0)->plaintext;
  35. $item->content = $element->find('span[class=st]',0)->plaintext;
  36. $this->items[] = $item;
  37. }
  38. }
  39. }
  40. public function getName(){
  41. return 'Google search';
  42. }
  43. public function getURI(){
  44. return 'http://google.com';
  45. }
  46. public function getCacheDuration(){
  47. return 1800; // 30 minutes
  48. }
  49. }