fix voice

This commit is contained in:
Mohamed jinas
2024-01-07 21:18:43 +05:00
parent 46ead16ef8
commit c8329962b7
4 changed files with 37 additions and 40 deletions

View File

@@ -25,8 +25,7 @@ class SourcesAPIController extends Controller
'zaviyani', 'zaviyani',
'funadhoo-times', 'funadhoo-times',
'psm', 'psm',
'raajjemv', 'raajjemv'
'voice'
])->get()); ])->get());
}); });
} }

View File

@@ -11,6 +11,7 @@ class VoiceFeed implements Feed
{ {
$this->client = new Client(); $this->client = new Client();
} }
/** /**
* Get all the latest news * Get all the latest news
* *
@@ -18,11 +19,14 @@ class VoiceFeed implements Feed
*/ */
public function get() : array public function get() : array
{ {
$crawler = $this->client->request('GET', "https://voice.mv/"); $crawler = $this->client->request('GET', "https://voice.mv/");
return $crawler->filter('div[id*="latest-news"] .content a') return $crawler->filter('div#latest-news > div > a')->each(function ($node) {
->extract(['_text', '_attr' => 'href']); return [
"title" => $node->filter('.dv-bold')->text(),
"link" => 'https://voice.mv' . $node->attr('href'),
"date" => $node->filter('.en-font')->text(),
];
});
} }
} }

View File

@@ -8,54 +8,46 @@ use Illuminate\Support\Carbon;
class VoiceScraper class VoiceScraper
{ {
protected $client; protected $client;
protected $author = 'unknown';
protected $author; protected $content = [];
protected $content;
protected $topics = []; protected $topics = [];
public function __construct() public function __construct()
{ {
$this->client = new Client; $this->client = new Client();
} }
public function extract($url) public function extract($url)
{ {
$crawler = $this->client->request('GET', $url); $crawler = $this->client->request('GET', $url);
$title = $crawler->filter('.content h1')->first()->text(); // Extracting title - checking for multiple class names
$title = $crawler->filter('.text-3xl')->first()->text();
// Extracting article content
$image = $crawler->filter('.image img')->first()->attr('src'); $crawler->filter('.container .dv')->each(function ($node) {
$this->content[] = $node->text();
$crawler->filter('article p')->each(function ($node) {
$this->content[] = preg_replace("/[a-zA-Z]/", "", $node->text());
}); });
// Extracting topics
$this->topics = [];
$crawler->filter('.related-tags-holder a')->each(function ($node) { $crawler->filter('.related-tags-holder a')->each(function ($node) {
$this->topics[] = [ $this->topics[] = [
"name" => $node->text(), "name" => $node->text(),
"slug" => str_replace("/", "", $node->attr('href')) "slug" => str_replace("/", "", $node->attr('href'))
]; ];
}); });
if ($crawler->filter(".authorname a")->count() == 1) { // Returning extracted data
$this->author = str_replace("- ", "", $crawler->filter('.authorname a')->first()->text());
}
//Remove all the alphabets from string
//preg_replace("/[a-zA-Z]/", "",$string);
return [ return [
'source' => 'Voice', 'source' => 'Voice',
'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'),
'image' => $image, 'image' => $crawler->filter('meta[property="og:image"]')->first()->attr('content'),
'content' => $this->content, 'content' => $this->content,
'url' => $url, 'url' => $url,
'date' => Carbon::parse(str_replace("- ", "", $crawler->filter('.authordatecomment .date')->first()->text()))->format("Y-m-d H:i:s"), 'date' => Carbon::now(),
'guid' => str_replace("https://voice.mv/", "", $url), 'guid' => str_replace("https://voice.mv/", "", $url),
'author' => $this->author, 'author' => $this->author,
'topics' => $this->topics 'topics' => $this->topics

View File

@@ -1,4 +1,5 @@
<?php <?php
namespace App\Services; namespace App\Services;
use App\Services\Feeds\VoiceFeed; use App\Services\Feeds\VoiceFeed;
@@ -18,7 +19,8 @@ class VoiceService
$articlesitems = []; $articlesitems = [];
//Looping through the articles and scraping and while scraping it creates a new instance of the scraper. //Looping through the articles and scraping and while scraping it creates a new instance of the scraper.
foreach ($articles as $article) { foreach ($articles as $article) {
$articlesitems[] = (new VoiceScraper)->extract("https://voice.mv".$article[1]);
$articlesitems[] = (new VoiceScraper)->extract($article["link"]);
} }
return $articlesitems; return $articlesitems;