summaryrefslogtreecommitdiff
path: root/bridges/BloombergBridge.php
blob: 9eb1219189fb63c71c6db3b54a5276673cd97d18 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
<?php
class BloombergBridge extends BridgeAbstract
{
	const NAME = 'Bloomberg';
	const URI = 'https://www.bloomberg.com/';
	const DESCRIPTION = 'Trending stories from Bloomberg';
	const MAINTAINER = 'mdemoss';

	const PARAMETERS = array(
	'Trending Stories' => array(),
	'From Search' => array(
	'q' => array(
				'name' => 'Keyword',
				'required' => true
	)
	)
	);

	public function getName()
	{
		switch($this->queriedContext) {
		case 'Trending Stories':
			return self::NAME . ' Trending Stories';
		case 'From Search':
			if (!is_null($this->getInput('q'))) {
					return self::NAME . ' Search : ' . $this->getInput('q');
			}
			break;
		}

		return parent::getName();
	}

	public function getIcon() {
		return 'https://assets.bwbx.io/s3/javelin/public/hub/images/favicon-black-63fe5249d3.png';
	}

	public function collectData()
	{
		switch($this->queriedContext) {
		case 'Trending Stories': // Get list of top new <article>s from the front page.
			$html = getSimpleHTMLDOMCached($this->getURI(), 300);
			$stories = $html->find('ul.top-news-v3__stories article.top-news-v3-story');
			break;
		case 'From Search': // Get list of <article> elements from search.
			$html = getSimpleHTMLDOMCached(
				$this->getURI() .
				'search?sort=time:desc&page=1&query=' .
				urlencode($this->getInput('q')), 300
			);
			$stories = $html->find('div.search-result-items article.search-result-story');
			break;
		}
		foreach ($stories as $element) {
			$item['uri'] = $element->find('h1 a', 0)->href;
			if (preg_match('#^https://#i', $item['uri']) !== 1) {
				$item['uri'] = $this->getURI() . $item['uri'];
			}
			$articleHtml = getSimpleHTMLDOMCached($item['uri']);
			if (!$articleHtml) {
				continue;
			}
			$item['title'] = $element->find('h1 a', 0)->plaintext;
			$item['timestamp'] = strtotime($articleHtml->find('meta[name=iso-8601-publish-date],meta[name=date]', 0)->content);
			$item['content'] = $articleHtml->find('meta[name=description]', 0)->content;
			$this->items[] = $item;
		}
	}
}