-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #3 from Ahmard/scraper/new
https://www.thenetnaija.com/ searcher added
- Loading branch information
Showing
9 changed files
with
179 additions
and
30 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
<?php | ||
|
||
use Uticlass\Video\Search\NetNaijaSearch; | ||
|
||
require dirname(__DIR__, 1) . '/vendor/autoload.php'; | ||
|
||
$url = 'https://www.thenetnaija.com/search?t=star+wars'; | ||
|
||
$results = NetNaijaSearch::create() | ||
->search('love') | ||
->category(NetNaijaSearch::CAT_VIDEOS) | ||
->get(3); | ||
|
||
var_dump($results); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
<?php | ||
|
||
|
||
namespace Uticlass\Core; | ||
|
||
|
||
class Searcher extends Scraper | ||
{ | ||
protected string $query; | ||
protected array $paramValues = []; | ||
protected array $params = []; | ||
|
||
public function search(string $query): Searcher | ||
{ | ||
$this->paramValues['{query}'] = $query; | ||
return $this; | ||
} | ||
|
||
public function category(string $categoryName): Searcher | ||
{ | ||
$this->paramValues['{category}'] = $categoryName; | ||
return $this; | ||
} | ||
|
||
protected function hasParam(string $param): bool | ||
{ | ||
return array_key_exists("{$param}", $this->paramValues); | ||
} | ||
|
||
protected function getConstructedUrl(int $pageNumber = 1): string | ||
{ | ||
$builtUrl = urldecode(http_build_query($this->params)); | ||
|
||
$url = $this->url . '?' . str_replace( | ||
array_values($this->params), | ||
array_values($this->paramValues), | ||
$builtUrl | ||
); | ||
|
||
return str_replace('{pageNumber}', (string)$pageNumber, $url); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
<?php | ||
|
||
|
||
namespace Uticlass\Video\Search; | ||
|
||
|
||
use QL\Dom\Elements; | ||
use Queliwrap\Client; | ||
use Throwable; | ||
use Uticlass\Core\Searcher; | ||
|
||
class NetNaijaSearch extends Searcher | ||
{ | ||
public const CAT_EVERYWHERE = ''; | ||
public const CAT_FORUM = 'forum'; | ||
public const CAT_MUSIC = 'music'; | ||
public const CAT_VIDEOS = 'videos'; | ||
|
||
protected string $url = 'https://www.thenetnaija.com/search/page/{pageNumber}'; | ||
protected array $params = [ | ||
't' => '{query}', | ||
'folder' => '{category}', | ||
]; | ||
|
||
/** | ||
* @param int $pageNumber | ||
* @return array | ||
* @throws Throwable | ||
*/ | ||
public function get(int $pageNumber = 1): array | ||
{ | ||
$searchResults = []; | ||
$queryList = Client::get($this->getConstructedUrl($pageNumber))->execute(); | ||
|
||
$queryList->find('article[class="result"]') | ||
->each(function (Elements $element) use (&$searchResults) { | ||
$infoElement = $element->find('div.result-info h3'); | ||
$image = $element->find('div.result-img img')->attr('src'); | ||
$title = $infoElement->text(); | ||
$link = $infoElement->find('a')->attr('href'); | ||
$desc = $element->find('p.result-desc')->text(); | ||
$searchResults[] = [ | ||
'title' => $title, | ||
'href' => $link, | ||
'image' => $image, | ||
'desc' => $desc | ||
]; | ||
}); | ||
|
||
//Pagination | ||
$pagingText = $queryList->find('div.pages div')->eq(1)->text(); | ||
preg_match("@Viewing Page ([0-9]+) of ([0-9]+) pages@", $pagingText, $matches); | ||
|
||
return [ | ||
'meta' => [ | ||
'page_number' => $pageNumber, | ||
'total_pages' => (int)$matches[2], | ||
], | ||
'searchResults' => $searchResults, | ||
]; | ||
} | ||
} |