| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  | /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  |  * This file is part of GitterBot package. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  |  * @author Serafim <[email protected]> | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  |  * @date   18.04.2016 16:29 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  |  * For the full copyright and license information, please view the LICENSE | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  |  * file that was distributed with this source code. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  |  */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  | namespace Interfaces\Google; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  | use Amp\Artax\Client; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  | use Amp\Artax\Request; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  | use Amp\Artax\Response; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  | use Illuminate\Config\Repository; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  | use function Amp\wait; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  | /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  |  * Class GoogleSearch | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  |  * @package Domains\Bot\Middlewares\Common\GoogleSearch | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  |  */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  | class GoogleSearch | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  | { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  |      * @var Client | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  |     private $artax; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  |      * @var string | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  |     private $token; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 |  |  |      * GoogleSearch constructor. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 |  |  |      * @param Repository $config | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 |  |  |     public function __construct(Repository $config) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 |  |  |         $this->artax = new Client(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 |  |  |         $this->token = $config->get('google.token'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 |  |  |      * @param string $query | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 |  |  |      * @return array | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 |  |  |      * @throws \Throwable | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 |  |  |     public function search(string $query) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 |  |  |         if (empty($this->token)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 |  |  |             return []; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 |  |  |         $url = 'https://www.googleapis.com/customsearch/v1?'.http_build_query([ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 |  |  |             'key' => $this->token, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 |  |  |             'lr' => 'lang_ru', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 |  |  |             'cx' => '017648015832347857471:ap7pijkcqh4', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 |  |  |             'q' => $query, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 |  |  |         ]); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 |  |  |         $request = new Request(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 |  |  |         $request->setMethod('GET'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 |  |  |         $request->setUri($url); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 |  |  |         $request->setProtocol('1.1'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 |  |  |         /** @var Response $response */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 |  |  |         $response = wait($this->artax->request($request)); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 |  |  |         $result = json_decode($response->getBody()); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 75 |  |  |         if (json_last_error() !== JSON_ERROR_NONE) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 |  |  |             throw new \RuntimeException('Broken google api response'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 77 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 78 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 79 |  |  |         return property_exists($result, 'items') ? $result->items : []; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 80 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 81 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 82 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 83 |  |  |      * @param string $query | 
            
                                                                                                            
                            
            
                                    
            
            
                | 84 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 85 |  |  |      * @throws \Throwable | 
            
                                                                                                            
                            
            
                                    
            
            
                | 86 |  |  |      * @return string | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 87 |  |  |      */ | 
            
                                                        
            
                                    
            
            
                | 88 |  |  |     public function searchGetMessage(string $query) : string | 
            
                                                        
            
                                    
            
            
                | 89 |  |  |     { | 
            
                                                        
            
                                    
            
            
                | 90 |  |  |         $result = [trans('google.results')]; | 
            
                                                        
            
                                    
            
            
                | 91 |  |  |         $response = $this->search($query); | 
            
                                                        
            
                                    
            
            
                | 92 |  |  |  | 
            
                                                        
            
                                    
            
            
                | 93 |  |  |         foreach ($response as $i => $link) { | 
            
                                                        
            
                                    
            
            
                | 94 |  |  |             if ($i >= 3) { | 
            
                                                        
            
                                    
            
            
                | 95 |  |  |                 continue; | 
            
                                                        
            
                                    
            
            
                | 96 |  |  |             } | 
            
                                                        
            
                                    
            
            
                | 97 |  |  |  | 
            
                                                        
            
                                    
            
            
                | 98 |  |  |             $result[] = sprintf('[*] [i][url=%s]%s[/url][/i]', $link->link, $link->title); | 
            
                                                        
            
                                    
            
            
                | 99 |  |  |         } | 
            
                                                        
            
                                    
            
            
                | 100 |  |  |  | 
            
                                                        
            
                                    
            
            
                | 101 |  |  |         if (!empty($result)) { | 
            
                                                        
            
                                    
            
            
                | 102 |  |  |             $result = '[list]'.implode(PHP_EOL, $result).'[/list]'; | 
            
                                                        
            
                                    
            
            
                | 103 |  |  |         } | 
            
                                                        
            
                                    
            
            
                | 104 |  |  |  | 
            
                                                        
            
                                    
            
            
                | 105 |  |  |         return $result; | 
            
                                                        
            
                                    
            
            
                | 106 |  |  |     } | 
            
                                                        
            
                                    
            
            
                | 107 |  |  | } |