Фикс парсера Anivisual с unit-тестом для верности
На Anivisual обновили вёрстку и собираются что-то делать с сайтом.
This commit is contained in:
parent
b6199572e7
commit
00cb77957f
|
@ -61,9 +61,11 @@ class Anivisual extends Source {
|
||||||
$this->output .= $game->print();
|
$this->output .= $game->print();
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
public function checkPage($url) {
|
public function checkPage($url) {
|
||||||
return (strpos($url,'http://anivisual.net/stuff/') !== FALSE);
|
return (strpos($url,'://anivisual.net/stuff/') !== FALSE);
|
||||||
}
|
}
|
||||||
|
|
||||||
public function page($url) {
|
public function page($url) {
|
||||||
$text = $this->get_text($url);
|
$text = $this->get_text($url);
|
||||||
$this->loadStr($text);
|
$this->loadStr($text);
|
||||||
|
@ -71,7 +73,11 @@ class Anivisual extends Source {
|
||||||
$game = new Game;
|
$game = new Game;
|
||||||
$game->url = $url;
|
$game->url = $url;
|
||||||
$gameBlock = $this->dom->filter('#casing-box');
|
$gameBlock = $this->dom->filter('#casing-box');
|
||||||
$date = trim($gameBlock->filter('.icon-calendar')->first()->text());
|
$dateBlock = $this->dom->filter('.icon-calendar');
|
||||||
|
$date = '';
|
||||||
|
if ($dateBlock->count() > 0) {
|
||||||
|
$date = trim($dateBlock->first()->text());
|
||||||
|
}
|
||||||
if (!empty($date)) {
|
if (!empty($date)) {
|
||||||
foreach ($this->months as $ruM => $enM) {
|
foreach ($this->months as $ruM => $enM) {
|
||||||
$date = str_replace($ruM, $enM, $date);
|
$date = str_replace($ruM, $enM, $date);
|
||||||
|
@ -81,18 +87,20 @@ class Anivisual extends Source {
|
||||||
}
|
}
|
||||||
$title = $this->dom->filter('h1.logo')->first();
|
$title = $this->dom->filter('h1.logo')->first();
|
||||||
if ($title->count() > 0) {
|
if ($title->count() > 0) {
|
||||||
$game->title = htmlspecialchars_decode($title->text());
|
$game->title = trim(htmlspecialchars_decode($title->text()));
|
||||||
}
|
}
|
||||||
$game->description = $gameBlock->filter('#content > section > span')->first()->text();
|
$game->description = $this->dom->filter('#content > section > span')->first()->text();
|
||||||
$game->description = str_replace('(adsbygoogle = window.adsbygoogle || []).push({});', '', $game->description);
|
$game->description = str_replace('(adsbygoogle = window.adsbygoogle || []).push({});', '', $game->description);
|
||||||
|
$game->description = str_replace('Доп. ссылки: Доступно только для пользователей', '', $game->description);
|
||||||
$game->description = trim($game->description);
|
$game->description = trim($game->description);
|
||||||
|
|
||||||
$sidebar = $gameBlock->filter('#sidebar')->first()->html();
|
$sidebar = $gameBlock->filter('#sidebar')->first()->html();
|
||||||
preg_match ('/<b>Автор:<\/b>\s+(.*)(<\/a>)?<br>/', $sidebar, $matches);
|
$pos_start = mb_strpos($sidebar, '<b>Автор:');
|
||||||
if (count($matches) > 0) {
|
$sidebar_search = trim(mb_substr($sidebar, $pos_start));
|
||||||
$game->author = strip_tags($matches[1]);
|
$pos_end = mb_strpos($sidebar_search, '<br>');
|
||||||
preg_match('/(.*)Теги:/', $game->author, $matches);
|
$sidebar_search = trim(mb_substr($sidebar_search, 0, $pos_end));
|
||||||
$game->author = $matches[1];
|
$sidebar_search = str_replace('<b>Автор:</b>', '', $sidebar_search);
|
||||||
}
|
$game->author = trim(strip_tags($sidebar_search));
|
||||||
$matches = [];
|
$matches = [];
|
||||||
preg_match ('/<b>Перевод:<\/b>\s+(.*)<\/a><br>/', $sidebar, $matches);
|
preg_match ('/<b>Перевод:<\/b>\s+(.*)<\/a><br>/', $sidebar, $matches);
|
||||||
if (count($matches) > 0) {
|
if (count($matches) > 0) {
|
||||||
|
|
54
tests/AnivisualTest.php
Normal file
54
tests/AnivisualTest.php
Normal file
|
@ -0,0 +1,54 @@
|
||||||
|
<?php
|
||||||
|
/*
|
||||||
|
* A set of utilities for tracking text-based game releases
|
||||||
|
* Copyright (C) 2018 Alexander Yakovlev <keloero@oreolek.ru>
|
||||||
|
*
|
||||||
|
* This program is free software: you can redistribute it and/or modify
|
||||||
|
* it under the terms of the GNU General Public License as published by
|
||||||
|
* the Free Software Foundation, either version 3 of the License, or
|
||||||
|
* (at your option) any later version.
|
||||||
|
*
|
||||||
|
* This program is distributed in the hope that it will be useful,
|
||||||
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||||
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||||
|
* GNU General Public License for more details.
|
||||||
|
*
|
||||||
|
* You should have received a copy of the GNU General Public License
|
||||||
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||||
|
*/
|
||||||
|
|
||||||
|
require_once 'BaseTest.php';
|
||||||
|
require_once 'Source/Anivisual.php';
|
||||||
|
|
||||||
|
class ItchTest extends BaseTest
|
||||||
|
{
|
||||||
|
protected function setUp() {
|
||||||
|
$this->parser = new Source\Anivisual();
|
||||||
|
}
|
||||||
|
public function testPageCheck()
|
||||||
|
{
|
||||||
|
$this->assertTrue($this->parser->checkPage('https://anivisual.net/stuff/3-1-0-1889'));
|
||||||
|
$this->assertFalse($this->parser->checkPage('https://anivisual.net/'));
|
||||||
|
$this->assertFalse($this->parser->checkPage('http://anivisual.net'));
|
||||||
|
$this->assertFalse($this->parser->checkPage('http://itch.io'));
|
||||||
|
}
|
||||||
|
public function testPageParsing()
|
||||||
|
{
|
||||||
|
$url = 'https://anivisual.net/stuff/3-1-0-1889';
|
||||||
|
$game_page = NULL;
|
||||||
|
try {
|
||||||
|
$game_page = $this->parser->get_text($url);
|
||||||
|
} catch (\Exception $e) {
|
||||||
|
$game_page = file_get_contents('./tests/fixtures/anivisual_test_1.html');
|
||||||
|
}
|
||||||
|
if (is_null($game_page)) {
|
||||||
|
$game_page = file_get_contents('./tests/fixtures/anivisual_test_1.html');
|
||||||
|
}
|
||||||
|
$this->parser->loadStr($game_page, []);
|
||||||
|
$game = $this->parser->page($url);
|
||||||
|
$this->assertSame($game->url, $url);
|
||||||
|
$this->assertSame($game->title, 'Contract Demon');
|
||||||
|
$this->assertSame($game->author, 'NomnomNami, пер. Sol Taere');
|
||||||
|
$this->assertSame($game->description, 'Ангел, вместо своих ангельских дел, вызывает... демона. Да и демонесса какая-то странная, пытается выглядеть зловеще, но что-то совсем не получается. По крайней мере Ангел её не боится ни капли, а вроде даже наоборот... _______ От переводчика. Как я люблю эту короткометражную няшноту от NomnomNami, просто ужос. Хотел было и дальше переводить её поделки, но узнал, что уже всё переведено. А жаль. Работать с такими штуками - одно удовольствие. Лёгкий и приятный текст, и всего один-два дня работы. Прохождение этой ВН от Тернокса и Химари — р-р-раз, д-д-два');
|
||||||
|
}
|
||||||
|
}
|
1767
tests/fixtures/anivisual_test_1.html
vendored
Normal file
1767
tests/fixtures/anivisual_test_1.html
vendored
Normal file
File diff suppressed because it is too large
Load diff
Reference in a new issue