ifnews2/app/Sources/VNDB.php

94 lines
2.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 App\Sources;
use \App\Models\Game;
use \App\Source;
class VNDB extends Source {
public $title = "VNDB";
public $keyword = 'vndb';
protected $fp;
public function parse() {
$this->fp = fsockopen("api.vndb.org", 19534, $errno, $errstr, 10);
$data = array(
'protocol' => 1,
'client' => 'ifnews',
'clientver' => 1.0,
'username' => env('VNDB_USER'),
'password' => env('VNDB_PASSWORD'),
);
$response = $this->sendCommand('login', $data);
if (empty($response)) {
return;
}
$date = (new \DateTime("1 week ago"))->format('Y-m-d');
$list = $this->sendCommand('get vn basic (released > "'.$date.'")');
/*
foreach ($list['items'] as $gameData) {
$game = new Game;
$game->title = $gameData->title;
$game = $this->findGame($game);
// $res = $client->getVisualNovelDataById(5);
}
*/
}
public function sendCommand($command, $data = null)
{
$packet = $command;
if ($data) {
$packet .= ' ' . json_encode($data);
}
//echo "SENDING: [$packet]";
fwrite($this->fp, $packet . chr(0x04));
$res = $this->getResponse();
if ($res === 'ok') {
return 'ok';
} else {
$p = strpos($res, '{');
if ($p>0) {
// $type = substr($res, 0, $p - 1);
$json = substr($res, $p);
$data = json_decode($json, true);
return $data;
}
}
}
public function getResponse()
{
$buffer = '';
while (!feof($this->fp)) {
$c = fgets($this->fp, 2);
if (ord($c)==0x04) {
return $buffer;
} else {
$buffer .= $c;
}
}
return null;
}
}