rss-bridge/bridges/CarThrottleBridge.php

128 lines
3.6 KiB
PHP
Raw Normal View History

<?php
2023-08-25 13:34:35 +03:00
class CarThrottleBridge extends BridgeAbstract
{
2023-08-25 13:34:35 +03:00
const NAME = 'Car Throttle';
const URI = 'https://www.carthrottle.com/';
const DESCRIPTION = 'Get the latest car-related news from Car Throttle.';
const MAINTAINER = 't0stiman';
2024-02-04 20:28:12 +03:00
const DONATION_URI = 'https://ko-fi.com/tostiman';
const PARAMETERS = [
'Show articles from these categories:' => [
'news' => [
'name' => 'news',
'type' => 'checkbox'
],
'reviews' => [
'name' => 'reviews',
'type' => 'checkbox'
],
'features' => [
'name' => 'features',
'type' => 'checkbox'
],
'videos' => [
'name' => 'videos',
'type' => 'checkbox'
],
'gaming' => [
'name' => 'gaming',
'type' => 'checkbox'
]
]
];
public function collectData()
{
2024-02-04 20:28:12 +03:00
$this->items = [];
$this->handleCategory('news');
$this->handleCategory('reviews');
$this->handleCategory('features');
$this->handleCategory2('videos', 'video');
$this->handleCategory('gaming');
}
2024-02-04 20:28:12 +03:00
private function handleCategory($category)
{
if ($this->getInput($category)) {
$this->getArticles($category);
}
}
private function handleCategory2($categoryParameter, $categoryURLname)
{
if ($this->getInput($categoryParameter)) {
$this->getArticles($categoryURLname);
}
}
private function getArticles($category)
{
$categoryPage = getSimpleHTMLDOMCached(self::URI . $category);
2023-08-25 13:34:35 +03:00
//for each post
2024-02-04 20:28:12 +03:00
foreach ($categoryPage->find('div.cmg-card') as $post) {
2023-08-25 13:34:35 +03:00
$item = [];
2024-02-04 20:28:12 +03:00
$titleElement = $post->find('div.title a')[0];
$post_uri = self::URI . $titleElement->getAttribute('href');
if (!isset($post_uri) || $post_uri == '') {
continue;
}
$item['uri'] = $post_uri;
2023-08-25 13:34:35 +03:00
$item['title'] = $titleElement->innertext;
2023-12-21 11:24:22 +03:00
$articlePage = getSimpleHTMLDOMCached($item['uri']);
2024-02-04 20:28:12 +03:00
$item['author'] = $this->parseAuthor($articlePage);
$articleElement = $articlePage->find('article')[0];
2023-08-25 13:34:35 +03:00
//remove ads
2024-02-04 20:28:12 +03:00
foreach ($articleElement->find('aside') as $ad) {
$ad->outertext = '';
}
$summary = $articleElement->find('div.summary')[0];
//remove header so we are left with the article content
foreach ($articleElement->find('header') as $found) {
$found->outertext = '';
}
//remove comments (registering on carthrottle.com is impossible so the comment sections are empty anyway)
foreach ($articleElement->find('#lbs-comments') as $found) {
$found->outertext = '';
}
2024-02-04 20:28:12 +03:00
//these are supposed to be hidden
foreach ($articleElement->find('.visually-hidden') as $found) {
$found->outertext = '';
}
2023-10-12 20:49:04 +03:00
2024-02-04 20:28:12 +03:00
$item['content'] = $summary . $articleElement;
2023-08-25 13:34:35 +03:00
array_push($this->items, $item);
}
}
2024-02-04 20:28:12 +03:00
private function parseAuthor($articlePage)
{
$authorDivs = $articlePage->find('div address');
if (!$authorDivs) {
return '';
}
$a = $authorDivs[0]->find('a');
if ($a) {
return $a->innertext;
}
return $authorDivs[0]->innertext;
}
}