Skip to content

Commit

Permalink
Merge pull request #3 from Ahmard/scraper/new
Browse files Browse the repository at this point in the history
  • Loading branch information
Ahmard authored Jan 20, 2021
2 parents bf47533 + 2d1c3ce commit c0f96ba
Show file tree
Hide file tree
Showing 9 changed files with 179 additions and 30 deletions.
28 changes: 24 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,12 @@ composer require ahmard/uticlass
```

# Usage

### Videos
Extract movies/tv shows download links
```php
use Uticlass\Others\ZippyShare;
use Uticlass\Video\CoolMoviez;
use Uticlass\Video\FZMovies;
use Uticlass\Lyrics\Genius;
use Uticlass\Video\NetNaija;

require 'vendor/autoload.php';
Expand All @@ -41,20 +42,33 @@ $dlLink2 = NetNaija::init($nnUrl)->get()->linkTwo();
//Mycoolmoviez
$mcUrl = 'https://www.coolmoviez.shop/movie/4715/Megafault_(2009)_english_movie.html';
$dlLink3 = CoolMoviez::init($mcUrl)->get();
```

### Others
Extract links from file storage and lyric sites
```php
use Uticlass\Lyrics\Genius;
use Uticlass\Others\ZippyShare;
use Uticlass\Others\FireFiles;

//Genius lyrics
$lyrics = Genius::init('https://genius.com/Taylor-swift-the-last-great-american-dynasty-lyrics')->get();

//ZippyShare
$zippyUrl = 'https://www118.zippyshare.com/v/5pwuoWgg/file.html';
$fileUrl = ZippyShare::init($zippyUrl)->get();

//FireFiles
$webpageUrl = 'https://firefiles.org/5m6rzmnb7v54'; //The Mandalorian S01E06
$fileLink = FireFiles::init($webpageUrl)->get();
```

## Searching
### Searching
Search movies sites
```php

use Uticlass\Video\Search\FZMoviesSearch;
use Uticlass\Video\Search\FEMKVComSearch;
use Uticlass\Video\Search\NetNaijaSearch;

require 'vendor/autoload.php';

Expand All @@ -67,6 +81,12 @@ $searchResults = FZMoviesSearch::create()
$searchResults = FEMKVComSearch::create()
->search('love')
->get();

//NetNaija.com
$searchResults = NetNaijaSearch::create()
->search('love')
->category(NetNaijaSearch::CAT_VIDEOS)
->get(3);
```
## [Examples](examples)

Expand Down
41 changes: 21 additions & 20 deletions composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

14 changes: 14 additions & 0 deletions examples/netnaijasearch.php
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);
2 changes: 2 additions & 0 deletions phpstan.neon
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ parameters:
checkMissingIterableValueType: false
ignoreErrors:
- '#Unsafe usage of new static\(\)#'
- '#Call to an undefined method Guzwrap\\Core\\GuzzleWrapper::execute\(\)#'
- '#Call to an undefined method Uticlass\\Core\\Searcher::get\(\)#'
paths:
- examples
- src
12 changes: 7 additions & 5 deletions src/Core/Scraper.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,17 +18,19 @@ class Scraper
*/
private Config $config;

public function __construct(string $url)
public function __construct(?string $url = null)
{
$this->url = $url;
if (!empty($url)){
$this->url = $url;
}
}

/**
* Instantiate this class
* @param string $url
* @return static
*/
public static function init(string $url = ''): self
public static function init(string $url): Scraper
{
return new static($url);
}
Expand All @@ -37,9 +39,9 @@ public static function init(string $url = ''): self
* Instantiate this class
* @return static
*/
public static function create()
public static function create(): Scraper
{
return new static('');
return new static();
}

/**
Expand Down
42 changes: 42 additions & 0 deletions src/Core/Searcher.php
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);
}
}
2 changes: 1 addition & 1 deletion src/Others/FireFiles.php
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ public function get(): string
return $this->secondPage($inputs);
}

private function secondPage(array $inputs)
private function secondPage(array $inputs): string
{
$secondInputs = [];
Client::post(function (Post $post) use ($inputs) {
Expand Down
6 changes: 6 additions & 0 deletions src/Video/Search/MobileTVShowsSearch.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

use Nette\Utils\Strings;
use Queliwrap\Client;
use Throwable;
use Uticlass\Core\Struct\Traits\InstanceCreator;

class MobileTVShowsSearch
Expand Down Expand Up @@ -43,6 +44,11 @@ public function searchBy(string $searchBy): MobileTVShowsSearch
return $this;
}

/**
* @param int $pageNumber
* @return array
* @throws Throwable
*/
public function get(int $pageNumber = 1): array
{
$urlTemplate = $this->urlTemplate;
Expand Down
62 changes: 62 additions & 0 deletions src/Video/Search/NetNaijaSearch.php
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,
];
}
}

0 comments on commit c0f96ba

Please sign in to comment.