FUnadhoo times support
This commit is contained in:
30
app/Services/FunadhooTimesService.php
Normal file
30
app/Services/FunadhooTimesService.php
Normal file
@@ -0,0 +1,30 @@
|
||||
<?php
|
||||
|
||||
namespace App\Services;
|
||||
|
||||
use App\Services\Scrapers\FunadhooTimesScraper;
|
||||
use Illuminate\Support\Str;
|
||||
|
||||
|
||||
class FunadhooTimesService extends Client
|
||||
{
|
||||
/**
|
||||
* Scrap all the rss articles from Press
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function scrape(): array
|
||||
{
|
||||
//Return only the rss that contains "news" keyboard in its url
|
||||
$articles = $this->get("https://funadhootimes.com/rss.xml")["channel"]["article"];
|
||||
$articlesitems = [];
|
||||
//Looping through the articles and scraping and while scraping it creates a new instance of the scraper.
|
||||
foreach ($articles as $article) {
|
||||
$link = $article['link'];
|
||||
$title = $article['title'];
|
||||
$articlesitems[] = (new FunadhooTimesScraper)->extract($link, $title);
|
||||
}
|
||||
|
||||
return $articlesitems;
|
||||
}
|
||||
}
|
||||
60
app/Services/Scrapers/FunadhooTimesScraper.php
Normal file
60
app/Services/Scrapers/FunadhooTimesScraper.php
Normal file
@@ -0,0 +1,60 @@
|
||||
<?php
|
||||
|
||||
namespace App\Services\Scrapers;
|
||||
|
||||
use Exception;
|
||||
use Goutte\Client;
|
||||
use Illuminate\Support\Carbon;
|
||||
|
||||
class FunadhooTimesScraper
|
||||
{
|
||||
protected $client;
|
||||
|
||||
protected $title;
|
||||
protected $content;
|
||||
protected $topics;
|
||||
|
||||
public function __construct()
|
||||
{
|
||||
$this->client = new Client;
|
||||
}
|
||||
|
||||
public function extract($url,$title)
|
||||
{
|
||||
$crawler = $this->client->request('GET', $url);
|
||||
|
||||
$crawler->filter('.card-body > p')->each(function ($node) {
|
||||
$this->content[] = $node->text();
|
||||
});
|
||||
|
||||
|
||||
$crawler->filter('.article-tags a')->each(function ($node) {
|
||||
$this->topics[] = [
|
||||
"name" => $node->text(),
|
||||
"slug" => str_replace("https://hama.mv/", "", $node->attr('href'))
|
||||
];
|
||||
});
|
||||
|
||||
|
||||
//Remove all the alphabets from string
|
||||
//preg_replace("/[a-zA-Z]/", "",$string);
|
||||
return [
|
||||
'source' => 'Funadhoo times',
|
||||
'title' => $title,
|
||||
'og_title' => $crawler->filter('meta[property*="og:title"]')->first()->attr('content'),
|
||||
'image' => "https://funadhootimes.com/".$crawler->filter(".card img")->first()->attr('src'),
|
||||
'content' => $this->content,
|
||||
'url' => $url,
|
||||
'date' => Carbon::parse(explode('|',$crawler->filter('div[class*="d-flex flex-row-reverse justify-content-between"] small')->first()->text())[0])->format("Y-m-d H:i:s"),
|
||||
'guid' => str_replace("https://www.funadhootimes.com/p.php?id=","",$url),
|
||||
'author' => explode('|',$crawler->filter('div[class*="d-flex flex-row-reverse justify-content-between"] small')->first()->text())[1],
|
||||
'topics' => [
|
||||
[
|
||||
"name" => "ވަކި މަޢުލޫއެއް ނޭންގެ",
|
||||
"slug" => "no-specific-topic"
|
||||
]
|
||||
]
|
||||
];
|
||||
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user