Files
futurewalker/app/Jobs/Tasks/BrowseRSSLatestNewsTask.php
2023-11-21 19:18:11 +08:00

61 lines
1.4 KiB
PHP

<?php
namespace App\Jobs\Tasks;
use Carbon\Carbon;
use Vedmant\FeedReader\Facades\FeedReader;
class BrowseRSSLatestNewsTask
{
public static function handleMulti($hours = 3)
{
$rss_urls = config('platform.global.rss');
$raw_posts = [];
foreach ($rss_urls as $rss_url) {
$this_rss_posts = array_merge(self::handleSingle($rss_url, $hours));
foreach ($this_rss_posts as $item) {
$raw_posts[] = $item;
}
}
return $raw_posts;
}
public static function handleSingle($rss_url, $hours = 3)
{
$f = FeedReader::read($rss_url);
$raw_posts = [];
foreach ($f->get_items() as $item) {
$post_datetime = Carbon::parse($item->get_date(\DateTime::ATOM));
if (! $post_datetime->isBetween(now()->subHours($hours), now())) {
continue;
}
$title = trim($item->get_title());
$description = trim($item->get_content());
$raw_posts[] = (object) [
'source' => $f->get_title(),
'source_url' => $rss_url,
'title' => $title,
'link' => $item->get_link(),
'description' => $description,
'date' => $post_datetime,
'category' => $item->get_category()?->term,
];
}
unset($f);
return $raw_posts;
}
}