Skip to content

Commit

Permalink
Merge pull request #2 from Ahmard/scraper/new
Browse files Browse the repository at this point in the history
  • Loading branch information
Ahmard authored Jan 18, 2021
2 parents 04f8688 + 26d7d88 commit 9b46ebc
Show file tree
Hide file tree
Showing 4 changed files with 122 additions and 18 deletions.
12 changes: 6 additions & 6 deletions composer.lock

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

9 changes: 9 additions & 0 deletions examples/firefiles.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?php

use Uticlass\Others\FireFiles;

require '../vendor/autoload.php';

$webpageUrl = 'https://firefiles.org/5m6rzmnb7v54'; //The Mandalorian S01E06

$fileLink = FireFiles::init($webpageUrl)->get();
25 changes: 13 additions & 12 deletions src/Importer.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,18 @@

namespace Uticlass;

use Guzwrap\Request;
use Guzwrap\RequestInterface;
use Queliwrap\Client;
use Uticlass\Core\Scraper;

/**
* Class Importer
* @package Uticlass
* @method static Importer import(string $url) Import remote file
*/
class Importer
class Importer extends Scraper
{
protected string $url;

public static function __callStatic(string $method, array $args): Importer
{
if ('import' == $method){
Expand All @@ -23,16 +24,16 @@ public static function __callStatic(string $method, array $args): Importer
throw new \Exception("Method {$className}::{$method}() does not exists.");
}

public function __construct(string $url)
{
$this->url = $url;
}

public function save(string $file): bool
public function save(string $file): void
{
Client::get($this->url)->sink($file)->execute();

return true;
if (isset($this->request)){
var_dump(Request::useRequest($this->request)
->get($this->url)
->sink($file)
->getRequestData());
}else{
Request::get($this->url)->sink($file)->execute();
}
}

}
94 changes: 94 additions & 0 deletions src/Others/FireFiles.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
<?php


namespace Uticlass\Others;


use Guzwrap\Core\Post;
use Guzwrap\Core\Redirect;
use Guzwrap\Request;
use GuzzleHttp\Cookie\CookieJar;
use Psr\Http\Message\ResponseInterface;
use QL\Dom\Elements;
use Queliwrap\Client;
use Uticlass\Core\Scraper;

/**
* Class FireFiles
* @package Uticlass\Others
* @link https://firefiles.org/
*/
class FireFiles extends Scraper
{
private ?string $firstRedirectedUrl = null;

private string $domain = 'https://firefiles.org';

public function __construct(string $url)
{
parent::__construct($url);

$this->useRequest(
Request::withCookie(new CookieJar())
->redirects(function (Redirect $redirect) {
$redirect->max(10);
})
);
}

public function get(): string
{
$inputs = [];
Client::get($this->url)
->useRequest($this->request)
->onHeaders(function (ResponseInterface $response) {
if (!$this->firstRedirectedUrl) {
$this->firstRedirectedUrl = $this->domain . $response->getHeaderLine('Location');
}
})
->execute()
->find('#container > form input')
->each(function (Elements $input) use (&$inputs) {
$inputs[$input->attr('name')] = $input->attr('value');
});

return $this->secondPage($inputs);
}

private function secondPage(array $inputs)
{
$secondInputs = [];
Client::post(function (Post $post) use ($inputs) {
$post->url($this->firstRedirectedUrl);
foreach ($inputs as $name => $value) {
$post->field($name, $value);
}
})
->useRequest($this->request)
->execute()
->find('form input')
->each(function (Elements $input) use (&$secondInputs) {
$secondInputs[$input->attr('name')] = $input->attr('value');
});

return $this->thirdPage($secondInputs);
}

private function thirdPage(array $inputs): string
{
$buttonOnClickAttr = Client::post(function (Post $post) use ($inputs) {
$post->url($this->firstRedirectedUrl);
foreach ($inputs as $name => $value) {
$post->field($name, $value);
}
})
->useRequest($this->request)
->execute()
->find('button[id="downloadbtn"]')
->attr('onclick');

preg_match("@window\.open\('(.*)','_self'\)@", $buttonOnClickAttr, $matches);

return $matches[1];
}
}

0 comments on commit 9b46ebc

Please sign in to comment.