Support for Avas mv
This commit is contained in:
72
app/Console/Commands/ScrapeAvasCommand.php
Normal file
72
app/Console/Commands/ScrapeAvasCommand.php
Normal file
@@ -0,0 +1,72 @@
|
||||
<?php
|
||||
|
||||
namespace App\Console\Commands;
|
||||
|
||||
use Illuminate\Console\Command;
|
||||
use App\Services\AvasService;
|
||||
use App\Topic;
|
||||
use App\Source;
|
||||
|
||||
class ScrapeAvasCommand extends Command
|
||||
{
|
||||
/**
|
||||
* The name and signature of the console command.
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected $signature = 'scrape:avas';
|
||||
|
||||
/**
|
||||
* The console command description.
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected $description = 'Scrape Avas';
|
||||
|
||||
/**
|
||||
* 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', 'avas')->first();
|
||||
|
||||
$articles = (new AvasService)->scrape();
|
||||
|
||||
foreach ($articles as $article) {
|
||||
|
||||
// Attach the relationship between source and article and return the curren article instance
|
||||
$articleModel = $source->articles()->firstOrCreate(["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);
|
||||
});
|
||||
|
||||
}
|
||||
}
|
||||
}
|
26
app/Services/AvasService.php
Normal file
26
app/Services/AvasService.php
Normal file
@@ -0,0 +1,26 @@
|
||||
<?php
|
||||
namespace App\Services;
|
||||
|
||||
use App\Services\Feeds\AvasFeed;
|
||||
use App\Services\Scrapers\AvasScraper;
|
||||
|
||||
class AvasService
|
||||
{
|
||||
/**
|
||||
* 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 AvasFeed)->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 AvasScraper)->extract($article["link"]);
|
||||
}
|
||||
|
||||
return $articlesitems;
|
||||
}
|
||||
}
|
37
app/Services/Feeds/AvasFeed.php
Normal file
37
app/Services/Feeds/AvasFeed.php
Normal file
@@ -0,0 +1,37 @@
|
||||
<?php
|
||||
namespace App\Services\Feeds;
|
||||
|
||||
use Goutte\Client;
|
||||
|
||||
class AvasFeed
|
||||
{
|
||||
protected $client;
|
||||
|
||||
public function __construct()
|
||||
{
|
||||
$this->client = new Client();
|
||||
}
|
||||
/**
|
||||
* Return the latest articles from avas
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function get() : array
|
||||
{
|
||||
|
||||
$crawler = $this->client->request('GET', "https://avas.mv/");
|
||||
|
||||
$feeds = [];
|
||||
|
||||
$crawler->filter('div[class*="flex rtl -mx-4 flex-wrap md:px-0"] div[class*="w-full md:w-1/3 px-4 mb-7"] div a')->each(function($node) use (&$feeds){
|
||||
$feeds[] = [
|
||||
"title" => trim($node->text()),
|
||||
"link" => "https://avas.mv".$node->attr('href')
|
||||
];
|
||||
});
|
||||
|
||||
|
||||
return array_slice($feeds,0,18);
|
||||
|
||||
}
|
||||
}
|
71
app/Services/Scrapers/AvasScraper.php
Normal file
71
app/Services/Scrapers/AvasScraper.php
Normal file
@@ -0,0 +1,71 @@
|
||||
<?php
|
||||
namespace App\Services\Scrapers;
|
||||
|
||||
use Goutte\Client;
|
||||
use Illuminate\Support\Carbon;
|
||||
|
||||
class AvasScraper
|
||||
{
|
||||
protected $client;
|
||||
|
||||
protected $content;
|
||||
protected $author;
|
||||
protected $topics = [];
|
||||
|
||||
public function __construct()
|
||||
{
|
||||
$this->client = new Client;
|
||||
}
|
||||
|
||||
public function extract($url)
|
||||
{
|
||||
|
||||
$crawler = $this->client->request('GET', $url);
|
||||
|
||||
$title = $crawler->filter('h1')->first()->text();
|
||||
|
||||
|
||||
|
||||
$image = $crawler->filter('figure img')->first()->attr('src');
|
||||
|
||||
$crawler->filter('.post_content p')->each(function ($node) {
|
||||
$this->content[] = preg_replace("/[a-zA-Z]/","",$node->text());
|
||||
});
|
||||
|
||||
$crawler->filter('div[class*="border-t border-grey-light border-dotted mt-7 py-3"] a')->each(function ($node) {
|
||||
|
||||
//Removing the show more tags button
|
||||
if($node->text() == "+")
|
||||
{
|
||||
return;
|
||||
}
|
||||
$this->topics[] = [
|
||||
"name" => $node->text(),
|
||||
"slug" => str_replace("/", "", $node->attr('href'))
|
||||
];
|
||||
});
|
||||
|
||||
|
||||
|
||||
if($crawler->filter('div[class*="font-waheed text-grey ml-3 pl-3 text-lg border-l border-grey border-dotted"] a')->count() == 1)
|
||||
{
|
||||
$this->author = $crawler->filter('div[class*="font-waheed text-grey ml-3 pl-3 text-lg border-l border-grey border-dotted"] a')->first()->text();
|
||||
}
|
||||
|
||||
|
||||
//Remove all the alphabets from string
|
||||
//preg_replace("/[a-zA-Z]/", "",$string);
|
||||
return [
|
||||
'source' => 'Avas',
|
||||
'title' => $title,
|
||||
'og_title' => $crawler->filter('meta[property*="og:title"]')->first()->attr('content'),
|
||||
'image' => $image,
|
||||
'content' => $this->content,
|
||||
'url' => $url,
|
||||
'date' => Carbon::parse($crawler->filter('timeago')->first()->attr('datetime'))->format("Y-m-d H:i:s"),
|
||||
'guid' => str_replace("https://avas.mv/","",$url),
|
||||
'author' => $this->author,
|
||||
'topics' => $this->topics
|
||||
];
|
||||
}
|
||||
}
|
@@ -0,0 +1,30 @@
|
||||
<?php
|
||||
|
||||
use Illuminate\Database\Migrations\Migration;
|
||||
use Illuminate\Database\Schema\Blueprint;
|
||||
use Illuminate\Support\Facades\Schema;
|
||||
|
||||
class ChangeArticlesTableColumn extends Migration
|
||||
{
|
||||
/**
|
||||
* Run the migrations.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function up()
|
||||
{
|
||||
Schema::table('articles', function (Blueprint $table) {
|
||||
$table->longText('body')->change();
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Reverse the migrations.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function down()
|
||||
{
|
||||
//
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user