This commit is contained in:
2021-01-04 15:06:13 +05:00
parent bf157c793d
commit 31404d2f48

View File

@@ -1,4 +1,5 @@
<?php <?php
namespace App\Services\Scrapers; namespace App\Services\Scrapers;
use Goutte\Client; use Goutte\Client;
@@ -10,7 +11,7 @@ class DhenScraper
protected $content; protected $content;
protected $author; protected $author;
public function __construct() public function __construct()
{ {
$this->client = new Client; $this->client = new Client;
@@ -23,25 +24,30 @@ class DhenScraper
$title = $crawler->filter('h1')->first()->text(); $title = $crawler->filter('h1')->first()->text();
if($crawler->filter('article .article-visual img')->count() == 1) if ($crawler->filter('article .article-visual img')->count() == 1) {
{
$image = $crawler->filter('article .article-visual img')->first()->attr('src'); $image = $crawler->filter('article .article-visual img')->first()->attr('src');
} }
$crawler->filter('article .article-entry p')->each(function ($node) { if ($crawler->filter('article .article-entry p')->count() > 0) {
$this->content[] = preg_replace("/[a-zA-Z]/","",$node->text()); $crawler->filter('article .article-entry p')->each(function ($node) {
}); $this->content[] = preg_replace("/[a-zA-Z]/", "", $node->text());
});
} else {
$crawler->filter('.im_message_body .im_message_text')->each(function ($node) {
$this->content[] = preg_replace("/[a-zA-Z]/", "", $node->text());
});
}
if($crawler->filter(".story-item--meta a")->count() == 1)
{
if ($crawler->filter(".story-item--meta a")->count() == 1) {
$this->author = $crawler->filter('.story-item--meta a')->first()->text(); $this->author = $crawler->filter('.story-item--meta a')->first()->text();
} }
//Remove all the alphabets from string //Remove all the alphabets from string
//preg_replace("/[a-zA-Z]/", "",$string); //preg_replace("/[a-zA-Z]/", "",$string);
return [ return [
'source' => 'Dhen', 'source' => 'Dhen',
'title' => $title, 'title' => $title,
'og_title' => $crawler->filter('meta[property*="og:title"]')->first()->attr('content'), 'og_title' => $crawler->filter('meta[property*="og:title"]')->first()->attr('content'),
@@ -49,7 +55,7 @@ class DhenScraper
'content' => $this->content, 'content' => $this->content,
'url' => $url, 'url' => $url,
'date' => Carbon::parse(str_replace("- ", "", $crawler->filter('.story-item--meta time')->first()->text()))->format("Y-m-d H:i:s"), '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), 'guid' => str_replace("https://dhen.mv/", "", $url),
'author' => $this->author, 'author' => $this->author,
'topics' => [ 'topics' => [
[ [
@@ -59,4 +65,4 @@ class DhenScraper
] ]
]; ];
} }
} }