Dhen news intergrated

This commit is contained in:
2020-08-20 03:48:56 +05:00
parent f2f2773acb
commit b3e154c1f7
3 changed files with 161 additions and 0 deletions

View File

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

@@ -0,0 +1,28 @@
<?php
namespace App\Services;
use App\Services\Scrapers\DhenScraper;
class DhenService 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://dhen.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) {
$link = $article['link'];
$articlesitems[] = (new DhenScraper)->extract($link);
}
return $articlesitems;
}
}

View File

@@ -0,0 +1,61 @@
<?php
namespace App\Services\Scrapers;
use Goutte\Client;
use Illuminate\Support\Carbon;
class DhenScraper
{
protected $client;
protected $content;
protected $author;
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 .article-visual img')->first()->attr('src');
$crawler->filter('article .article-entry p')->each(function ($node) {
$this->content[] = preg_replace("/[a-zA-Z]/","",$node->text());
});
if($crawler->filter(".story-item--meta a")->count() == 1)
{
$this->author = $crawler->filter('.story-item--meta a')->first()->text();
}
//Remove all the alphabets from string
//preg_replace("/[a-zA-Z]/", "",$string);
return [
'source' => 'Dhen',
'title' => $title,
'og_title' => $crawler->filter('meta[property*="og:title"]')->first()->attr('content'),
'image' => $image,
'content' => $this->content,
'url' => $url,
'date' => Carbon::parse(str_replace("- ", "", $crawler->filter('.story-item--meta time')->first()->text()))->format("Y-m-d H:i:s"),
'guid' => str_replace("https://dhen.mv/","",$url),
'author' => $this->author,
'topics' => [
[
"name" => "ވަކި މަޢުލޫއެއް ނޭންގެ",
"slug" => "uncategorized"
]
]
];
}
}