Initial checkin

This commit is contained in:
j.faassen 2015-01-03 13:19:22 +01:00
commit d6797bca95
9 changed files with 470 additions and 0 deletions

2
.gitignore vendored Normal file
View File

@ -0,0 +1,2 @@
.DS_Store
vendor/

22
LICENSE Normal file
View File

@ -0,0 +1,22 @@
The MIT License (MIT)
Copyright (c) 2014 LinkORB B.V.
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.

33
bin/vndb-client Executable file
View File

@ -0,0 +1,33 @@
#!/usr/bin/env php
<?php
function includeIfExists($file)
{
if (file_exists($file)) {
return include $file;
}
}
$loader = __DIR__ . '/../vendor/autoload.php';
if (!file_exists($loader)) {
$loader = __DIR__ . '/../../../autoload.php';
}
if (!file_exists($loader)) {
die(
'You must set up the project dependencies, run the following commands:' . PHP_EOL .
'curl -s http://getcomposer.org/installer | php' . PHP_EOL .
'php composer.phar install' . PHP_EOL
);
}
require $loader;
use Symfony\Component\Console\Application;
$application = new Application('vndb-client', '0.0.1');
$application->setCatchExceptions(true);
$application->add(new \VndbClient\Command\TestCommand());
$application->add(new \VndbClient\Command\GetByIdCommand());
$application->run();

23
composer.json Normal file
View File

@ -0,0 +1,23 @@
{
"name": "joostfaassen/vndb-client-php",
"description": "API client for vndb.org",
"homepage": "https://github.com/joostfaassen/vndb-client-php",
"keywords": ["vndb", "api", "client"],
"type": "library",
"authors": [
{
"name": "Joost Faassen",
"email": "joost@joost.cx",
"role": "Development"
}
],
"require": {
"symfony/console": "~2.4"
},
"autoload": {
"psr-4": {
"VndbClient\\": "src/"
}
},
"license": "MIT"
}

75
composer.lock generated Normal file
View File

@ -0,0 +1,75 @@
{
"_readme": [
"This file locks the dependencies of your project to a known state",
"Read more about it at http://getcomposer.org/doc/01-basic-usage.md#composer-lock-the-lock-file",
"This file is @generated automatically"
],
"hash": "417674a558aaae67911f119034cbc154",
"packages": [
{
"name": "symfony/console",
"version": "v2.6.1",
"target-dir": "Symfony/Component/Console",
"source": {
"type": "git",
"url": "https://github.com/symfony/Console.git",
"reference": "ef825fd9f809d275926547c9e57cbf14968793e8"
},
"dist": {
"type": "zip",
"url": "https://api.github.com/repos/symfony/Console/zipball/ef825fd9f809d275926547c9e57cbf14968793e8",
"reference": "ef825fd9f809d275926547c9e57cbf14968793e8",
"shasum": ""
},
"require": {
"php": ">=5.3.3"
},
"require-dev": {
"psr/log": "~1.0",
"symfony/event-dispatcher": "~2.1",
"symfony/process": "~2.1"
},
"suggest": {
"psr/log": "For using the console logger",
"symfony/event-dispatcher": "",
"symfony/process": ""
},
"type": "library",
"extra": {
"branch-alias": {
"dev-master": "2.6-dev"
}
},
"autoload": {
"psr-0": {
"Symfony\\Component\\Console\\": ""
}
},
"notification-url": "https://packagist.org/downloads/",
"license": [
"MIT"
],
"authors": [
{
"name": "Symfony Community",
"homepage": "http://symfony.com/contributors"
},
{
"name": "Fabien Potencier",
"email": "fabien@symfony.com"
}
],
"description": "Symfony Console Component",
"homepage": "http://symfony.com",
"time": "2014-12-02 20:19:20"
}
],
"packages-dev": [],
"aliases": [],
"minimum-stability": "stable",
"stability-flags": [],
"prefer-stable": false,
"prefer-lowest": false,
"platform": [],
"platform-dev": []
}

114
src/Client.php Normal file
View File

@ -0,0 +1,114 @@
<?php
namespace VndbClient;
class Client
{
private $fp;
public function __construct()
{
}
public function isConnected()
{
if ($this->fp) {
return true;
}
return false;
}
public function connect()
{
$this->fp = fsockopen("api.vndb.org", 19534, $errno, $errstr, 10);
if (!$this->fp) {
echo "ERROR: $errstr ($errno)<br />\n";
}
}
public function login($username, $password)
{
$data = array(
'protocol' => 1,
'client' => 'vndb-client-php',
'clientver' => 0.1,
'username' => $username,
'password' => $password
);
$response = $this->sendCommand('login', $data);
if ($response->getType() == 'ok') {
//echo "Login OK\n";
} else {
//echo "Login failed..\n";
}
}
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();
$response = new Response();
if ($res=='ok') {
$response->setType('ok');
} else {
$p = strpos($res, '{');
if ($p>0) {
$type = substr($res, 0, $p - 1);
$response->setType($type);
$json = substr($res, $p);
$data = json_decode($json, true);
$response->setData($data);
}
}
return $response;
}
public function getResponse()
{
//echo "Waiting for response...\n";
$buffer = '';
while (!feof($this->fp)) {
$c = fgets($this->fp, 2);
if (ord($c)==0x04) {
//echo "Received: [$buffer]\n\n";
return $buffer;
} else {
$buffer .= $c;
}
}
return null;
}
public function getVisualNovelDataById($id)
{
$res = $this->sendCommand('get vn basic,anime,details,relations,stats (id = ' . (int)$id . ')');
return $res;
}
public function getReleaseDataById($id)
{
$res = $this->sendCommand('get release basic,details,vn,producers (id = ' . (int)$id . ')');
return $res;
}
public function getProducerDataById($id)
{
$res = $this->sendCommand('get producer basic,details,relations (id = ' . (int)$id . ')');
return $res;
}
public function getCharacterDataById($id)
{
$res = $this->sendCommand('get character basic,details,meas,traits (id = ' . (int)$id . ')');
return $res;
}
}

View File

@ -0,0 +1,95 @@
<?php
namespace VndbClient\Command;
use Symfony\Component\Console\Helper\DescriptorHelper;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\Command\Command;
use Gitonomy\Git\Repository;
use VndbClient\Client;
class GetByIdCommand extends Command
{
/**
* {@inheritdoc}
*/
protected function configure()
{
$this->ignoreValidationErrors();
$this
->setName('vndb:getbyid')
->setDescription('Get data by id')
->addArgument(
'username',
InputArgument::REQUIRED,
'username'
)
->addArgument(
'password',
InputArgument::REQUIRED,
'username'
)
->addArgument(
'type',
InputArgument::REQUIRED,
'username'
)
->addArgument(
'id',
InputArgument::REQUIRED,
'username'
);
}
/**
* {@inheritdoc}
*/
protected function execute(InputInterface $input, OutputInterface $output)
{
$username = $input->getArgument('username');
$password = $input->getArgument('password');
$type = $input->getArgument('type');
$id = $input->getArgument('id');
$client = new Client();
$client->connect();
if (!$client->isConnected()) {
echo "Not connected...\n";
} else {
$client->login($username, $password);
}
$response = null;
switch ($type) {
case 'vn':
$response = $client->getVisualNovelDataById($id);
break;
case 'release':
$response = $client->getReleaseDataById($id);
break;
case 'character':
$response = $client->getCharacterDataById($id);
break;
case 'producer':
$response = $client->getProducerDataById($id);
break;
default:
echo "unsupported type. use vn, release, producer or character\n";
break;
}
if ($response) {
$this->dumpResponse($response);
}
}
private function dumpResponse($response)
{
echo "TYPE: [{$response->getType()}]\n";
echo "DATA: [" . json_encode($response->getData()) . "]\n\n";
}
}

View File

@ -0,0 +1,78 @@
<?php
namespace VndbClient\Command;
use Symfony\Component\Console\Helper\DescriptorHelper;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\Command\Command;
use Gitonomy\Git\Repository;
use VndbClient\Client;
class TestCommand extends Command
{
/**
* {@inheritdoc}
*/
protected function configure()
{
$this->ignoreValidationErrors();
$this
->setName('vndb:test')
->setDescription('Get data by id')
->addArgument(
'username',
InputArgument::REQUIRED,
'username'
)
->addArgument(
'password',
InputArgument::REQUIRED,
'username'
);
}
/**
* {@inheritdoc}
*/
protected function execute(InputInterface $input, OutputInterface $output)
{
$username = $input->getArgument('username');
$password = $input->getArgument('password');
$client = new Client();
$client->connect();
if (!$client->isConnected()) {
echo "Not connected...\n";
} else {
$client->login($username, $password);
}
$res = $client->sendCommand('dbstats');
$this->dumpResponse($res);
$res = $client->getVisualNovelDataById(14274);
$this->dumpResponse($res);
$res = $client->getReleaseDataById(21446);
$this->dumpResponse($res);
$res = $client->getProducerDataById(24);
$this->dumpResponse($res);
$res = $client->getCharacterDataById(537);
$this->dumpResponse($res);
$res = $client->getCharacterDataById(9999999537);
$this->dumpResponse($res);
}
private function dumpResponse($response)
{
echo "TYPE: [{$response->getType()}]\n";
echo "DATA: [" . json_encode($response->getData()) . "]\n\n";
}
}

28
src/Response.php Normal file
View File

@ -0,0 +1,28 @@
<?php
namespace VndbClient;
class Response
{
private $type;
private $data;
public function getType()
{
return $this->type;
}
public function setType($type)
{
$this->type = $type;
}
public function setData($data)
{
$this->data = $data;
}
public function getData()
{
return $this->data;
}
}