Vnews support
This commit is contained in:
72
app/Console/Commands/ScrapeVnewsCommand.php
Normal file
72
app/Console/Commands/ScrapeVnewsCommand.php
Normal file
@@ -0,0 +1,72 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace App\Console\Commands;
|
||||||
|
|
||||||
|
use Illuminate\Console\Command;
|
||||||
|
use App\Services\VnewsService;
|
||||||
|
use App\Topic;
|
||||||
|
use App\Source;
|
||||||
|
|
||||||
|
class ScrapeVnewsCommand extends Command
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* The name and signature of the console command.
|
||||||
|
*
|
||||||
|
* @var string
|
||||||
|
*/
|
||||||
|
protected $signature = 'scrape:vnews';
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The console command description.
|
||||||
|
*
|
||||||
|
* @var string
|
||||||
|
*/
|
||||||
|
protected $description = 'Scrape Vnews';
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 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', 'vnews')->first();
|
||||||
|
|
||||||
|
$articles = (new VnewsService)->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);
|
||||||
|
});
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -63,6 +63,9 @@ class Kernel extends ConsoleKernel
|
|||||||
|
|
||||||
$schedule->command('scrape:miadhu')->everyFiveMinutes()
|
$schedule->command('scrape:miadhu')->everyFiveMinutes()
|
||||||
->pingOnSuccess(env('APP_URL') . "/api/ping/miadhu");
|
->pingOnSuccess(env('APP_URL') . "/api/ping/miadhu");
|
||||||
|
|
||||||
|
$schedule->command('scrape:vnews')->everyFiveMinutes()
|
||||||
|
->pingOnSuccess(env('APP_URL') . "/api/ping/vnews");
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|||||||
58
app/Services/Scrapers/VnewsScraper.php
Normal file
58
app/Services/Scrapers/VnewsScraper.php
Normal file
@@ -0,0 +1,58 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace App\Services\Scrapers;
|
||||||
|
|
||||||
|
use Goutte\Client;
|
||||||
|
use Illuminate\Support\Carbon;
|
||||||
|
|
||||||
|
class VnewsScraper
|
||||||
|
{
|
||||||
|
protected $client;
|
||||||
|
|
||||||
|
protected $title;
|
||||||
|
protected $content;
|
||||||
|
protected $author = "unknown";
|
||||||
|
protected $topics;
|
||||||
|
|
||||||
|
public function __construct()
|
||||||
|
{
|
||||||
|
$this->client = new Client;
|
||||||
|
}
|
||||||
|
|
||||||
|
public function extract($url, $date)
|
||||||
|
{
|
||||||
|
|
||||||
|
$crawler = $this->client->request('GET', $url);
|
||||||
|
|
||||||
|
$crawler->filter('.news-content > p')->each(function ($node) {
|
||||||
|
$this->content[] = $node->text();
|
||||||
|
});
|
||||||
|
|
||||||
|
if ($crawler->filter('.author')->count() > 0) {
|
||||||
|
$this->author = $crawler->filter('.author')->first()->text();
|
||||||
|
}
|
||||||
|
|
||||||
|
$crawler->filter('.news-tags a')->each(function ($node) {
|
||||||
|
$this->topics[] = [
|
||||||
|
"name" => $node->text(),
|
||||||
|
"slug" => str_replace("#", "", str_replace("https://www.vnews.mv/", "", $node->attr('href')))
|
||||||
|
];
|
||||||
|
});
|
||||||
|
|
||||||
|
|
||||||
|
//Remove all the alphabets from string
|
||||||
|
//preg_replace("/[a-zA-Z]/", "",$string);
|
||||||
|
return [
|
||||||
|
'source' => 'VNews',
|
||||||
|
'title' => $crawler->filter('h1')->first()->text(),
|
||||||
|
'og_title' => $crawler->filter('meta[property*="og:title"]')->first()->attr('content'),
|
||||||
|
'image' => $crawler->filter(".news-image img")->first()->attr('src'),
|
||||||
|
'content' => $this->content,
|
||||||
|
'url' => $url,
|
||||||
|
'date' => Carbon::parse($date)->format("Y-m-d H:i:s"),
|
||||||
|
'guid' => str_replace("https://www.vnews.mv/", "", $url),
|
||||||
|
'author' => $this->author,
|
||||||
|
'topics' => $this->topics
|
||||||
|
];
|
||||||
|
}
|
||||||
|
}
|
||||||
29
app/Services/VnewsService.php
Normal file
29
app/Services/VnewsService.php
Normal file
@@ -0,0 +1,29 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace App\Services;
|
||||||
|
|
||||||
|
use App\Services\Scrapers\VnewsScraper;
|
||||||
|
|
||||||
|
class VnewsService extends Client
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* Scrap all the rss articles from Press
|
||||||
|
*
|
||||||
|
* @return array
|
||||||
|
*/
|
||||||
|
public function scrape(): array
|
||||||
|
{
|
||||||
|
//Return only the rss that contains "news" keyboard in its url
|
||||||
|
$articles = $this->get("https://www.vnews.mv/rss")["channel"]["item"];
|
||||||
|
|
||||||
|
$articlesitems = [];
|
||||||
|
//Looping through the articles and scraping and while scraping it creates a new instance of the scraper.
|
||||||
|
foreach ($articles as $article) {
|
||||||
|
$link = $article['link'];
|
||||||
|
$date = $article['pubDate'];
|
||||||
|
$articlesitems[] = (new VnewsScraper)->extract($link, $date);
|
||||||
|
}
|
||||||
|
|
||||||
|
return $articlesitems;
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user