Archived
1
0
Fork 0

Questbook

This commit is contained in:
Alexander Yakovlev 2020-04-04 18:54:28 +07:00
parent 2a4483c1db
commit 8679dab6be
Signed by: oreolek
GPG key ID: 1CDC4B7820C93BD3

View file

@ -53,24 +53,32 @@ class Questbook extends Source {
},
];
try {
$games = $service->parse($string)[0]['value'];
$parsed = $service->parse($string);
if (is_array($parsed) && isset($parsed[0])) {
$games = $parsed[0]['value'] ?? [];
}
} catch (\Sabre\Xml\LibXMLException $e) {
echo $e->getMessage();
echo $e->getTraceAsString();
return "";
}
unset($string);
if (empty($games)) {
Log::error('Игр не найдено.');
return;
}
foreach ($games as $gameBlock) {
$game = new Game();
$game->title = trim($gameBlock['title'] ?? '');
$game->release_date = strtotime($gameBlock['pubDate']);
$game->release_date = new \DateTime($gameBlock['pubDate']);
$game->url = trim($gameBlock['link'] ?? '');
$game->url = str_replace('http://', 'https://', $game->url);
$source_id = NULL;
if (strpos('online/game', $game->url) !== false) {
if (strpos($game->url, 'online/game') !== false) {
$source_id = (int) str_replace('https://quest-book.ru/online/game', '', $game->url);
$game->url_play_online = $game->url;
}
if (strpos('/directory/', $game->url) !== false) {
if (strpos($game->url, '/directory/') !== false) {
$url = str_replace('https://quest-book.ru/directory/', '', $game->url);
$url = explode('/', $url);
$url = array_filter($url);
@ -80,11 +88,22 @@ class Questbook extends Source {
$game->source_id = $source_id;
}
$game->description = trim($gameBlock['description'] ?? '');
$game->author = trim($gameBlock['author'] ?? '');
$game = $this->findGame($game);
if (!$game->isClean()) {
$game->save();
$author_name = trim($gameBlock['author'] ?? '');
$author_model = Author::findByName($author_name);
if (empty($author_model)) {
$author_model = new Author();
$author_model->name = $author_name;
$author_model->save();
}
if (!$game->authors()->where('name', $author_name)->exists()) {
$game->authors()->attach($author_model);
}
$this->page($game->url);
}
}
@ -95,8 +114,13 @@ class Questbook extends Source {
}
public function page($url) {
$game = new Game;
$game->url = $url;
$game = Game::where('url', $url)->first();
if (!$game) {
$game = new Game();
$game->url = $url;
}
$text = $this->get_text($url);
$this->loadStr($text);
$title = $this->dom->filter('h2 a b');
if ($title->count() > 0) {
$title = $title->first()->text();