Raajje mv intergration

This commit is contained in:
2020-10-15 06:47:05 +05:00
parent fff50a36ac
commit 79dca057a9
5 changed files with 201 additions and 0 deletions

View File

@@ -0,0 +1,40 @@
<?php
namespace App\Services\Feeds;
use Goutte\Client;
class RaajjeMvFeed implements Feed
{
protected $client;
public function __construct()
{
$this->client = new Client();
}
/**
* Get all the latest news
*
* @return array
*/
public function get(): array
{
$crawler = $this->client->request('GET', "https://raajje.mv/");
$feeds = [];
$articles = $crawler->filter('div[v-if*="homepage.emme_fas"] article-grid')->first()->attr(':collection');
$raw_response = json_decode(html_entity_decode($articles), true);
foreach($raw_response as $response)
{
$feeds[] = [
"title" => $response["heading"],
"link" => "https://raajje.mv/".$response["id"],
"date" => $response["approved_date"]
];
}
return $feeds;
}
}

View File

@@ -0,0 +1,27 @@
<?php
namespace App\Services;
use App\Services\Feeds\RaajjeMvFeed;
use App\Services\Scrapers\RaajjeMvScraper;
class RaajjeMvService
{
/**
* Scrap all the rss articles from Sun
*
* @return array
*/
public function scrape(): array
{
//Return only the rss that contains "news" keyboard in its url
$articles = (new RaajjeMvFeed)->get();
$articlesitems = [];
//Looping through the articles and scraping and while scraping it creates a new instance of the scraper.
foreach ($articles as $article) {
$articlesitems[] = (new RaajjeMvScraper)->extract($article["link"], $article["date"]);
}
return $articlesitems;
}
}

View File

@@ -0,0 +1,57 @@
<?php
namespace App\Services\Scrapers;
use Exception;
use Goutte\Client;
use Illuminate\Support\Carbon;
use App\Helpers\Date;
class RaajjeMvScraper
{
protected $client;
protected $title;
protected $content;
protected $author = "unknown";
public function __construct()
{
$this->client = new Client;
}
public function extract($url, $date)
{
$crawler = $this->client->request('GET', $url);
$crawler->filter('div[id*="article-content"] div > p')->each(function ($node) {
$this->content[] = $node->text();
});
if ($crawler->filter('.twitter-user')->count() > 0) {
$this->author = $crawler->filter('.twitter-user')->first()->text();
}
//Remove all the alphabets from string
//preg_replace("/[a-zA-Z]/", "",$string);
return [
'source' => 'RaajjeMv',
'title' => $crawler->filter('h1')->first()->text(),
'og_title' => $crawler->filter('meta[property*="og:title"]')->first()->attr('content'),
'image' => $crawler->filter("figure > img")->first()->attr('src'),
'content' => $this->content,
'url' => $url,
'date' => Carbon::parse($date)->format("Y-m-d H:i:s"),
'guid' => basename($url),
'author' => $this->author,
'topics' => [
[
"name" => "ވަކި މަޢުލޫއެއް ނޭންގެ",
"slug" => "no-specific-topic"
]
]
];
}
}