| 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 |  |  |      * @return RequestInterface | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 | 1 |  |     public static function getReviewsRequest( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 |  |  |         AppId $requestApp, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 |  |  |         int $count, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 |  |  |         SortEnum $sort, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 |  |  |         ?string $token = null | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 |  |  |     ): RequestInterface { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 | 1 |  |         $limit = min(self::LIMIT_REVIEW_ON_PAGE, max(1, $count)); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 |  |  |         $queryParams = [ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 | 1 |  |             'rpcids' => self::RPC_ID_REVIEWS, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 | 1 |  |             GPlayApps::REQ_PARAM_LOCALE => $requestApp->getLocale(), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 | 1 |  |             GPlayApps::REQ_PARAM_COUNTRY => $requestApp->getCountry(), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 |  |  |             'authuser' => null, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 | 1 |  |             'soc-app' => 121, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 | 1 |  |             'soc-platform' => 1, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 | 1 |  |             'soc-device' => 1, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 |  |  |         ]; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 | 1 |  |         $url = GPlayApps::GOOGLE_PLAY_URL . '/_/PlayStoreUi/data/batchexecute?' . http_build_query($queryParams); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 |  |  |         $formParams = [ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 | 1 |  |             'f.req' => '[[["' . self::RPC_ID_REVIEWS . '","[null,null,[2,' . $sort->value( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 | 1 |  |                 ) . ',[' . $limit . ',null,' . ($token === null ? 'null' : '\\"' . $token . '\\"') . | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 | 1 |  |                 ']],[\\"' . $requestApp->getId() . '\\",7]]",null,"generic"]]]', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 |  |  |         ]; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 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 | 1 |  |         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 |  |  |      * @return RequestInterface | 
            
                                                                                                            
                            
            
                                    
            
            
                | 83 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 84 | 17 |  |     public static function getAppsRequest(string $locale, string $country, int $count, string $token): RequestInterface | 
            
                                                                                                            
                            
            
                                    
            
            
                | 85 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 86 | 17 |  |         $limit = min(self::LIMIT_APPS_ON_PAGE, max(1, $count)); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 87 |  |  |         $queryParams = [ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 88 | 17 |  |             'rpcids' => self::RPC_ID_APPS, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 89 | 17 |  |             GPlayApps::REQ_PARAM_LOCALE => $locale, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 90 | 17 |  |             GPlayApps::REQ_PARAM_COUNTRY => $country, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 91 | 17 |  |             'soc-app' => 121, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 92 | 17 |  |             'soc-platform' => 1, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 93 | 17 |  |             'soc-device' => 1, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 94 |  |  |         ]; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 95 | 17 |  |         $url = GPlayApps::GOOGLE_PLAY_URL . '/_/PlayStoreUi/data/batchexecute?' . http_build_query($queryParams); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 96 |  |  |         $formParams = [ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 97 | 17 |  |             'f.req' => '[[["' . self::RPC_ID_APPS . '","[[null,[[10,[10,' . $limit . ']],true,null,[1]],null,\\"' . $token . '\\"]]",null,"generic"]]]', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 98 |  |  |         ]; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 99 |  |  |         $headers = [ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 100 | 17 |  |             'Content-Type' => 'application/x-www-form-urlencoded;charset=utf-8', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 101 |  |  |         ]; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 102 | 17 |  |         $body = Utils::streamFor(http_build_query($formParams)); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 103 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 104 | 17 |  |         return new Request('POST', $url, $headers, $body); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 105 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 106 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 107 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 108 |  |  |      * @param AppId $requestApp | 
            
                                                                                                            
                            
            
                                    
            
            
                | 109 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 110 |  |  |      * @throws \Exception | 
            
                                                                                                            
                            
            
                                    
            
            
                | 111 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 112 |  |  |      * @return RequestInterface | 
            
                                                                                                            
                            
            
                                    
            
            
                | 113 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 114 | 1 |  |     public static function getPermissionsRequest(AppId $requestApp): RequestInterface | 
            
                                                                                                            
                            
            
                                    
            
            
                | 115 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 116 |  |  |         $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 | 1 |  |             'soc-platform' => 1, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 122 | 1 |  |             'soc-device' => 1, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 123 |  |  |         ]; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 124 | 1 |  |         $url = GPlayApps::GOOGLE_PLAY_URL . '/_/PlayStoreUi/data/batchexecute?' . http_build_query($queryParams); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 125 |  |  |         $formParams = [ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 126 | 1 |  |             'f.req' => '[[["' . self::RPC_ID_PERMISSIONS . '","[[null,[\"' . | 
            
                                                                                                            
                            
            
                                    
            
            
                | 127 | 1 |  |                 $requestApp->getId() . '\",7],[]]]",null,"1"]]]', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 128 |  |  |         ]; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 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 | 1 |  |         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 | 2 |  |     public static function getSuggestRequest( | 
            
                                                                        
                            
            
                                    
            
            
                | 147 |  |  |         string $query, | 
            
                                                                        
                            
            
                                    
            
            
                | 148 |  |  |         string $locale = GPlayApps::DEFAULT_LOCALE, | 
            
                                                                        
                            
            
                                    
            
            
                | 149 |  |  |         string $country = GPlayApps::DEFAULT_COUNTRY | 
            
                                                                        
                            
            
                                    
            
            
                | 150 |  |  |     ): RequestInterface { | 
            
                                                                        
                            
            
                                    
            
            
                | 151 |  |  |         $queryParams = [ | 
            
                                                                        
                            
            
                                    
            
            
                | 152 | 2 |  |             'rpcids' => self::RPC_ID_SUGGEST, | 
            
                                                                        
                            
            
                                    
            
            
                | 153 | 2 |  |             GPlayApps::REQ_PARAM_LOCALE => $locale, | 
            
                                                                        
                            
            
                                    
            
            
                | 154 | 2 |  |             GPlayApps::REQ_PARAM_COUNTRY => $country, | 
            
                                                                        
                            
            
                                    
            
            
                | 155 | 2 |  |             'source-path' => '/store/apps', | 
            
                                                                        
                            
            
                                    
            
            
                | 156 |  |  |             'authuser' => null, | 
            
                                                                        
                            
            
                                    
            
            
                | 157 | 2 |  |             'soc-app' => 121, | 
            
                                                                        
                            
            
                                    
            
            
                | 158 | 2 |  |             'soc-platform' => 1, | 
            
                                                                        
                            
            
                                    
            
            
                | 159 | 2 |  |             'soc-device' => 1, | 
            
                                                                        
                            
            
                                    
            
            
                | 160 |  |  |         ]; | 
            
                                                                        
                            
            
                                    
            
            
                | 161 | 2 |  |         $url = GPlayApps::GOOGLE_PLAY_URL . '/_/PlayStoreUi/data/batchexecute?' . http_build_query($queryParams); | 
            
                                                                        
                            
            
                                    
            
            
                | 162 |  |  |         $formParams = [ | 
            
                                                                        
                            
            
                                    
            
            
                | 163 | 2 |  |             'f.req' => '[[["' . self::RPC_ID_SUGGEST . '","[[null,[\"' . | 
            
                                                                        
                            
            
                                    
            
            
                | 164 | 2 |  |                 str_replace('"', '\\\\\\"', $query) . | 
            
                                                                        
                            
            
                                    
            
            
                | 165 | 2 |  |                 '\"],[10],[2],4]]",null,"generic"]]]', | 
            
                                                                        
                            
            
                                    
            
            
                | 166 |  |  |         ]; | 
            
                                                                        
                            
            
                                    
            
            
                | 167 |  |  |         $headers = [ | 
            
                                                                        
                            
            
                                    
            
            
                | 168 | 2 |  |             'Content-Type' => 'application/x-www-form-urlencoded;charset=utf-8', | 
            
                                                                        
                            
            
                                    
            
            
                | 169 |  |  |         ]; | 
            
                                                                        
                            
            
                                    
            
            
                | 170 | 2 |  |         $body = Utils::streamFor(http_build_query($formParams)); | 
            
                                                                        
                            
            
                                    
            
            
                | 171 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 172 | 2 |  |         return new Request('POST', $url, $headers, $body); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 173 |  |  |     } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 174 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 175 |  |  |  |