Passed
Pull Request — master (#13)
by Alexey
08:47
created

PlayStoreUiRequest::getSuggestRequest()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 27
Code Lines 18

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
eloc 18
c 0
b 0
f 0
dl 0
loc 27
ccs 0
cts 0
cp 0
rs 9.6666
cc 1
nc 1
nop 3
crap 2
1
<?php
2
3
declare(strict_types=1);
4
5
/**
6
 * @author   Ne-Lexa
7
 * @license  MIT
8
 *
9
 * @see      https://github.com/Ne-Lexa/google-play-scraper
10
 */
11
12
namespace Nelexa\GPlay\Scraper;
13
14
use GuzzleHttp\Psr7\Request;
15
use GuzzleHttp\Psr7\Utils;
16
use Nelexa\GPlay\Enum\SortEnum;
17
use Nelexa\GPlay\GPlayApps;
18
use Nelexa\GPlay\Model\AppId;
19
use Psr\Http\Message\RequestInterface;
20
21
/**
22
 * @internal
23
 */
24
class PlayStoreUiRequest
25
{
26
    public const LIMIT_REVIEW_ON_PAGE = 199;
27
28
    public const LIMIT_APPS_ON_PAGE = 100;
29
30
    private const RPC_ID_REVIEWS = 'UsvDTd';
31
32
    private const RPC_ID_APPS = 'qnKhOb';
33
34
    private const RPC_ID_PERMISSIONS = 'xdSrCf';
35
36
    private const RPC_ID_SUGGEST = 'IJ4APc';
37
38
    /**
39
     * @param AppId       $requestApp
40
     * @param int         $count
41
     * @param SortEnum    $sort
42
     * @param string|null $token
43
     *
44 1
     * @return RequestInterface
45
     */
46
    public static function getReviewsRequest(
47
        AppId $requestApp,
48
        int $count,
49
        SortEnum $sort,
50 1
        ?string $token = null
51
    ): RequestInterface {
52 1
        $limit = min(self::LIMIT_REVIEW_ON_PAGE, max(1, $count));
53 1
        $queryParams = [
54 1
            'rpcids' => self::RPC_ID_REVIEWS,
55
            GPlayApps::REQ_PARAM_LOCALE => $requestApp->getLocale(),
56 1
            GPlayApps::REQ_PARAM_COUNTRY => $requestApp->getCountry(),
57 1
            'authuser' => null,
58 1
            'soc-app' => 121,
59
            'soc-platform' => 1,
60 1
            'soc-device' => 1,
61
        ];
62 1
        $url = GPlayApps::GOOGLE_PLAY_URL . '/_/PlayStoreUi/data/batchexecute?' . http_build_query($queryParams);
63 1
        $formParams = [
64 1
            'f.req' => '[[["' . self::RPC_ID_REVIEWS . '","[null,null,[2,' . $sort->value(
65
                ) . ',[' . $limit . ',null,' . ($token === null ? 'null' : '\\"' . $token . '\\"') .
66
                ']],[\\"' . $requestApp->getId() . '\\",7]]",null,"generic"]]]',
67 1
        ];
68
        $headers = [
69 1
            'Content-Type' => 'application/x-www-form-urlencoded;charset=utf-8',
70
        ];
71 1
        $body = Utils::streamFor(http_build_query($formParams));
72
73
        return new Request('POST', $url, $headers, $body);
74
    }
75
76
    /**
77
     * @param string $locale
78
     * @param string $country
79
     * @param int    $count
80
     * @param string $token
81
     *
82 16
     * @return RequestInterface
83
     */
84 16
    public static function getAppsRequest(string $locale, string $country, int $count, string $token): RequestInterface
85
    {
86 16
        $limit = min(self::LIMIT_APPS_ON_PAGE, max(1, $count));
87 16
        $queryParams = [
88 16
            'rpcids' => self::RPC_ID_APPS,
89 16
            GPlayApps::REQ_PARAM_LOCALE => $locale,
90 16
            GPlayApps::REQ_PARAM_COUNTRY => $country,
91 16
            'soc-app' => 121,
92
            'soc-platform' => 1,
93 16
            'soc-device' => 1,
94
        ];
95 16
        $url = GPlayApps::GOOGLE_PLAY_URL . '/_/PlayStoreUi/data/batchexecute?' . http_build_query($queryParams);
96
        $formParams = [
97
            'f.req' => '[[["' . self::RPC_ID_APPS . '","[[null,[[10,[10,' . $limit . ']],true,null,[1]],null,\\"' . $token . '\\"]]",null,"generic"]]]',
98 16
        ];
99
        $headers = [
100 16
            'Content-Type' => 'application/x-www-form-urlencoded;charset=utf-8',
101
        ];
102 16
        $body = Utils::streamFor(http_build_query($formParams));
103
104
        return new Request('POST', $url, $headers, $body);
105
    }
106
107
    /**
108
     * @param AppId $requestApp
109
     *
110
     * @throws \Exception
111
     *
112 1
     * @return RequestInterface
113
     */
114
    public static function getPermissionsRequest(AppId $requestApp): RequestInterface
115 1
    {
116 1
        $queryParams = [
117 1
            'rpcids' => self::RPC_ID_PERMISSIONS,
118 1
            GPlayApps::REQ_PARAM_LOCALE => $requestApp->getLocale(),
119 1
            GPlayApps::REQ_PARAM_COUNTRY => $requestApp->getCountry(),
120 1
            'soc-app' => 121,
121
            'soc-platform' => 1,
122 1
            'soc-device' => 1,
123
        ];
124 1
        $url = GPlayApps::GOOGLE_PLAY_URL . '/_/PlayStoreUi/data/batchexecute?' . http_build_query($queryParams);
125 1
        $formParams = [
126
            'f.req' => '[[["' . self::RPC_ID_PERMISSIONS . '","[[null,[\"' .
127
                $requestApp->getId() . '\",7],[]]]",null,"1"]]]',
128 1
        ];
129
        $headers = [
130 1
            'Content-Type' => 'application/x-www-form-urlencoded;charset=utf-8',
131
        ];
132 1
        $body = Utils::streamFor(http_build_query($formParams));
133
134
        return new Request('POST', $url, $headers, $body);
135
    }
136
137
    /**
138
     * @param string $query
139
     * @param string $locale
140
     * @param string $country
141
     *
142
     * @throws \Exception
143
     *
144
     * @return RequestInterface
145
     */
146
    public static function getSuggestRequest(
147
        string $query,
148
        string $locale = GPlayApps::DEFAULT_LOCALE,
149
        string $country = GPlayApps::DEFAULT_COUNTRY
150
    ): RequestInterface {
151
        $queryParams = [
152
            'rpcids' => self::RPC_ID_SUGGEST,
153
            GPlayApps::REQ_PARAM_LOCALE => $locale,
154
            GPlayApps::REQ_PARAM_COUNTRY => $country,
155
            'source-path' => '/store/apps',
156
            'authuser' => null,
157
            'soc-app' => 121,
158
            'soc-platform' => 1,
159
            'soc-device' => 1,
160
        ];
161
        $url = GPlayApps::GOOGLE_PLAY_URL . '/_/PlayStoreUi/data/batchexecute?' . http_build_query($queryParams);
162
        $formParams = [
163
            'f.req' => '[[["' . self::RPC_ID_SUGGEST . '","[[null,[\"' .
164
                str_replace('"', '\\\\\\"', $query) .
165
                '\"],[10],[2],4]]",null,"generic"]]]',
166
        ];
167
        $headers = [
168
            'Content-Type' => 'application/x-www-form-urlencoded;charset=utf-8',
169
        ];
170
        $body = Utils::streamFor(http_build_query($formParams));
171
172
        return new Request('POST', $url, $headers, $body);
173
    }
174
}
175