Zaviyani news intergration

This commit is contained in:
2021-01-11 13:46:06 +05:00
parent 31404d2f48
commit bb5602f911
5 changed files with 202 additions and 0 deletions

View File

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

View File

@@ -92,6 +92,10 @@ class Kernel extends ConsoleKernel
$schedule->command('scrape:funadhoo-times')->everyFiveMinutes()
->runInBackground()
->pingOnSuccess(env('APP_URL') . "/api/ping/funadhoo-times");
$schedule->command('scrape:zaviyani')->everyFiveMinutes()
->runInBackground()
->pingOnSuccess(env('APP_URL') . "/api/ping/zaviyani");
}
/**

View File

@@ -0,0 +1,39 @@
<?php
namespace App\Services\Feeds;
use Goutte\Client;
class ZaviyaniFeed implements Feed
{
protected $client;
public function __construct()
{
$this->client = new Client();
}
/**
* Get all the latest news
*
* @return array
*/
public function get() : array
{
$crawler = $this->client->request('GET', "https://zaviyani.mv/");
$feeds = [];
$crawler->filter('div[class*="jeg_vc_content"] article')->each(function ($node) use (&$feeds) {
$feeds[] = [
"title" => $node->filter('.jeg_post_title')->text(),
"link" => $node->filter('.jeg_post_title a')->attr('href'),
"date" => str_replace("-","",$node->filter('.jeg_meta_date a')->first()->text())
];
});
return array_slice($feeds,0,24);
}
}

View File

@@ -0,0 +1,55 @@
<?php
namespace App\Services\Scrapers;
use Goutte\Client;
use Illuminate\Support\Carbon;
class ZaviyaniScraper
{
protected $client;
protected $title;
protected $content;
protected $topics = [];
protected $author = "unknown";
public function __construct()
{
$this->client = new Client;
}
public function extract($url, $date)
{
$crawler = $this->client->request('GET', $url);
$crawler->filter('.content-inner p')->each(function ($node) {
$this->content[] = $node->text();
});
if ($crawler->filter('div[class*="jeg_meta_author"] a')->count() == 1) {
$this->author = $crawler->filter('div[class*="jeg_meta_author"] a')->first()->text();
}
//Remove all the alphabets from string
//preg_replace("/[a-zA-Z]/", "",$string);
return [
'source' => 'Zaviyani',
'title' => $crawler->filter('h1')->first()->text(),
'og_title' => $crawler->filter('meta[property*="og:title"]')->first()->attr('content'),
'image' => $crawler->filter(".featured_image img")->first()->attr('data-src'),
'content' => $this->content,
'url' => $url,
'date' => Carbon::parse($date)->format("Y-m-d H:i:s"),
'guid' => str_replace("https://zaviyani.mv/?p=", "", $url),
'author' => $this->author,
'topics' => [
[
"name" => "ވަކި މަޢުލޫއެއް ނޭންގެ",
"slug" => "no-specific-topic"
]
]
];
}
}

View File

@@ -0,0 +1,30 @@
<?php
namespace App\Services;
use App\Services\Scrapers\ZaviyaniScraper;
use App\Services\Feeds\ZaviyaniFeed;
use Illuminate\Support\Str;
class ZaviyaniService 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 = (new ZaviyaniFeed)->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 ZaviyaniScraper)->extract($article["link"], $article["date"]);
}
return $articlesitems;
}
}