Dhen news intergrated
This commit is contained in:
28
app/Services/DhenService.php
Normal file
28
app/Services/DhenService.php
Normal file
@@ -0,0 +1,28 @@
|
||||
<?php
|
||||
|
||||
namespace App\Services;
|
||||
|
||||
use App\Services\Scrapers\DhenScraper;
|
||||
|
||||
class DhenService 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://dhen.mv/feed")["channel"]["item"];
|
||||
|
||||
$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'];
|
||||
$articlesitems[] = (new DhenScraper)->extract($link);
|
||||
}
|
||||
|
||||
return $articlesitems;
|
||||
}
|
||||
}
|
||||
61
app/Services/Scrapers/DhenScraper.php
Normal file
61
app/Services/Scrapers/DhenScraper.php
Normal file
@@ -0,0 +1,61 @@
|
||||
<?php
|
||||
namespace App\Services\Scrapers;
|
||||
|
||||
use Goutte\Client;
|
||||
use Illuminate\Support\Carbon;
|
||||
|
||||
class DhenScraper
|
||||
{
|
||||
protected $client;
|
||||
|
||||
protected $content;
|
||||
protected $author;
|
||||
|
||||
public function __construct()
|
||||
{
|
||||
$this->client = new Client;
|
||||
}
|
||||
|
||||
public function extract($url)
|
||||
{
|
||||
|
||||
$crawler = $this->client->request('GET', $url);
|
||||
|
||||
$title = $crawler->filter('h1')->first()->text();
|
||||
|
||||
|
||||
|
||||
$image = $crawler->filter('article .article-visual img')->first()->attr('src');
|
||||
|
||||
$crawler->filter('article .article-entry p')->each(function ($node) {
|
||||
$this->content[] = preg_replace("/[a-zA-Z]/","",$node->text());
|
||||
});
|
||||
|
||||
|
||||
|
||||
if($crawler->filter(".story-item--meta a")->count() == 1)
|
||||
{
|
||||
$this->author = $crawler->filter('.story-item--meta a')->first()->text();
|
||||
}
|
||||
|
||||
//Remove all the alphabets from string
|
||||
//preg_replace("/[a-zA-Z]/", "",$string);
|
||||
return [
|
||||
'source' => 'Dhen',
|
||||
'title' => $title,
|
||||
'og_title' => $crawler->filter('meta[property*="og:title"]')->first()->attr('content'),
|
||||
'image' => $image,
|
||||
'content' => $this->content,
|
||||
'url' => $url,
|
||||
'date' => Carbon::parse(str_replace("- ", "", $crawler->filter('.story-item--meta time')->first()->text()))->format("Y-m-d H:i:s"),
|
||||
'guid' => str_replace("https://dhen.mv/","",$url),
|
||||
'author' => $this->author,
|
||||
'topics' => [
|
||||
[
|
||||
"name" => "ވަކި މަޢުލޫއެއް ނޭންގެ",
|
||||
"slug" => "uncategorized"
|
||||
]
|
||||
]
|
||||
];
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user