Skip to content

Commit

Permalink
[RoadAndTrackBridge] Major rewrite, due to the depreciation of their API
Browse files Browse the repository at this point in the history
  • Loading branch information
teromene committed May 8, 2019
1 parent a3446ae commit a2f3866
Showing 1 changed file with 42 additions and 73 deletions.
115 changes: 42 additions & 73 deletions bridges/RoadAndTrackBridge.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,88 +6,57 @@ class RoadAndTrackBridge extends BridgeAbstract {
const CACHE_TIMEOUT = 86400; // 24h
const DESCRIPTION = 'Returns the latest news from Road & Track.';

const PARAMETERS = array(
array(
'new-cars' => array(
'name' => 'New Cars',
'type' => 'checkbox',
'exampleValue' => 'checked',
'title' => 'Activate to load New Cars articles'
),
'motorsports' => array(
'name' => 'Motorsports',
'type' => 'checkbox',
'exampleValue' => 'checked',
'title' => 'Activate to load Motorsports articles'
),
'car-culture' => array(
'name' => 'Car Culture',
'type' => 'checkbox',
'exampleValue' => 'checked',
'title' => 'Activate to load Car Culture articles'
),
'car-shows' => array(
'name' => 'Car shows',
'type' => 'checkbox',
'exampleValue' => 'checked',
'title' => 'Activate to load Car shows articles'
)
)
);

const API_TOKEN = '2e18e904-d9cd-4911-b30c-1817b1e0b04b';
const SIG_URL = 'https://cloud.mazdigital.com/feeds/production/comboapp/204/api/v3/';
const GSIG_URL = 'https://dashboard.mazsystems.com/services/cf_access?app_id=204&app_type=comboapp&api_token=';

public function collectData() {

$signVal = json_decode(getContents(self::GSIG_URL . self::API_TOKEN));
$signVal = $signVal->signature;
$page = getSimpleHTMLDOM(self::URI);

$newsElements = array();
if($this->getInput('new-cars')) {
$newsElements = array_merge($newsElements,
json_decode(getContents(self::SIG_URL . '7591/item_feed' . $signVal))
);
}
if($this->getInput('motorsports')) {
$newsElements = array_merge($newsElements,
json_decode(getContents(self::SIG_URL . '7590/item_feed' . $signVal))
);
//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;
}
if($this->getInput('car-culture')) {
$newsElements = array_merge($newsElements,
json_decode(getContents(self::SIG_URL . '7588/item_feed' . $signVal))
);

}

private function fixImages($content) {

$enclosures = [];
foreach($content->find('img') as $image) {
$image->src = explode('?', $image->getAttribute('data-src'))[0];
$enclosures[] = $image->src;
}
if($this->getInput('car-shows')) {
$newsElements = array_merge($newsElements,
json_decode(getContents(self::SIG_URL . '7589/item_feed' . $signVal))
);

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

usort($newsElements, function($a, $b) {
return $b->published - $a->published;
});
return $enclosures;

$limit = 19;
foreach($newsElements as $element) {

$item = array();
$item['uri'] = $element->sourceUrl;
$item['timestamp'] = $element->published;
$item['enclosures'] = array($element->cover->url);
$item['title'] = $element->title;
$item['content'] = $this->getArticleContent($element);
$this->items[] = $item;

if($limit > 0) {
$limit--;
} else {
break;
}
}

}
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;

}

Expand Down

0 comments on commit a2f3866

Please sign in to comment.