2021-05-06 21:16:22 +03:00
|
|
|
<?php
|
|
|
|
|
|
|
|
class Formula1Bridge extends BridgeAbstract
|
2022-07-01 16:10:30 +03:00
|
|
|
{
|
2021-05-06 21:16:22 +03:00
|
|
|
const NAME = 'Formula1 Bridge';
|
|
|
|
const URI = 'https://formula1.com/';
|
|
|
|
const DESCRIPTION = 'Returns latest official Formula 1 news';
|
|
|
|
const MAINTAINER = 'AxorPL';
|
|
|
|
|
|
|
|
const API_KEY = 'qPgPPRJyGCIPxFT3el4MF7thXHyJCzAP';
|
|
|
|
const API_URL = 'https://api.formula1.com/v1/editorial/articles?limit=%u';
|
|
|
|
|
|
|
|
const ARTICLE_AUTHOR = 'Formula 1';
|
|
|
|
const ARTICLE_URL = 'https://formula1.com/en/latest/article.%s.%s.html';
|
|
|
|
|
|
|
|
const LIMIT_MIN = 1;
|
|
|
|
const LIMIT_DEFAULT = 10;
|
|
|
|
const LIMIT_MAX = 100;
|
|
|
|
|
|
|
|
const PARAMETERS = [
|
2022-07-01 16:10:30 +03:00
|
|
|
[
|
2021-05-06 21:16:22 +03:00
|
|
|
'limit' => [
|
|
|
|
'name' => 'Limit',
|
|
|
|
'type' => 'number',
|
|
|
|
'required' => false,
|
|
|
|
'title' => 'Number of articles to return',
|
|
|
|
'exampleValue' => self::LIMIT_DEFAULT,
|
|
|
|
'defaultValue' => self::LIMIT_DEFAULT
|
2022-07-01 16:10:30 +03:00
|
|
|
]
|
|
|
|
]
|
2021-05-06 21:16:22 +03:00
|
|
|
];
|
|
|
|
|
2021-05-30 21:33:15 +03:00
|
|
|
public function collectData()
|
2022-07-01 16:10:30 +03:00
|
|
|
{
|
2021-05-30 21:33:15 +03:00
|
|
|
$limit = $this->getInput('limit') ?: self::LIMIT_DEFAULT;
|
|
|
|
$limit = min(self::LIMIT_MAX, max(self::LIMIT_MIN, $limit));
|
|
|
|
$url = sprintf(self::API_URL, $limit);
|
2021-05-06 21:16:22 +03:00
|
|
|
|
|
|
|
$json = json_decode(getContents($url, ['apikey: ' . self::API_KEY]));
|
2021-05-30 21:33:15 +03:00
|
|
|
if (property_exists($json, 'error')) {
|
|
|
|
returnServerError($json->message);
|
|
|
|
}
|
|
|
|
$list = $json->items;
|
|
|
|
|
2021-05-06 21:16:22 +03:00
|
|
|
foreach ($list as $article) {
|
2021-05-30 21:33:15 +03:00
|
|
|
if (property_exists($article->thumbnail, 'caption')) {
|
|
|
|
$caption = $article->thumbnail->caption;
|
2022-07-01 16:10:30 +03:00
|
|
|
} else {
|
2021-05-30 21:33:15 +03:00
|
|
|
$caption = $article->thumbnail->image->title;
|
2022-07-01 16:10:30 +03:00
|
|
|
}
|
|
|
|
|
2021-05-06 21:16:22 +03:00
|
|
|
$item = [];
|
|
|
|
$item['uri'] = sprintf(self::ARTICLE_URL, $article->slug, $article->id);
|
|
|
|
$item['title'] = $article->title;
|
|
|
|
$item['timestamp'] = $article->updatedAt;
|
|
|
|
$item['author'] = self::ARTICLE_AUTHOR;
|
|
|
|
$item['enclosures'] = [$article->thumbnail->image->url];
|
|
|
|
$item['uid'] = $article->id;
|
|
|
|
$item['content'] = sprintf(
|
2022-09-09 21:18:50 +03:00
|
|
|
'<p>%s</p><a href="%s" target="_blank"><img src="%s" alt="%s" title="%s"></a>',
|
|
|
|
$article->metaDescription ?? $article->title,
|
2021-05-30 21:33:15 +03:00
|
|
|
$item['uri'],
|
2021-05-06 21:16:22 +03:00
|
|
|
$item['enclosures'][0],
|
2021-05-30 21:33:15 +03:00
|
|
|
$caption,
|
|
|
|
$caption
|
2021-05-06 21:16:22 +03:00
|
|
|
);
|
|
|
|
$this->items[] = $item;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|