Dhiyares intergration
This commit is contained in:
74
app/Console/Commands/ScrapeDhiyaresCommand.php
Normal file
74
app/Console/Commands/ScrapeDhiyaresCommand.php
Normal file
@@ -0,0 +1,74 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace App\Console\Commands;
|
||||||
|
|
||||||
|
use Illuminate\Console\Command;
|
||||||
|
use App\Source;
|
||||||
|
use App\Services\DhiyaresService;
|
||||||
|
use App\Topic;
|
||||||
|
use Illuminate\Support\Carbon;
|
||||||
|
|
||||||
|
class ScrapeDhiyaresCommand extends Command
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* The name and signature of the console command.
|
||||||
|
*
|
||||||
|
* @var string
|
||||||
|
*/
|
||||||
|
protected $signature = 'scrape:dhiyares';
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The console command description.
|
||||||
|
*
|
||||||
|
* @var string
|
||||||
|
*/
|
||||||
|
protected $description = 'Scrape Dhiyares';
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 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', 'dhiyares')->first();
|
||||||
|
|
||||||
|
$articles = (new DhiyaresService)->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);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@@ -100,6 +100,10 @@ class Kernel extends ConsoleKernel
|
|||||||
$schedule->command('scrape:jazeera')->everyFiveMinutes()
|
$schedule->command('scrape:jazeera')->everyFiveMinutes()
|
||||||
->runInBackground()
|
->runInBackground()
|
||||||
->pingOnSuccess(env('APP_URL') . "/api/ping/jazeera");
|
->pingOnSuccess(env('APP_URL') . "/api/ping/jazeera");
|
||||||
|
|
||||||
|
$schedule->command('scrape:dhiyares')->everyFiveMinutes()
|
||||||
|
->runInBackground()
|
||||||
|
->pingOnSuccess(env('APP_URL') . "/api/ping/dhiyares");
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
28
app/Services/DhiyaresService.php
Normal file
28
app/Services/DhiyaresService.php
Normal file
@@ -0,0 +1,28 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace App\Services;
|
||||||
|
|
||||||
|
use App\Services\Feeds\DhiyaresFeed;
|
||||||
|
use App\Services\Scrapers\DhiyaresScraper;
|
||||||
|
|
||||||
|
class DhiyaresService extends Client
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* Scrap all the rss articles from Thiladhun
|
||||||
|
*
|
||||||
|
* @return array
|
||||||
|
*/
|
||||||
|
public function scrape(): array
|
||||||
|
{
|
||||||
|
//Return only the rss that contains "news" keyboard in its url
|
||||||
|
$articles = (new DhiyaresFeed)->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 DhiyaresScraper)->extract($article["link"], $article["date"]);
|
||||||
|
}
|
||||||
|
|
||||||
|
return $articlesitems;
|
||||||
|
}
|
||||||
|
}
|
39
app/Services/Feeds/DhiyaresFeed.php
Normal file
39
app/Services/Feeds/DhiyaresFeed.php
Normal file
@@ -0,0 +1,39 @@
|
|||||||
|
<?php
|
||||||
|
namespace App\Services\Feeds;
|
||||||
|
|
||||||
|
use Goutte\Client;
|
||||||
|
|
||||||
|
class DhiyaresFeed 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://dhiyares.com");
|
||||||
|
|
||||||
|
$feeds = [];
|
||||||
|
$crawler->filter('.recent-list')->each(function ($node) use (&$feeds) {
|
||||||
|
|
||||||
|
|
||||||
|
$feeds[] = [
|
||||||
|
"title" => $node->filter('h3')->text(),
|
||||||
|
"link" => $node->filter('a')->attr('href'),
|
||||||
|
"date" => $node->filter('time-comment')->first()->attr('datetime')
|
||||||
|
];
|
||||||
|
|
||||||
|
});
|
||||||
|
|
||||||
|
return $feeds;
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
66
app/Services/Scrapers/DhiyaresScraper.php
Normal file
66
app/Services/Scrapers/DhiyaresScraper.php
Normal file
@@ -0,0 +1,66 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace App\Services\Scrapers;
|
||||||
|
|
||||||
|
use Goutte\Client;
|
||||||
|
use Illuminate\Support\Carbon;
|
||||||
|
|
||||||
|
class DhiyaresScraper
|
||||||
|
{
|
||||||
|
protected $client;
|
||||||
|
|
||||||
|
protected $title;
|
||||||
|
protected $content;
|
||||||
|
protected $topics = [];
|
||||||
|
protected $author = "unknown";
|
||||||
|
|
||||||
|
public function __construct()
|
||||||
|
{
|
||||||
|
$this->client = new Client;
|
||||||
|
}
|
||||||
|
|
||||||
|
public function extract($url, $date)
|
||||||
|
{
|
||||||
|
|
||||||
|
$crawler = $this->client->request('GET', $url);
|
||||||
|
|
||||||
|
$crawler->filter('.content p')->each(function ($node) {
|
||||||
|
$this->content[] = $node->text();
|
||||||
|
});
|
||||||
|
|
||||||
|
$crawler->filter('a[class*="bg-blue-50 border border-blue-50 font-normal text-blue-400 hover:text-white hover:bg-base p-1 px-4 mx-2 mb-4 rounded-full font-mv-bold text-base"]')->each(function ($node) {
|
||||||
|
if(!preg_match('/[^A-Za-z0-9-]/', basename($node->attr('href'))))
|
||||||
|
{
|
||||||
|
$this->topics[] = [
|
||||||
|
"name" => $node->text(),
|
||||||
|
"slug" => basename($node->attr('href'))
|
||||||
|
];
|
||||||
|
}
|
||||||
|
|
||||||
|
});
|
||||||
|
|
||||||
|
|
||||||
|
if ($crawler->filter('div[class*="text-base font-mv-bold"] a[class*="py-4"]')->count() == 1) {
|
||||||
|
$this->author = $crawler->filter('div[class*="text-base font-mv-bold"] a[class*="py-4"]')->first()->text();
|
||||||
|
}
|
||||||
|
//Remove all the alphabets from string
|
||||||
|
//preg_replace("/[a-zA-Z]/", "",$string);
|
||||||
|
return [
|
||||||
|
'source' => 'Dhiyares',
|
||||||
|
'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' => $this->topics ? : [
|
||||||
|
[
|
||||||
|
"name" => "ވަކި މަޢުލޫއެއް ނޭންގެ",
|
||||||
|
"slug" => "no-specific-topic"
|
||||||
|
]
|
||||||
|
]
|
||||||
|
];
|
||||||
|
}
|
||||||
|
}
|
Reference in New Issue
Block a user