. */ namespace App\Sources; use \App\Models\Game; use \App\Models\Platform; use \App\Models\Language; use \App\Models\Author; use \App\Models\Tag; use \App\Source; use Log; class Questbook extends Source { public $title = "Сторигеймы"; public $keyword = 'questbook'; protected $platform_atril; protected $platform_pdf; protected $language_model; public function parse() { $this->language_model = Language::findByCode('ru'); $this->platform_atril = Platform::findByName('Атрил'); $this->platform_pdf = Platform::findByName('PDF'); $this->parseFeed('https://quest-book.ru/directory/rss/'); $this->parseFeed('https://quest-book.ru/online/rss.xml'); } public function parseFeed($feedUrl) { $string = $this->get_text($feedUrl); $string = mb_convert_encoding($string, 'UTF-8', 'auto'); $service = new \Sabre\Xml\Service(); $service->elementMap = [ '{}item' => function(\Sabre\Xml\Reader $reader) { return \Sabre\Xml\Deserializer\keyValue($reader, ''); }, '{}channel' => function(\Sabre\Xml\Reader $reader) { return \Sabre\Xml\Deserializer\repeatingElements($reader, '{}item'); }, ]; try { $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 = new \DateTime($gameBlock['pubDate']); $game->url = trim($gameBlock['link'] ?? ''); $game->url = str_replace('http://', 'https://', $game->url); $source_id = NULL; 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($game->url, '/directory/') !== false) { $url = str_replace('https://quest-book.ru/directory/', '', $game->url); $url = explode('/', $url); $url = array_filter($url); $source_id = reset($url); } if (!empty($source_id)) { $game->source_id = $source_id; } $game->description = trim($gameBlock['description'] ?? ''); $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); } if (strpos($game->title, 'PDF') !== FALSE) { $game->platforms()->attach($this->platform_pdf); } else { $game->platforms()->attach($this->platform_atril); } if (!$game->languages()->where('code', $this->language_model->code)->exists()) { $game->languages()->attach($this->language_model); } } } } public function checkPage($url) { return (strpos($url,'https://quest-book.ru/') !== FALSE); } }