|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
/* |
|
6
|
|
|
* Copyright (c) Ne-Lexa |
|
7
|
|
|
* |
|
8
|
|
|
* For the full copyright and license information, please view |
|
9
|
|
|
* the LICENSE file that was distributed with this source code. |
|
10
|
|
|
* |
|
11
|
|
|
* @see https://github.com/Ne-Lexa/google-play-scraper |
|
12
|
|
|
*/ |
|
13
|
|
|
|
|
14
|
|
|
namespace Nelexa\GPlay\Scraper; |
|
15
|
|
|
|
|
16
|
|
|
use Nelexa\GPlay\GPlayApps; |
|
17
|
|
|
use Nelexa\GPlay\HttpClient\ParseHandlerInterface; |
|
18
|
|
|
use Nelexa\GPlay\Model\AppId; |
|
19
|
|
|
use Nelexa\GPlay\Util\ScraperUtil; |
|
20
|
|
|
use Psr\Http\Message\RequestInterface; |
|
21
|
|
|
use Psr\Http\Message\ResponseInterface; |
|
22
|
|
|
|
|
23
|
|
|
/** |
|
24
|
|
|
* @internal |
|
25
|
|
|
*/ |
|
26
|
|
|
class FindSimilarAppsUrlScraper implements ParseHandlerInterface |
|
27
|
|
|
{ |
|
28
|
|
|
/** @var AppId */ |
|
29
|
|
|
private $appId; |
|
30
|
|
|
|
|
31
|
|
|
/** |
|
32
|
|
|
* SimilarScraper constructor. |
|
33
|
|
|
* |
|
34
|
|
|
* @param AppId $appId |
|
35
|
|
|
*/ |
|
36
|
2 |
|
public function __construct(AppId $appId) |
|
37
|
|
|
{ |
|
38
|
2 |
|
$this->appId = $appId; |
|
39
|
|
|
} |
|
40
|
|
|
|
|
41
|
|
|
/** |
|
42
|
|
|
* @param RequestInterface $request |
|
43
|
|
|
* @param ResponseInterface $response |
|
44
|
|
|
* @param array $options |
|
45
|
|
|
* |
|
46
|
|
|
* @return string|null |
|
47
|
|
|
*/ |
|
48
|
2 |
|
public function __invoke(RequestInterface $request, ResponseInterface $response, array &$options = []): ?string |
|
49
|
|
|
{ |
|
50
|
2 |
|
$scriptData = ScraperUtil::extractScriptData($response->getBody()->getContents()); |
|
51
|
2 |
|
$sidebarBlocksApps = ScraperUtil::getValue($scriptData, 'ds:6.1.1'); |
|
52
|
2 |
|
$url = null; |
|
53
|
|
|
|
|
54
|
2 |
|
foreach ($sidebarBlocksApps as $blockApps) { |
|
55
|
2 |
|
$url = ScraperUtil::getValue($blockApps, '21.1.2.4.2'); |
|
56
|
2 |
|
if ($url !== null && strpos($url, 'cluster') !== false) { |
|
57
|
2 |
|
break; |
|
58
|
|
|
} |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
2 |
|
if ($url !== null) { |
|
62
|
2 |
|
return GPlayApps::GOOGLE_PLAY_URL . $url |
|
63
|
2 |
|
. '&' . GPlayApps::REQ_PARAM_LOCALE . '=' . urlencode($this->appId->getLocale()) |
|
64
|
2 |
|
. '&' . GPlayApps::REQ_PARAM_COUNTRY . '=' . urlencode($this->appId->getCountry()); |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
|
|
return null; |
|
68
|
|
|
} |
|
69
|
|
|
} |
|
70
|
|
|
|