Raajje mv intergration
This commit is contained in:
73
app/Console/Commands/ScrapeRaajjeMvCommand.php
Normal file
73
app/Console/Commands/ScrapeRaajjeMvCommand.php
Normal file
@@ -0,0 +1,73 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace App\Console\Commands;
|
||||||
|
|
||||||
|
use App\Source;
|
||||||
|
use Illuminate\Console\Command;
|
||||||
|
use App\Services\RaajjeMvService;
|
||||||
|
use App\Topic;
|
||||||
|
use Illuminate\Support\Carbon;
|
||||||
|
|
||||||
|
class ScrapeRaajjeMvCommand extends Command
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* The name and signature of the console command.
|
||||||
|
*
|
||||||
|
* @var string
|
||||||
|
*/
|
||||||
|
protected $signature = 'scrape:raajjemv';
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The console command description.
|
||||||
|
*
|
||||||
|
* @var string
|
||||||
|
*/
|
||||||
|
protected $description = 'Scrape Raajje mv';
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Create a new command instance.
|
||||||
|
*
|
||||||
|
* @return void
|
||||||
|
*/
|
||||||
|
public function __construct()
|
||||||
|
{
|
||||||
|
parent::__construct();
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Execute the console command.
|
||||||
|
*
|
||||||
|
* @return int
|
||||||
|
*/
|
||||||
|
public function handle()
|
||||||
|
{
|
||||||
|
$source = Source::where('slug', 'raajjemv')->first();
|
||||||
|
|
||||||
|
$articles = (new RaajjeMvService)->scrape();
|
||||||
|
|
||||||
|
foreach ($articles as $article) {
|
||||||
|
|
||||||
|
// Attach the relationship between source and article and return the curren article instance
|
||||||
|
$articleModel = $source->articles()->updateOrCreate(["guid" => $article["guid"]],
|
||||||
|
[
|
||||||
|
"title" => $article["title"],
|
||||||
|
"url" => $article["url"],
|
||||||
|
"author" => $article["author"],
|
||||||
|
"featured_image" => $article["image"],
|
||||||
|
"body" => $article["content"],
|
||||||
|
"published_date" => $article["date"],
|
||||||
|
"meta" => [
|
||||||
|
"title" => $article["og_title"]
|
||||||
|
]
|
||||||
|
|
||||||
|
]);
|
||||||
|
|
||||||
|
collect($article["topics"])->each(function($topic) use ($articleModel) {
|
||||||
|
$topicModel = Topic::firstOrCreate(["slug" => $topic["slug"]],["name" => $topic["name"]]);
|
||||||
|
|
||||||
|
$topicModel->articles()->syncWithoutDetaching($articleModel);
|
||||||
|
});
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@@ -84,6 +84,10 @@ class Kernel extends ConsoleKernel
|
|||||||
$schedule->command('scrape:hama')->everyFiveMinutes()
|
$schedule->command('scrape:hama')->everyFiveMinutes()
|
||||||
->runInBackground()
|
->runInBackground()
|
||||||
->pingOnSuccess(env('APP_URL') . "/api/ping/hama");
|
->pingOnSuccess(env('APP_URL') . "/api/ping/hama");
|
||||||
|
|
||||||
|
$schedule->command('scrape:raajjemv')->everyFiveMinutes()
|
||||||
|
->runInBackground()
|
||||||
|
->pingOnSuccess(env('APP_URL') . "/api/ping/raajjemv");
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
40
app/Services/Feeds/RaajjeMvFeed.php
Normal file
40
app/Services/Feeds/RaajjeMvFeed.php
Normal 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;
|
||||||
|
}
|
||||||
|
}
|
27
app/Services/RaajjeMvService.php
Normal file
27
app/Services/RaajjeMvService.php
Normal 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;
|
||||||
|
}
|
||||||
|
}
|
57
app/Services/Scrapers/RaajjeMvScraper.php
Normal file
57
app/Services/Scrapers/RaajjeMvScraper.php
Normal 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"
|
||||||
|
]
|
||||||
|
]
|
||||||
|
];
|
||||||
|
}
|
||||||
|
}
|
Reference in New Issue
Block a user