Add (news bites)
This commit is contained in:
60
app/Jobs/Tasks/BrowseRSSLatestNewsTask.php
Normal file
60
app/Jobs/Tasks/BrowseRSSLatestNewsTask.php
Normal file
@@ -0,0 +1,60 @@
|
||||
<?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;
|
||||
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user