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

99 lines
3.4 KiB
PHP

<?php
/*
A set of utilities for tracking text-based game releases
Copyright (C) 2017-2018 Alexander Yakovlev
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/>.
*/
namespace Source;
use \Game;
use \Pandoc\Pandoc;
class Itch extends Source {
public $title = "Itch.io";
public $games = [];
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->games[] = $game->print();
return $game;
},
];
try {
$dom = $service->parse($xml);
} catch (\Exception $e) {} // ignore malformed XML
}
protected function parse() {
global $argv;
if (isset($argv[2])) {
$game_page = $this->get_text($argv[2]);
$this->loadStr($game_page, []);
$this->output .= $this->page($argv[2])->print();
} else {
$this->parse_tag("https://itch.io/games/newest/tag-text-based.xml");
$this->parse_tag("https://itch.io/games/newest/tag-twine.xml");
$this->parse_tag("https://itch.io/games/newest/tag-interactive-fiction.xml");
$this->games = array_unique($this->games);
foreach ($this->games as $game) {
$this->output .= $game;
}
}
}
public function checkPage($url) {
return (strpos($url,'.itch.io/') !== FALSE);
}
public function page($url) {
$game = new Game;
$game->url = $url;
$title = trim($this->dom->filter("title")->first()->text());
[$game->title, $game->author] = explode(' by ', $title);
unset($title);
$desc = $this->dom->filter('.formatted_description');
try {
$game->description = trim($desc->first()->html());
} catch (\Throwable $e) {}
$converter = new Pandoc();
$game->description = $converter->convert($game->description, 'html', 'mediawiki');
return $game;
}
}