Skip to content

Commit

Permalink
~ hide my ip
Browse files Browse the repository at this point in the history
  • Loading branch information
vantoozz committed Jul 1, 2017
1 parent 6bca090 commit b19f37a
Show file tree
Hide file tree
Showing 10 changed files with 348 additions and 388 deletions.
3 changes: 1 addition & 2 deletions src/HttpClient/HttpClientInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,7 @@ interface HttpClientInterface
{
/**
* @param string $uri
* @param array $headers
* @return string
*/
public function get(string $uri, array $headers): string;
public function get(string $uri): string;
}
5 changes: 2 additions & 3 deletions src/HttpClient/HttplugHttpClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,13 +38,12 @@ public function __construct(Client $httpClient, MessageFactory $messageFactory)

/**
* @param string $uri
* @param array $headers
* @return string
* @throws HttpClientException
*/
public function get(string $uri, array $headers): string
public function get(string $uri): string
{
$request = $this->messageFactory->createRequest(Http::GET, $uri, $headers);
$request = $this->messageFactory->createRequest(Http::GET, $uri);
try {
return $this->httpClient->sendRequest($request)->getBody()->getContents();
} catch (ClientException $e) {
Expand Down
2 changes: 1 addition & 1 deletion src/Scrapers/AbstractFreeProxyListScraper.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ public function __construct(HttpClientInterface $httpClient)
public function get(): \Generator
{
try {
$html = $this->httpClient->get($this->baseUrl(), []);
$html = $this->httpClient->get($this->baseUrl());
} catch (HttpClientException $e) {
throw new ScraperException($e->getMessage(), $e->getCode(), $e);
}
Expand Down
30 changes: 20 additions & 10 deletions src/Scrapers/HideMyIpScraper.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

namespace Vantoozz\ProxyScraper\Scrapers;

use Faker\Generator as Faker;
use Faker\Provider\UserAgent;
use Vantoozz\ProxyScraper\Exceptions\HttpClientException;
use Vantoozz\ProxyScraper\Exceptions\InvalidArgumentException;
use Vantoozz\ProxyScraper\Exceptions\ScraperException;
Expand All @@ -24,7 +26,7 @@ final class HideMyIpScraper implements ScraperInterface
/**
*
*/
private const URL = 'https://www.hide-my-ip.com/proxylist.shtml';
private const URL = 'https://www.hide-my-ip.com/%s/proxylist.shtml';

/**
* FreeProxyListScraper constructor.
Expand All @@ -42,14 +44,7 @@ public function __construct(HttpClientInterface $httpClient)
public function get(): \Generator
{
try {
$html = $this->httpClient->get(
static::URL,
[
'User-Agent' =>
'Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) ' .
'Chrome/41.0.2228.0 Safari/537.36',
]
);
$html = $this->httpClient->get($this->makeUrl());
} catch (HttpClientException $e) {
throw new ScraperException($e->getMessage(), $e->getCode(), $e);
}
Expand All @@ -66,6 +61,21 @@ public function get(): \Generator
}
}

/**
* @return string
*/
private function makeUrl(): string
{
$languages = [
'es', 'fr', 'it', 'pt', 'nl', 'de', 'se',
'dk', 'pl', 'tr', 'ar', 'ru', 'ro',
'cn', 'kr', 'jp', 'vn', 'th', 'sr',

];

return sprintf(static::URL, $languages[array_rand($languages)]);
}

/**
* @param string $html
* @return array
Expand All @@ -78,7 +88,7 @@ private function extractData(string $html): array
if ($expectedPartsCount !== count($parts)) {
throw new ScraperException('Unknown markup');
}
$json = trim(explode(';<!-- proxylist -->', $parts[1])[0]);
$json = trim(explode(";\n\n", $parts[1])[0]);
$data = json_decode($json, true);
if (!$data) {
throw new ScraperException('Cannot parse json: ' . json_last_error_msg());
Expand Down
2 changes: 1 addition & 1 deletion src/Scrapers/ProxyDbScraper.php
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ public function get(): \Generator
private function getPage(int $offset, int $pageSize): \Generator
{
try {
$html = $this->httpClient->get(sprintf(static::PAGE_URL, $pageSize, $offset), []);
$html = $this->httpClient->get(sprintf(static::PAGE_URL, $pageSize, $offset));
} catch (HttpClientException $e) {
throw new ScraperException($e->getMessage(), $e->getCode(), $e);
}
Expand Down
2 changes: 1 addition & 1 deletion src/Scrapers/RemoteTextScraper.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ public function __construct(HttpClientInterface $httpClient)
public function get(): \Generator
{
try {
$text = $this->httpClient->get($this->remoteTextUrl(), []);
$text = $this->httpClient->get($this->remoteTextUrl());
} catch (HttpClientException $e) {
throw new ScraperException($e->getMessage(), $e->getCode(), $e);
}
Expand Down
677 changes: 312 additions & 365 deletions tests/fixtures/hideMyIp.html

Large diffs are not rendered by default.

7 changes: 6 additions & 1 deletion tests/systemTests.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,13 @@
] as $class) {
$miner->addScraper($container->get($class));
}

$cacheFilename = __DIR__ . '/.cached_proxies';
if (in_array('--refresh', $argv, true)) {
unlink($cacheFilename);
}
if (in_array('--cached', $argv, true)) {
$miner = new Cached($miner, __DIR__ . '/.cached_proxies');
$miner = new Cached($miner, $cacheFilename);
}

$pipeline = new ReportsPipeline;
Expand Down
6 changes: 3 additions & 3 deletions tests/unit/HttpClient/HttplugHttpClientTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ public function it_throws_an_exception_if_an_error_happens(): void
->willThrowException(new \Exception('error message'));

$httpClient = new HttplugHttpClient($client, $messageFactory);
$httpClient->get('some url', []);
$httpClient->get('some url');
}

/**
Expand Down Expand Up @@ -67,7 +67,7 @@ public function it_throws_an_exception_if_processing_the_request_is_impossible()
});

$httpClient = new HttplugHttpClient($client, $messageFactory);
$httpClient->get('some url', []);
$httpClient->get('some url');
}

/**
Expand Down Expand Up @@ -110,6 +110,6 @@ public function it_returns_a_string(): void

$httpClient = new HttplugHttpClient($client, $messageFactory);

$this->assertEquals('some string', $httpClient->get('some url', []));
$this->assertEquals('some string', $httpClient->get('some url'));
}
}
2 changes: 1 addition & 1 deletion tests/unit/Scrapers/HideMyIpScraperTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ public function it_returns_a_proxy(): void
$proxy = $scraper->get()->current();

$this->assertInstanceOf(Proxy::class, $proxy);
$this->assertSame('104.41.154.213:8118', (string)$proxy);
$this->assertSame('218.161.1.189:3128', (string)$proxy);
}

/**
Expand Down

0 comments on commit b19f37a

Please sign in to comment.