summaryrefslogtreecommitdiff
path: root/bridges/RoadAndTrackBridge.php
blob: b3f0acc0025ca1e5c58e4f60fdc453702b38b158 (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
<?php
class RoadAndTrackBridge extends BridgeAbstract {
	const MAINTAINER = 'teromene';
	const NAME = 'Road And Track Bridge';
	const URI = 'https://www.roadandtrack.com/';
	const CACHE_TIMEOUT = 86400; // 24h
	const DESCRIPTION = 'Returns the latest news from Road & Track.';

	public function collectData() {

		$page = getSimpleHTMLDOM(self::URI);

		//Process the first element
		$firstArticleLink = $page->find('.custom-promo-title', 0)->href;
		$this->items[] = $this->fetchArticle($firstArticleLink);

		$limit = 19;
		foreach($page->find('.full-item-title') as $article) {
			$this->items[] = $this->fetchArticle($article->href);
			$limit -= 1;
			if($limit == 0) break;
		}

	}

	private function fixImages($content) {

		$enclosures = [];
		foreach($content->find('img') as $image) {
			$image->src = explode('?', $image->getAttribute('data-src'))[0];
			$enclosures[] = $image->src;
		}

		foreach($content->find('.embed-image-wrap, .content-lede-image-wrap') as $imgContainer) {
			$imgContainer->style = '';
		}

		return $enclosures;

	}

	private function fetchArticle($articleLink) {

		$articleLink = self::URI . $articleLink;
		$article = getSimpleHTMLDOM($articleLink);
		$item = array();

		$item['title'] = $article->find('.content-hed', 0)->innertext;
		$item['author'] = $article->find('.byline-name', 0)->innertext;
		$item['timestamp'] = strtotime($article->find('.content-info-date', 0)->getAttribute('datetime'));

		$content = $article->find('.content-container', 0);
		if($content->find('.content-rail', 0) !== null)
			$content->find('.content-rail', 0)->innertext = '';
		$enclosures = $this->fixImages($content);

		$item['enclosures'] = $enclosures;
		$item['content'] = $content;
		return $item;

	}

	private function getArticleContent($article) {

		return getContents($article->contentUrl);

	}
}