FUnadhoo times support

This commit is contained in:
2021-01-01 16:09:26 +05:00
parent dc57d8c7c5
commit cd8db7499c
4 changed files with 164 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\FunadhooTimesService;
use App\Topic;
use Illuminate\Support\Carbon;
class ScrapeFunadhooTimesCommand extends Command
{
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'scrape:funadhoo-times';
/**
* The console command description.
*
* @var string
*/
protected $description = 'Scrape Funadhoo times';
/**
* 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', 'funadhoo-times')->first();
$articles = (new FunadhooTimesService)->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

@@ -0,0 +1,30 @@
<?php
namespace App\Services;
use App\Services\Scrapers\FunadhooTimesScraper;
use Illuminate\Support\Str;
class FunadhooTimesService 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://funadhootimes.com/rss.xml")["channel"]["article"];
$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'];
$title = $article['title'];
$articlesitems[] = (new FunadhooTimesScraper)->extract($link, $title);
}
return $articlesitems;
}
}

View File

@@ -0,0 +1,60 @@
<?php
namespace App\Services\Scrapers;
use Exception;
use Goutte\Client;
use Illuminate\Support\Carbon;
class FunadhooTimesScraper
{
protected $client;
protected $title;
protected $content;
protected $topics;
public function __construct()
{
$this->client = new Client;
}
public function extract($url,$title)
{
$crawler = $this->client->request('GET', $url);
$crawler->filter('.card-body > p')->each(function ($node) {
$this->content[] = $node->text();
});
$crawler->filter('.article-tags a')->each(function ($node) {
$this->topics[] = [
"name" => $node->text(),
"slug" => str_replace("https://hama.mv/", "", $node->attr('href'))
];
});
//Remove all the alphabets from string
//preg_replace("/[a-zA-Z]/", "",$string);
return [
'source' => 'Funadhoo times',
'title' => $title,
'og_title' => $crawler->filter('meta[property*="og:title"]')->first()->attr('content'),
'image' => "https://funadhootimes.com/".$crawler->filter(".card img")->first()->attr('src'),
'content' => $this->content,
'url' => $url,
'date' => Carbon::parse(explode('|',$crawler->filter('div[class*="d-flex flex-row-reverse justify-content-between"] small')->first()->text())[0])->format("Y-m-d H:i:s"),
'guid' => str_replace("https://www.funadhootimes.com/p.php?id=","",$url),
'author' => explode('|',$crawler->filter('div[class*="d-flex flex-row-reverse justify-content-between"] small')->first()->text())[1],
'topics' => [
[
"name" => "ވަކި މަޢުލޫއެއް ނޭންގެ",
"slug" => "no-specific-topic"
]
]
];
}
}

Binary file not shown.

After

Width:  |  Height:  |  Size: 52 KiB