Completed
Push — master ( 45165d...5e44c4 )
by Alexey
04:08 queued 11s
created

PlayStoreUiRequest::getReviewsRequest()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 28
Code Lines 18

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 15
CRAP Score 2

Importance

Changes 0
Metric Value
eloc 18
c 0
b 0
f 0
dl 0
loc 28
ccs 15
cts 15
cp 1
rs 9.6666
cc 2
nc 2
nop 4
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 Nelexa\GPlay\Enum\SortEnum;
16
use Nelexa\GPlay\GPlayApps;
17
use Nelexa\GPlay\Model\AppId;
18
use Psr\Http\Message\RequestInterface;
19
use function GuzzleHttp\Psr7\stream_for;
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
    /**
37
     * @param AppId       $requestApp
38
     * @param int         $count
39
     * @param SortEnum    $sort
40
     * @param string|null $token
41
     *
42
     * @return RequestInterface
43
     */
44 1
    public static function getReviewsRequest(
45
        AppId $requestApp,
46
        int $count,
47
        SortEnum $sort,
48
        ?string $token = null
49
    ): RequestInterface {
50 1
        $limit = min(self::LIMIT_REVIEW_ON_PAGE, max(1, $count));
51
        $queryParams = [
52 1
            'rpcids' => self::RPC_ID_REVIEWS,
53 1
            GPlayApps::REQ_PARAM_LOCALE => $requestApp->getLocale(),
54 1
            GPlayApps::REQ_PARAM_COUNTRY => $requestApp->getCountry(),
55
            'authuser' => null,
56 1
            'soc-app' => 121,
57 1
            'soc-platform' => 1,
58 1
            'soc-device' => 1,
59
        ];
60 1
        $url = GPlayApps::GOOGLE_PLAY_URL . '/_/PlayStoreUi/data/batchexecute?' . http_build_query($queryParams);
61
        $formParams = [
62 1
            'f.req' => '[[["' . self::RPC_ID_REVIEWS . '","[null,null,[2,' . $sort->value(
63 1
                ) . ',[' . $limit . ',null,' . ($token === null ? 'null' : '\\"' . $token . '\\"') . ']],[\\"' . $requestApp->getId(
64 1
                ) . '\\",7]]",null,"generic"]]]',
65
        ];
66
        $headers = [
67 1
            'Content-Type' => 'application/x-www-form-urlencoded;charset=utf-8',
68
        ];
69 1
        $body = stream_for(http_build_query($formParams));
70
71 1
        return new Request('POST', $url, $headers, $body);
72
    }
73
74
    /**
75
     * @param string $locale
76
     * @param string $country
77
     * @param int    $count
78
     * @param string $token
79
     *
80
     * @return RequestInterface
81
     */
82 30
    public static function getAppsRequest(string $locale, string $country, int $count, string $token): RequestInterface
83
    {
84 30
        $limit = min(self::LIMIT_APPS_ON_PAGE, max(1, $count));
85
        $queryParams = [
86 30
            'rpcids' => self::RPC_ID_APPS,
87 30
            GPlayApps::REQ_PARAM_LOCALE => $locale,
88 30
            GPlayApps::REQ_PARAM_COUNTRY => $country,
89 30
            'soc-app' => 121,
90 30
            'soc-platform' => 1,
91 30
            'soc-device' => 1,
92
        ];
93 30
        $url = GPlayApps::GOOGLE_PLAY_URL . '/_/PlayStoreUi/data/batchexecute?' . http_build_query($queryParams);
94
        $formParams = [
95 30
            'f.req' => '[[["' . self::RPC_ID_APPS . '","[[null,[[10,[10,' . $limit . ']],true,null,[1]],null,\\"' . $token . '\\"]]",null,"generic"]]]',
96
        ];
97
        $headers = [
98 30
            'Content-Type' => 'application/x-www-form-urlencoded;charset=utf-8',
99
        ];
100 30
        $body = stream_for(http_build_query($formParams));
101
102 30
        return new Request('POST', $url, $headers, $body);
103
    }
104
105
    /**
106
     * @param AppId $requestApp
107
     *
108
     * @throws \Exception
109
     *
110
     * @return RequestInterface
111
     */
112 1
    public static function getPermissionsRequest(AppId $requestApp): RequestInterface
113
    {
114
        $queryParams = [
115 1
            'rpcids' => self::RPC_ID_PERMISSIONS,
116 1
            GPlayApps::REQ_PARAM_LOCALE => $requestApp->getLocale(),
117 1
            GPlayApps::REQ_PARAM_COUNTRY => $requestApp->getCountry(),
118 1
            'soc-app' => 121,
119 1
            'soc-platform' => 1,
120 1
            'soc-device' => 1,
121
        ];
122 1
        $url = GPlayApps::GOOGLE_PLAY_URL . '/_/PlayStoreUi/data/batchexecute?' . http_build_query($queryParams);
123
        $formParams = [
124 1
            'f.req' => '[[["' . self::RPC_ID_PERMISSIONS . '","[[null,[\"' .
125 1
                $requestApp->getId() . '\",7],[]]]",null,"1"]]]',
126
        ];
127
        $headers = [
128 1
            'Content-Type' => 'application/x-www-form-urlencoded;charset=utf-8',
129
        ];
130 1
        $body = stream_for(http_build_query($formParams));
131
132 1
        return new Request('POST', $url, $headers, $body);
133
    }
134
}
135