85 lines
2.5 KiB
PHP
85 lines
2.5 KiB
PHP
<?php
|
|
|
|
namespace App\Services\Feeds;
|
|
|
|
use Goutte\Client;
|
|
use Illuminate\Support\Carbon;
|
|
|
|
class AdhadhuFeed implements Feed
|
|
{
|
|
protected $client;
|
|
|
|
public function __construct()
|
|
{
|
|
$this->client = new Client();
|
|
}
|
|
|
|
/**
|
|
* Return the latest articles from Adhadhu
|
|
*
|
|
* @return array
|
|
*/
|
|
public function get(): array
|
|
{
|
|
$crawler = $this->client->request('GET', "https://adhadhu.com/category/News", [
|
|
"proxy" => config('karudhaas.proxy.host')
|
|
]);
|
|
|
|
$feeds = [];
|
|
|
|
// Parse the news articles
|
|
$crawler->filter('div.category-news div.row div.list a.item, div.category-news div.row div.list a')->each(function ($node) use (&$feeds) {
|
|
// Extract the details of each article
|
|
$title = $node->filter('h4')->text();
|
|
$link = $node->attr('href');
|
|
$timeText = $node->filter('p.font-11')->text();
|
|
// Extract the time and convert it to a Carbon instance
|
|
$date = $this->extractDate($timeText);
|
|
|
|
$feeds[] = [
|
|
"title" => trim($title),
|
|
"link" => "https://adhadhu.com" . $link,
|
|
"date" => $date
|
|
];
|
|
});
|
|
|
|
|
|
return $feeds;
|
|
}
|
|
|
|
/**
|
|
* Extract and format the date from the text
|
|
*
|
|
* @param string $timeText
|
|
* @return string
|
|
*/
|
|
protected function extractDate($timeText)
|
|
{
|
|
// A simple regex to extract numbers and time units (e.g., "minutes", "hours")
|
|
if (preg_match('/(\d+)\s*(minute|hour|day|second)s?/', $timeText, $matches)) {
|
|
$number = $matches[1];
|
|
$unit = $matches[2];
|
|
|
|
// Use Carbon's sub method to subtract the time
|
|
switch ($unit) {
|
|
case 'minute':
|
|
return Carbon::now()->subMinutes($number)->format('Y-m-d H:i:s');
|
|
case 'hour':
|
|
return Carbon::now()->subHours($number)->format('Y-m-d H:i:s');
|
|
case 'day':
|
|
return Carbon::now()->subDays($number)->format('Y-m-d H:i:s');
|
|
case 'second':
|
|
return Carbon::now()->subSeconds($number)->format('Y-m-d H:i:s');
|
|
default:
|
|
// Handle unexpected time unit
|
|
return Carbon::now()->format('Y-m-d H:i:s');
|
|
}
|
|
} else {
|
|
// Default to current time if parsing fails
|
|
return Carbon::now()->format('Y-m-d H:i:s');
|
|
}
|
|
}
|
|
|
|
}
|
|
|