Бот для создания страниц под игры КРИЛ 2018
This commit is contained in:
parent
73900424f4
commit
c5aa71759c
29
Game.php
29
Game.php
|
@ -29,7 +29,25 @@ class Game {
|
|||
* @var DateTime
|
||||
*/
|
||||
public $date;
|
||||
/**
|
||||
* Path or URL to game cover.
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
public $image;
|
||||
/**
|
||||
* Binary image data, game cover.
|
||||
* Most likely product of `file_get_contents`
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
public $image_data;
|
||||
/**
|
||||
* Cover data extension (jpg, png)
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
public $image_extension = 'jpg';
|
||||
public $platform;
|
||||
public $url_online;
|
||||
public $url_download;
|
||||
|
@ -37,6 +55,17 @@ class Game {
|
|||
public $url_download_description;
|
||||
public $url_online_description;
|
||||
public $language;
|
||||
/**
|
||||
* Темы на вики.
|
||||
*
|
||||
* @var string[]
|
||||
*/
|
||||
public $themes;
|
||||
/**
|
||||
* Категории на вики.
|
||||
*
|
||||
* @var string[]
|
||||
*/
|
||||
public $categories;
|
||||
public function print() {
|
||||
$converter = new Pandoc();
|
||||
|
|
|
@ -52,7 +52,7 @@ class Instory extends Source {
|
|||
$game->title = trim($gameBlock['title']);
|
||||
$game->url = trim($gameBlock['link']);
|
||||
$game->description = trim($gameBlock['description']);
|
||||
$game->author = trim($gameBlock['dc:creator']);
|
||||
$game->author = trim($gameBlock['{http://purl.org/dc/elements/1.1/}creator']);
|
||||
$this->output .= $game->print();
|
||||
}
|
||||
}
|
||||
|
|
22
Wikipage.php
22
Wikipage.php
|
@ -65,6 +65,14 @@ class Wikipage {
|
|||
$extension = pathinfo($filename, PATHINFO_EXTENSION);
|
||||
$this->covername = $slugify->slugify($this->game->title, '_').'.'.$extension;
|
||||
}
|
||||
if (!empty($this->game->image_data)) {
|
||||
$extension = 'jpg';
|
||||
if (isset($this->game->image_extension)) {
|
||||
$extension = $this->game->image_extension;
|
||||
}
|
||||
$this->covername = $slugify->slugify($this->game->title, '_').'.'.$extension;
|
||||
$filename = $this->covername.'.'.$extension;
|
||||
}
|
||||
|
||||
$pagetitle = strtr($this->game->title, [
|
||||
'|' => '-'
|
||||
|
@ -84,6 +92,14 @@ class Wikipage {
|
|||
unlink($filename);
|
||||
}
|
||||
}
|
||||
if (!empty($this->game->image_data)) {
|
||||
if ($this->services->newPageGetter()->getFromTitle($this->covername)) {
|
||||
$image = $this->game->image_data;
|
||||
file_put_contents($filename, $image);
|
||||
$this->fileUploader->upload($this->covername, $filename);
|
||||
unlink($filename);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
$this->makeContent();
|
||||
|
@ -136,7 +152,13 @@ class Wikipage {
|
|||
}
|
||||
$this->txtadd('platform', ' |платформа='.$this->game->platform);
|
||||
$this->txtadd('image', ' |обложка='.$this->covername);
|
||||
$this->txtadd('image_data', ' |обложка='.$this->covername);
|
||||
$this->txtadd('language', ' |язык='.$this->game->language);
|
||||
if (is_array($this->game->themes) && !empty($this->game->themes)) {
|
||||
$this->content .= PHP_EOL.' |темы='.implode(',', $this->game->themes);
|
||||
} else if (!empty($this->game->themes)) {
|
||||
$this->content .= PHP_EOL.' |темы='.$this->game->themes;
|
||||
}
|
||||
|
||||
$this->content .= "\n}}\n";
|
||||
|
||||
|
|
89
kril.php
Executable file
89
kril.php
Executable file
|
@ -0,0 +1,89 @@
|
|||
#!/usr/bin/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/>.
|
||||
*/
|
||||
use Symfony\Component\Yaml\Yaml;
|
||||
use \GuzzleHttp\Client as GuzzleClient;
|
||||
|
||||
require "vendor/autoload.php";
|
||||
require "Game.php";
|
||||
require "Source.php";
|
||||
require "Wikipage.php";
|
||||
$config = Yaml::parse(file_get_contents('config.yml'));
|
||||
$pandoc = new Pandoc\Pandoc();
|
||||
$loader = new \Aura\Autoload\Loader;
|
||||
$loader->register();
|
||||
$loader->addPrefix('Source', 'Source');
|
||||
$client = new GuzzleClient([
|
||||
'timeout' => 30,
|
||||
]);
|
||||
|
||||
/**
|
||||
* Function to download images.
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
function get_file($url) {
|
||||
}
|
||||
|
||||
$api = json_decode(file_get_contents('http://forum.ifiction.ru/extern.php?action=kril2018'));
|
||||
foreach ($api as $category) {
|
||||
$category_title = $category->category;
|
||||
$games = $category->games;
|
||||
foreach ($games as $game_data) {
|
||||
$game = new Game;
|
||||
$game->title = $game_data->full_title;
|
||||
$game->url_discussion = $game_data->url;
|
||||
$game->language = 'русский';
|
||||
$game->themes = [$game_data->rating];
|
||||
$game->description = $pandoc->runWith($game_data->description, [
|
||||
'from' => 'html',
|
||||
'to' => 'mediawiki'
|
||||
]);
|
||||
if (isset($game_data->posters[0])) {
|
||||
$image_url = $game_data->posters[0];
|
||||
$response = $client->request('GET', $image_url, [
|
||||
'sink' => 'tempfile.jpg',
|
||||
]);
|
||||
$filename = $response->getHeaderLine('Content-Disposition');
|
||||
preg_match('/\.(\w+)";\ssize/', $filename, $matches);
|
||||
if (isset($matches[1])) {
|
||||
$game->image_extension = $matches[1];
|
||||
}
|
||||
$image = file_get_contents('tempfile.jpg');
|
||||
$game->image_data = $image;
|
||||
unlink('tempfile.jpg');
|
||||
}
|
||||
$game->date = new \DateTime('08.12.2018');
|
||||
$game->author = $game_data->authors;
|
||||
$game->platform = $game_data->platform;
|
||||
$files = $game_data->files;
|
||||
if (isset($files[0])) {
|
||||
$game->url_download = $files[0]->url;
|
||||
$game->url_download_description = $files[0]->title;
|
||||
}
|
||||
if (isset($files[1])) {
|
||||
$game->url_online = $files[1]->url;
|
||||
$game->url_online_description = $files[1]->title;
|
||||
}
|
||||
if ($game) {
|
||||
$page = new Wikipage($game);
|
||||
$page->create();
|
||||
}
|
||||
}
|
||||
}
|
Reference in a new issue