rss-bridge-googlesearch.php 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041
  1. <?php
  2. require_once('rss-bridge-lib.php');
  3. /**
  4. * RssBridgeGoogleMostRecent
  5. * Search Google for most recent pages regarding a specific topic.
  6. * Returns the 100 most recent links in results in past year,
  7. * sorting by date (most recent first).
  8. * Example:
  9. * http://www.google.com/search?q=sebsauvage&num=100&complete=0&tbs=qdr:y,sbd:1
  10. * complete=0&num=100 : get 100 results
  11. * qdr:y : in past year
  12. * sbd:1 : sort by date (will only work if qdr: is specified)
  13. */
  14. class RssBridgeGoogleSearch extends RssBridgeAbstractClass
  15. {
  16. protected $bridgeName = 'Google search';
  17. protected $bridgeURI = 'http://google.com';
  18. protected $bridgeDescription = 'Returns most recent results from Google search.';
  19. protected $cacheDuration = 30; // 30 minutes, otherwise you could get banned by Google, or stumblr upon their captcha.
  20. protected function collectData($request) {
  21. $html = '';
  22. if (isset($request['q'])) { /* keyword search mode */
  23. $html = file_get_html('http://www.google.com/search?q='.urlencode($request['q']).'&num=100&complete=0&tbs=qdr:y,sbd:1') or $this->returnError('404 Not Found', 'ERROR: no results for this query.');
  24. } else {
  25. $this->returnError('400 Bad Request', 'ERROR: You must specify a keyword (?q=...).');
  26. }
  27. $this->items = Array();
  28. foreach($html->find('div[id=ires]',0)->find('li[class=g]') as $element) {
  29. $item['uri'] = $element->find('a[href]',0)->href;
  30. $item['title'] = $element->find('h3',0)->plaintext;
  31. $item['content'] = $element->find('span[class=st]',0)->plaintext;
  32. $this->items[] = $item;
  33. }
  34. }
  35. }
  36. $bridge = new RssBridgeGoogleSearch();
  37. $bridge->process();
  38. ?>