Addulive intergration

This commit is contained in:
2020-08-15 01:26:46 +05:00
parent 4fba54c83d
commit 2b81bbd5fa
3 changed files with 164 additions and 0 deletions

View File

@@ -0,0 +1,73 @@
<?php
namespace App\Console\Commands;
use App\Source;
use Illuminate\Console\Command;
use App\Services\AdduLiveService;
use App\Topic;
use Illuminate\Support\Carbon;
class ScrapeAdduLiveCommand extends Command
{
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'scrape:addulive';
/**
* The console command description.
*
* @var string
*/
protected $description = 'Scrape Addulive';
/**
* 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', 'addulive')->first();
$articles = (new AdduLiveService)->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" => Carbon::parse($article["date"])->format("Y-m-d H:i:s"),
"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,29 @@
<?php
namespace App\Services;
use App\Services\Scrapers\AdduLiveScraper;
use Illuminate\Support\Str;
class AdduLiveService 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.addulive.com/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 AdduLiveScraper)->extract($link);
}
return $articlesitems;
}
}

View File

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