From d931fd568bcc4f9f32f4e0a23841364ac386d55f Mon Sep 17 00:00:00 2001 From: Alexander Yakovlev Date: Thu, 29 Mar 2018 14:42:44 +0700 Subject: [PATCH] =?UTF-8?q?=D0=9F=D0=B0=D1=80=D1=81=D0=B5=D1=80=20QSP?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Source.php | 20 +++++++++----------- Source/Anivisual.php | 3 --- Source/Apero.php | 2 -- Source/Hyperbook.php | 2 -- Source/Instead.php | 1 - Source/Qsp.php | 24 ++++++++++++++++++++++++ Source/Steam.php | 15 ++++++--------- Source/Urq.php | 3 --- Wikipage.php | 14 ++++++++++---- wiki.php | 4 +++- 10 files changed, 52 insertions(+), 36 deletions(-) diff --git a/Source.php b/Source.php index 0184438..77adef4 100644 --- a/Source.php +++ b/Source.php @@ -3,6 +3,7 @@ namespace Source; use \Symfony\Component\DomCrawler\Crawler; use \Game; +use \GuzzleHttp\Client as GuzzleClient; abstract class Source { public $title; @@ -52,17 +53,14 @@ abstract class Source { * * @return string */ - protected function get_text($url) { - $curl = curl_init(); - curl_setopt_array($curl, array( - CURLOPT_RETURNTRANSFER => 1, - CURLOPT_URL => $url, - CURLOPT_CONNECTTIMEOUT => 30, - CURLOPT_COOKIE => $this->cookies, - )); - $resp = curl_exec($curl); - curl_close($curl); - return $resp; + public function get_text($url) { + $client = new GuzzleClient([ + 'timeout' => 30, + ]); + $response = $client->request('GET', $url, [ + 'cookies' => $this->cookies, + ]); + return (string) $response->getBody(); } /** diff --git a/Source/Anivisual.php b/Source/Anivisual.php index 5d5f2fd..f972294 100644 --- a/Source/Anivisual.php +++ b/Source/Anivisual.php @@ -20,9 +20,6 @@ class Anivisual extends Source { 'Декабря' => 'December', ]; protected function parse() { - $text = $this->get_text('http://anivisual.net/stuff/1'); - $this->loadStr($text); - unset($text); $this->dom->filter('.entryBlock')->each(function($gameBlock) { $date = trim($gameBlock->filter('.icon-calendar')->text()); foreach ($this->months as $ruM => $enM) { diff --git a/Source/Apero.php b/Source/Apero.php index b1e2561..dbc25e5 100644 --- a/Source/Apero.php +++ b/Source/Apero.php @@ -38,8 +38,6 @@ class Apero extends Source { return (strpos($url,'http://apero.ru/') !== FALSE); } public function page($url) { - $text = $this->get_text($url); - $this->loadStr($text); $game = new Game; $game->url = $url; $game->platform = 'Аперо'; diff --git a/Source/Hyperbook.php b/Source/Hyperbook.php index d12cb76..b5f9ad1 100644 --- a/Source/Hyperbook.php +++ b/Source/Hyperbook.php @@ -52,8 +52,6 @@ class Hyperbook extends Source { return (strpos($url,$this->rootUrl.'/comments.php') !== FALSE); } public function page($url) { - $text = $this->get_text($url); - $this->loadStr($text); $game = new Game; $game->url = $url; $game->platform = 'AXMA Story Maker'; diff --git a/Source/Instead.php b/Source/Instead.php index f388cec..99211ab 100644 --- a/Source/Instead.php +++ b/Source/Instead.php @@ -34,7 +34,6 @@ class Instead extends Source { return (strpos($url,'http://instead-games.ru/game.php') !== FALSE); } public function page($url) { - echo 'Распознана игра INSTEAD.'.PHP_EOL; $text = $this->get_text($url); $this->loadStr($text); unset($text); diff --git a/Source/Qsp.php b/Source/Qsp.php index 828f44d..34ef341 100644 --- a/Source/Qsp.php +++ b/Source/Qsp.php @@ -23,4 +23,28 @@ class Qsp extends Source { $this->output .= $game->print(); }); } + public function checkPage($url) { + return (strpos($url,'http://qsp.su') !== FALSE); + } + public function page($url) { + $game = new Game; + $game->url = $url; + try { + $game->author = trim($this->dom->filter('#sobi2Details_field_author')->text()); + $game->author = trim(str_replace($this->dom->filter('#sobi2Listing_field_author_label')->text(), '', $game->author)); + $game->title = trim($this->dom->filter('.sobi2Details h1')->first()->text()); + $game->description = trim($this->dom->filter('#sobi2Details_field_description')->text()); + $game->platform = 'QSP'; + $game->url_download = trim($this->dom->filter('h2 a')->attr('href')); + $game->url_download_description = 'Архив для интерпретатора QSP'; + $game->image = trim($this->dom->filter('.sobi2DetailsImage')->first()->attr('src')); + preg_match('/\d?\d\.\d?\d\.\d{4}/', $this->dom->filter('.sobi2DetailsFooter tr:first-child td')->text(), $matches); + $game->date = new \DateTime($matches[0]); + } catch (\Exception $e) { + echo 'Ошибка парсинга. Проверьте URL.'.PHP_EOL; + echo $e->getTraceAsString(); + die(); + } + return $game; + } } diff --git a/Source/Steam.php b/Source/Steam.php index ad91c8e..fd1bc61 100644 --- a/Source/Steam.php +++ b/Source/Steam.php @@ -48,28 +48,25 @@ class Steam extends Source { } public function page($url) { $this->cookies = 'mature_content=1; Steam_Language=russian'; - $text = $this->get_text($url); - $game_page = new Crawler($text); - unset($text); $game = new Game; $game->url = $url; - $game->title = trim($game_page->filter('div.apphub_AppName')->first()->text()); - $game->description = trim($game_page->filter('div.game_description_snippet')->first()->text()); - $game->author = trim($game_page->filter('div#developers_list')->first()->text()); + $game->title = trim($this->dom->filter('div.apphub_AppName')->first()->text()); + $game->description = trim($this->dom->filter('div.game_description_snippet')->first()->text()); + $game->author = trim($this->dom->filter('div#developers_list')->first()->text()); if (strpos($game->author, ',') !== FALSE) { $game->author = explode(',', $game->author); $game->author = array_map('trim', $game->author); } - $game->image = $game_page->filter('img.game_header_image_full')->first()->attr('src'); + $game->image = $this->dom->filter('img.game_header_image_full')->first()->attr('src'); $game->categories = 'Коммерческая ИЛ'; - $languages = $game_page->filter('.game_language_options tr td:first-child'); + $languages = $this->dom->filter('.game_language_options tr td:first-child'); $game->language = []; foreach ($languages as $language) { $game->language[] = trim($language->nodeValue); } $game->language = implode(', ', $game->language); - $date = $game_page->filter('div.date')->first()->text(); + $date = $this->dom->filter('div.date')->first()->text(); $game->date = \DateTime::createFromFormat('d M, Y', $date); if ($game->date === FALSE) { // если Steam отдал страницу на русском foreach ($this->months as $ruM => $enM) { diff --git a/Source/Urq.php b/Source/Urq.php index 1f575b4..53bf760 100644 --- a/Source/Urq.php +++ b/Source/Urq.php @@ -22,9 +22,6 @@ class Urq extends Source { return (strpos($url,'http://urq.plut.info/node/') !== FALSE); } public function page($url) { - $text = $this->get_text($url); - $this->loadStr($text, []); - unset($text); $game = new Game; $game->url = $url; $gameBlock = $this->dom->filter('.content'); diff --git a/Wikipage.php b/Wikipage.php index d50cde6..8ded6f8 100644 --- a/Wikipage.php +++ b/Wikipage.php @@ -110,21 +110,27 @@ class Wikipage { $this->content .= "\n== Версии =="; } $this->txtadd('url_online', "\n* [#{@game.url_online} #{@game.url_online_description}]"); - if (!empty($this->game->url_download) && !empty($this->game->url_download_description)) { - $this->content .= "\n* [$this->game->url_download $this->game->url_download_description]"; + if (!empty($this->game->url_download)) { + if (!empty($this->game->url_download_description)) { + $this->content .= PHP_EOL.'* ['.$this->game->url_download.' '.$this->game->url_download_description.']'; + } else { + $this->content .= "\n* [#{$this->game->url_download} Скачать игру]"; + } } if (!empty($this->game->url_discussion) || !empty($this->game->url)) { $this->content .= "\n== Ссылки =="; } $this->txtadd('url_discussion', '* ['.$this->game->url_discussion.' Обсуждение игры]'); $this->txtadd('url', '* ['.$this->game->url.' Страница игры]'); - if (is_array($this->game->categories)) { + if (!empty($this->game->categories) && is_array($this->game->categories)) { $this->content .= PHP_EOL; foreach ($this->game->categories as $category) { $this->content .= '[[Категория:'.$category.']]'; } } else { - $this->content .= PHP_EOL.'[[Категория:'.$this->game->categories.']]'; + if (!empty($this->game->categories)) { + $this->content .= PHP_EOL.'[[Категория:'.$this->game->categories.']]'; + } } } diff --git a/wiki.php b/wiki.php index 60888d4..95acb6b 100755 --- a/wiki.php +++ b/wiki.php @@ -13,7 +13,7 @@ if (!isset($argv[1])) { echo 'Please provide the direct URL to the game page.'.PHP_EOL; die(); } -$url = strtolower($argv[1]); +$url = $argv[1]; $game = new Game; function check($classname) { @@ -23,6 +23,8 @@ function check($classname) { $cname = 'Source\\'.$classname; $cl = (new $cname()); if ($cl->checkPage($url)) { + echo 'Используется правило "'.$cl->title.'".'.PHP_EOL; + $cl->loadStr($cl->get_text($url)); $game = $cl->page($url); if ($game) { $page = new Wikipage($game);