BloombergBridge.php 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. <?php
  2. class BloombergBridge extends BridgeAbstract
  3. {
  4. const NAME = 'Bloomberg';
  5. const URI = 'https://www.bloomberg.com/';
  6. const DESCRIPTION = 'Trending stories from Bloomberg';
  7. const MAINTAINER = 'mdemoss';
  8. const PARAMETERS = array(
  9. 'Trending Stories' => array(),
  10. 'From Search' => array(
  11. 'q' => array(
  12. 'name' => 'Keyword',
  13. 'required' => true
  14. )
  15. )
  16. );
  17. public function getName()
  18. {
  19. switch($this->queriedContext) {
  20. case 'Trending Stories':
  21. return self::NAME . ' Trending Stories';
  22. case 'From Search':
  23. if (!is_null($this->getInput('q'))) {
  24. return self::NAME . ' Search : ' . $this->getInput('q');
  25. }
  26. break;
  27. }
  28. return parent::getName();
  29. }
  30. public function collectData()
  31. {
  32. switch($this->queriedContext) {
  33. case 'Trending Stories': // Get list of top new <article>s from the front page.
  34. $html = getSimpleHTMLDOMCached($this->getURI(), 300);
  35. $stories = $html->find('ul.top-news-v3__stories article.top-news-v3-story');
  36. break;
  37. case 'From Search': // Get list of <article> elements from search.
  38. $html = getSimpleHTMLDOMCached(
  39. $this->getURI() .
  40. 'search?sort=time:desc&page=1&query=' .
  41. urlencode($this->getInput('q')), 300
  42. );
  43. $stories = $html->find('div.search-result-items article.search-result-story');
  44. break;
  45. }
  46. foreach ($stories as $element) {
  47. $item['uri'] = $element->find('h1 a', 0)->href;
  48. if (preg_match('#^https://#i', $item['uri']) !== 1) {
  49. $item['uri'] = $this->getURI() . $item['uri'];
  50. }
  51. $articleHtml = getSimpleHTMLDOMCached($item['uri']);
  52. if (!$articleHtml) {
  53. continue;
  54. }
  55. $item['title'] = $element->find('h1 a', 0)->plaintext;
  56. $item['timestamp'] = strtotime($articleHtml->find('meta[name=iso-8601-publish-date],meta[name=date]', 0)->content);
  57. $item['content'] = $articleHtml->find('meta[name=description]', 0)->content;
  58. $this->items[] = $item;
  59. }
  60. }
  61. }