Archived
1
0
Fork 0
This repository has been archived on 2020-07-31. You can view files and clone it, but cannot push or open issues or pull requests.
news-script/Source/Itch.php

67 lines
2.2 KiB
PHP

<?php
namespace Source;
use \Game;
use \Pandoc\Pandoc;
class Itch extends Source {
public $title = "Itch.io";
protected function parse_tag($url) {
$service = new \Sabre\Xml\Service();
$xml = $this->get_text($url);
$service->elementMap = [
'{}item' => function(\Sabre\Xml\Reader $reader) {
$game = new Game;
$keyValue = \Sabre\Xml\Deserializer\keyValue($reader, '{}item');
if (isset($keyValue['{}pubDate'])) {
$game->date = strtotime($keyValue['{}pubDate']);
if ($game->date < $this->period) {
return $game;
}
}
if (isset($keyValue['{}plainTitle'])) {
$game->title = $keyValue['{}plainTitle'];
}
if (isset($keyValue['{}link'])) {
$game->url = $keyValue['{}link'];
}
if (isset($keyValue['{}description'])) {
$game->description = trim(strip_tags($keyValue['{}description'], '<p><a><br>'));
}
$game_page = $this->get_text($game->url);
$this->loadStr($game_page, []);
$lines = $this->dom->filter('.game_info_panel_widget tr')->each(function($line) use($game){
$text = $line->filter('td');
if (trim($text->text()) == 'Author') {
$game->author = strip_tags($text->nextAll()->first()->html());
}
});
$this->output .= $game->print();
return $game;
},
];
try {
$dom = $service->parse($xml);
} catch (\Exception $e) {} // ignore malformed XML
}
protected function parse() {
$this->parse_tag("https://itch.io/games/newest/tag-text-based.xml");
$this->parse_tag("https://itch.io/games/newest/tag-interactive-fiction.xml");
}
public function checkPage($url) {
return (strpos($url,'.itch.io/') !== FALSE);
}
public function page($url) {
$game = new Game;
$game->url = $url;
$game->platform = 'Квестер';
$title = trim($this->dom->filter("title")->first()->text());
[$game->title, $game->author] = explode(' by ', $title);
unset($title);
$game->description = trim($this->dom->filter('.formatted_description')->first()->html());
$converter = new Pandoc();
$game->description = $converter->convert($game->description, 'html', 'mediawiki');
return $game;
}
}