GoogleSearchBridge.php 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. <?php
  2. /**
  3. * Returns the 100 most recent links in results in past year, sorting by date (most recent first).
  4. * Example:
  5. * http://www.google.com/search?q=sebsauvage&num=100&complete=0&tbs=qdr:y,sbd:1
  6. * complete=0&num=100 : get 100 results
  7. * qdr:y : in past year
  8. * sbd:1 : sort by date (will only work if qdr: is specified)
  9. */
  10. class GoogleSearchBridge extends BridgeAbstract{
  11. private $request;
  12. public function loadMetadatas() {
  13. $this->maintainer = "sebsauvage";
  14. $this->name = "Google search";
  15. $this->uri = "https://www.google.com/";
  16. $this->description = "Returns most recent results from Google search.";
  17. $this->update = "2014-05-25";
  18. $this->parameters[] =
  19. '[
  20. {
  21. "name" : "keyword",
  22. "identifier" : "q"
  23. }
  24. ]';
  25. }
  26. public function collectData(array $param){
  27. $html = '';
  28. if (isset($param['q'])) { /* keyword search mode */
  29. $this->request = $param['q'];
  30. $html = $this->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);
  31. }
  32. else{
  33. $this->returnError('You must specify a keyword (?q=...).', 400);
  34. }
  35. $emIsRes = $html->find('div[id=ires]',0);
  36. if( !is_null($emIsRes) ){
  37. foreach($emIsRes->find('li[class=g]') as $element) {
  38. $item = new Item();
  39. // Extract direct URL from google href (eg. /url?q=...)
  40. $t = $element->find('a[href]',0)->href;
  41. $item->uri = ''.$t;
  42. parse_str(parse_url($t, PHP_URL_QUERY),$parameters);
  43. if (isset($parameters['q'])) { $item->uri = $parameters['q']; }
  44. $item->title = $element->find('h3',0)->plaintext;
  45. $item->content = $element->find('span[class=st]',0)->plaintext;
  46. $this->items[] = $item;
  47. }
  48. }
  49. }
  50. public function getName(){
  51. return (!empty($this->request) ? $this->request .' - ' : '') .'Google search';
  52. }
  53. public function getURI(){
  54. return 'http://google.com';
  55. }
  56. public function getCacheDuration(){
  57. return 1800; // 30 minutes
  58. }
  59. }