Psm Support
This commit is contained in:
73
app/Console/Commands/ScrapePsmCommand.php
Normal file
73
app/Console/Commands/ScrapePsmCommand.php
Normal file
@@ -0,0 +1,73 @@
|
||||
<?php
|
||||
|
||||
namespace App\Console\Commands;
|
||||
|
||||
use App\Source;
|
||||
use Illuminate\Console\Command;
|
||||
use App\Services\PsmService;
|
||||
use App\Topic;
|
||||
use Illuminate\Support\Carbon;
|
||||
|
||||
class ScrapePsmCommand extends Command
|
||||
{
|
||||
/**
|
||||
* The name and signature of the console command.
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected $signature = 'scrape:psm';
|
||||
|
||||
/**
|
||||
* The console command description.
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected $description = 'Scrape PSM News';
|
||||
|
||||
/**
|
||||
* 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', 'psm')->first();
|
||||
|
||||
$articles = (new PsmService)->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" => Carbon::parse($article["date"])->format("Y-m-d H:i:s"),
|
||||
"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);
|
||||
});
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -51,6 +51,9 @@ class Kernel extends ConsoleKernel
|
||||
|
||||
$schedule->command('scrape:dhen')->everyFiveMinutes()
|
||||
->pingOnSuccess(env('APP_URL')."/api/ping/dhen");
|
||||
|
||||
$schedule->command('scrape:psm')->everyFiveMinutes()
|
||||
->pingOnSuccess(env('APP_URL')."/api/ping/psm");
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
29
app/Services/PsmService.php
Normal file
29
app/Services/PsmService.php
Normal file
@@ -0,0 +1,29 @@
|
||||
<?php
|
||||
|
||||
namespace App\Services;
|
||||
|
||||
use App\Services\Scrapers\PsmScraper;
|
||||
use Illuminate\Support\Str;
|
||||
|
||||
class PsmService extends Client
|
||||
{
|
||||
/**
|
||||
* Scrap all the rss articles from mihaaru
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function scrape(): array
|
||||
{
|
||||
$articles = $this->get("https://www.psmnews.mv/feed")["entry"];
|
||||
|
||||
$articlesitems = [];
|
||||
//Looping through the articles and scraping and while scraping it creates a new instance of the scraper.
|
||||
foreach ($articles as $article) {
|
||||
$link = $article['id'];
|
||||
$date = $article['updated'];
|
||||
$articlesitems[] = (new PsmScraper)->extract($link, $date);
|
||||
}
|
||||
|
||||
return $articlesitems;
|
||||
}
|
||||
}
|
||||
59
app/Services/Scrapers/PsmScraper.php
Normal file
59
app/Services/Scrapers/PsmScraper.php
Normal file
@@ -0,0 +1,59 @@
|
||||
<?php
|
||||
|
||||
namespace App\Services\Scrapers;
|
||||
|
||||
use Goutte\Client;
|
||||
|
||||
class PsmScraper
|
||||
{
|
||||
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('h1')->each(function ($node) {
|
||||
$this->title = $node->text();
|
||||
});
|
||||
|
||||
$crawler->filter('.content > p')->each(function ($node) {
|
||||
$this->content[] = preg_replace("/[a-zA-Z]/", "", $node->text());
|
||||
});
|
||||
|
||||
|
||||
if($crawler->filter('div[class*="flex items-center border-grey-light border-solid"] p a')->count() == 1)
|
||||
{
|
||||
$this->author = $crawler->filter('div[class*="flex items-center border-grey-light border-solid"] p a')->first()->text();
|
||||
}
|
||||
//Remove all the alphabets from string
|
||||
//preg_replace("/[a-zA-Z]/", "",$string);
|
||||
return [
|
||||
'source' => 'PSM',
|
||||
'title' => $this->title,
|
||||
'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' => $date,
|
||||
'guid' => str_replace("https://www.psmnews.mv/", "", $url),
|
||||
'author' => $this->author,
|
||||
'topics' => [
|
||||
[
|
||||
"name" => "ވަކި މަޢުލޫއެއް ނޭންގެ",
|
||||
"slug" => "no-specific-topic"
|
||||
]
|
||||
]
|
||||
];
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user