The Press intergration

This commit is contained in:
2020-08-14 21:57:16 +05:00
parent 14858d5bfa
commit 2382583587
4 changed files with 165 additions and 0 deletions

View File

@@ -0,0 +1,72 @@
<?php
namespace App\Console\Commands;
use App\Source;
use Illuminate\Console\Command;
use App\Services\ThePressService;
use App\Topic;
class ScrapeThePressCommand extends Command
{
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'scrape:thepress';
/**
* The console command description.
*
* @var string
*/
protected $description = 'Scrape The press';
/**
* 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', 'thepress')->first();
$articles = (new ThePressService)->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);
});
}
}
}

View File

@@ -28,6 +28,7 @@ class Kernel extends ConsoleKernel
$schedule->command('scrape:mihaaru')->everyFiveMinutes(); $schedule->command('scrape:mihaaru')->everyFiveMinutes();
$schedule->command('scrape:sun')->everyFiveMinutes(); $schedule->command('scrape:sun')->everyFiveMinutes();
$schedule->command('scrape:thiladhun')->everyFiveMinutes(); $schedule->command('scrape:thiladhun')->everyFiveMinutes();
$schedule->command('scrape:thepress')->everyFiveMinutes();
} }
/** /**

View File

@@ -0,0 +1,62 @@
<?php
namespace App\Services\Scrapers;
use Goutte\Client;
class ThePressScraper
{
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('.article-image img')->first()->attr('src');
$crawler->filter('article p')->each(function ($node) {
$this->content[] = preg_replace("/[a-zA-Z]/","",$node->text());
});
$crawler->filter('.article-tags a')->each(function ($node) {
$this->topics[] = [
"name" => $node->text(),
"slug" => str_replace("https://thepress.mv/", "", $node->attr('href'))
];
});
if($crawler->filter(".author-details strong")->count() == 1)
{
$this->author = $crawler->filter('.author-details strong')->first()->text();
}
//Remove all the alphabets from string
//preg_replace("/[a-zA-Z]/", "",$string);
return [
'source' => 'The Press',
'title' => $title,
'og_title' => $crawler->filter('meta[property*="og:title"]')->first()->attr('content'),
'image' => $image,
'content' => $this->content,
'url' => $url,
'date' => $crawler->filter('.article-header .datetime')->first()->text(),
'guid' => str_replace("https://thepress.mv/","",$url),
'author' => $this->author,
'topics' => $this->topics
];
}
}

View File

@@ -0,0 +1,30 @@
<?php
namespace App\Services;
use App\Services\Scrapers\ThePressScraper;
use Illuminate\Support\Str;
class ThePressService 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://thepress.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'];
$articlesitems[] = (new ThePressScraper)->extract($link);
}
return $articlesitems;
}
}