Files
karudhaas/app/Services/Feeds/MihaaruFeed.php
2024-01-13 04:53:31 +05:00

69 lines
1.8 KiB
PHP

<?php
namespace App\Services\Feeds;
use Illuminate\Support\Facades\Http;
use Carbon\Carbon;
class MihaaruFeed implements Feed
{
/**
* Get all the latest news
*
* @return array
*/
public function get() : array
{
$response = Http::withOptions([
'proxy' => config('karudhaas.proxy.host')
])->withHeaders([
'Referer' => 'https://mihaaru.com/?ref=mhr-lm',
])
->get('https://mihaaru.com/api/home/latest-popular-weekly?type=latest')
->json();
$feeds = [];
foreach ($response['data'] as $item) {
// Approximate the date from the human-readable format
$date = $this->approximateDateFromHumanTime($item['human_time']);
$feeds[] = [
"title" => $item['short_headline'],
"link" => $item['link'],
"date" => $date
];
}
return $feeds;
}
/**
* Approximates the date from a human-readable time format.
*
* @param string $humanTime
* @return string
*/
protected function approximateDateFromHumanTime($humanTime)
{
$now = Carbon::now();
// Example pattern: "11 hr", "1 day"
if(preg_match('/(\d+)\s*(hr|hour|day|days)/', $humanTime, $matches)) {
$number = $matches[1];
$unit = $matches[2];
switch ($unit) {
case 'hr':
case 'hour':
return $now->subHours($number)->toDateTimeString();
case 'day':
case 'days':
return $now->subDays($number)->toDateTimeString();
default:
return $now->toDateTimeString();
}
}
return $now->toDateTimeString();
}
}