Feshun Support
This commit is contained in:
72
app/Console/Commands/ScrapeFeshunCommand.php
Normal file
72
app/Console/Commands/ScrapeFeshunCommand.php
Normal file
@@ -0,0 +1,72 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace App\Console\Commands;
|
||||||
|
|
||||||
|
use Illuminate\Console\Command;
|
||||||
|
use App\Services\FeshunService;
|
||||||
|
use App\Topic;
|
||||||
|
use App\Source;
|
||||||
|
|
||||||
|
class ScrapeFeshunCommand extends Command
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* The name and signature of the console command.
|
||||||
|
*
|
||||||
|
* @var string
|
||||||
|
*/
|
||||||
|
protected $signature = 'scrape:feshun';
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The console command description.
|
||||||
|
*
|
||||||
|
* @var string
|
||||||
|
*/
|
||||||
|
protected $description = 'Scrape Feshun';
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 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', 'feshun')->first();
|
||||||
|
|
||||||
|
$articles = (new FeshunService)->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);
|
||||||
|
});
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@@ -57,6 +57,9 @@ class Kernel extends ConsoleKernel
|
|||||||
|
|
||||||
$schedule->command('scrape:oneonline')->everyFiveMinutes()
|
$schedule->command('scrape:oneonline')->everyFiveMinutes()
|
||||||
->pingOnSuccess(env('APP_URL') . "/api/ping/oneonline");
|
->pingOnSuccess(env('APP_URL') . "/api/ping/oneonline");
|
||||||
|
|
||||||
|
$schedule->command('scrape:feshun')->everyFiveMinutes()
|
||||||
|
->pingOnSuccess(env('APP_URL') . "/api/ping/feshun");
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
26
app/Services/FeshunService.php
Normal file
26
app/Services/FeshunService.php
Normal file
@@ -0,0 +1,26 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace App\Services;
|
||||||
|
|
||||||
|
use App\Services\Scrapers\FeshunScraper;
|
||||||
|
|
||||||
|
class FeshunService extends Client
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* Scrap all the rss articles from Thiladhun
|
||||||
|
*
|
||||||
|
* @return array
|
||||||
|
*/
|
||||||
|
public function scrape(): array
|
||||||
|
{
|
||||||
|
$articles = $this->get("https://feshun.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) {
|
||||||
|
$articlesitems[] = (new FeshunScraper)->extract($article['link'], $article['pubDate']);
|
||||||
|
}
|
||||||
|
|
||||||
|
return $articlesitems;
|
||||||
|
}
|
||||||
|
}
|
75
app/Services/Scrapers/FeshunScraper.php
Normal file
75
app/Services/Scrapers/FeshunScraper.php
Normal file
@@ -0,0 +1,75 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace App\Services\Scrapers;
|
||||||
|
|
||||||
|
use Goutte\Client;
|
||||||
|
use Illuminate\Support\Carbon;
|
||||||
|
|
||||||
|
class FeshunScraper
|
||||||
|
{
|
||||||
|
protected $client;
|
||||||
|
|
||||||
|
protected $title;
|
||||||
|
protected $content;
|
||||||
|
protected $guid;
|
||||||
|
protected $author;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* __construct.
|
||||||
|
*
|
||||||
|
* @return void
|
||||||
|
*/
|
||||||
|
public function __construct()
|
||||||
|
{
|
||||||
|
$this->client = new Client();
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* extract.
|
||||||
|
*
|
||||||
|
* @param mixed $url
|
||||||
|
* @param mixed $date
|
||||||
|
* @param mixed $guid
|
||||||
|
*
|
||||||
|
* @return array
|
||||||
|
*/
|
||||||
|
public function extract($url, $date)
|
||||||
|
{
|
||||||
|
$this->guid = str_replace('https://feshun.mv/', '', $url);
|
||||||
|
|
||||||
|
$crawler = $this->client->request('GET', $url);
|
||||||
|
|
||||||
|
$crawler->filter('h3')->each(function ($node) {
|
||||||
|
$this->title = $node->text();
|
||||||
|
});
|
||||||
|
|
||||||
|
|
||||||
|
$crawler->filter('.elementor-widget-container p')->each(function ($node) {
|
||||||
|
$this->content[] = preg_replace("/[a-zA-Z]/", "", $node->text());;
|
||||||
|
});
|
||||||
|
|
||||||
|
|
||||||
|
$crawler->filter('li[itemprop*="author"] span')->each(function ($node) {
|
||||||
|
$this->author = $node->text();
|
||||||
|
});
|
||||||
|
|
||||||
|
|
||||||
|
return [
|
||||||
|
'source' => 'Feshun',
|
||||||
|
'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,
|
||||||
|
'date' => Carbon::parse($date)->format("Y-m-d H:i:s"),
|
||||||
|
'url' => $url,
|
||||||
|
'author' => $this->author,
|
||||||
|
'guid' => $this->guid,
|
||||||
|
'topics' => [
|
||||||
|
[
|
||||||
|
"name" => "ވަކި މަޢުލޫއެއް ނޭންގެ",
|
||||||
|
"slug" => "no-specific-topic"
|
||||||
|
]
|
||||||
|
]
|
||||||
|
];
|
||||||
|
}
|
||||||
|
}
|
Reference in New Issue
Block a user