googlesearch.php 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041
  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 search")
  15. */
  16. class RssBridgeGoogleSearch extends RssBridgeAbstractClass
  17. {
  18. protected $bridgeName = 'Google search';
  19. protected $bridgeURI = 'http://google.com';
  20. protected $bridgeDescription = 'Returns most recent results from Google search.';
  21. protected $cacheDuration = 30; // 30 minutes, otherwise you could get banned by Google, or stumblr upon their captcha.
  22. protected function collectData($request) {
  23. $html = '';
  24. if (isset($request['q'])) { /* keyword search mode */
  25. $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, 'no results for this query.');
  26. } else {
  27. $this->returnError(400, 'You must specify a keyword (?q=...).');
  28. }
  29. $this->items = Array();
  30. foreach($html->find('div[id=ires]',0)->find('li[class=g]') as $element) {
  31. $item['uri'] = $element->find('a[href]',0)->href;
  32. $item['title'] = $element->find('h3',0)->plaintext;
  33. $item['content'] = $element->find('span[class=st]',0)->plaintext;
  34. $this->items[] = $item;
  35. }
  36. }
  37. }
  38. $bridge = new RssBridgeGoogleSearch();
  39. $bridge->process();