Beginning move to PHP implementation

This commit is contained in:
ilumos 2017-08-14 17:49:04 +01:00
parent 1da757a76a
commit 06b882a9b0
8 changed files with 1217 additions and 1 deletions

2
.gitignore vendored
View file

@ -1 +1 @@
*.json
/vendor/

12
composer.json Normal file
View file

@ -0,0 +1,12 @@
{
"require": {
"illuminate/console": "^5.4",
"illuminate/container": "^5.4",
"illuminate/events": "^5.4",
"illuminate/database": "^5.4",
"guzzlehttp/guzzle": "^6.3"
},
"autoload": {
"psr-4": {"Zeropingheroes\\LancacheAutofill\\": "src/"}
},
}

1011
composer.lock generated Normal file

File diff suppressed because it is too large Load diff

0
database.sqlite Normal file
View file

36
lancache-autofill Normal file
View file

@ -0,0 +1,36 @@
#!/usr/bin/env php
<?php
use Illuminate\Console\Application;
use Illuminate\Container\Container;
use Illuminate\Events\Dispatcher;
use Illuminate\Database\Capsule\Manager as Capsule;
use Zeropingheroes\LancacheAutofill\Console\Commands\Steam\UpdateAppList;
use Zeropingheroes\LancacheAutofill\Console\Commands\Steam\SearchApps;
use Zeropingheroes\LancacheAutofill\Console\Commands\CreateDatabase;
if (file_exists($a = __DIR__.'/../../autoload.php')) {
require_once $a;
} else {
require_once __DIR__.'/vendor/autoload.php';
}
// Set up the app
$app = new Application(new Container, new Dispatcher, '5.4');
// Set up the database
$capsule = new Capsule;
$capsule->addConnection([
'driver' => 'sqlite',
'database' => __DIR__.'/database.sqlite',
'prefix' => '',
]);
$capsule->setAsGlobal();
// Add commands
$app->add(new UpdateAppList);
$app->add(new SearchApps);
$app->add(new CreateDatabase);
$app->run();

View file

@ -0,0 +1,42 @@
<?php
namespace Zeropingheroes\LancacheAutofill\Console\Commands;
use Illuminate\Console\Command;
use Illuminate\Database\Capsule\Manager as Capsule;
class CreateDatabase extends Command
{
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'app:create-database';
/**
* The console command description.
*
* @var string
*/
protected $description = 'Create the database';
/**
* Execute the console command.
*
* @return mixed
*/
public function handle()
{
$this->info('Creating database');
Capsule::schema()->create('steam_apps', function ($table) {
$table->integer('appid')->unique();
$table->string('name');
});
Capsule::schema()->create('dummy', function ($table) {
$table->increments('id')->unique();
});
}
}

View file

@ -0,0 +1,40 @@
<?php
namespace Zeropingheroes\LancacheAutofill\Console\Commands\Steam;
use Illuminate\Console\Command;
use Illuminate\Database\Capsule\Manager as Capsule;
class SearchApps extends Command
{
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'steam:search-apps {name}';
/**
* The console command description.
*
* @var string
*/
protected $description = 'Search Steam apps by name';
/**
* Execute the console command.
*
* @return mixed
*/
public function handle()
{
$apps = Capsule::table('steam_apps')
->where('name', 'like', '%' . $this->argument('name') . '%')
->get();
foreach($apps as $app)
{
$this->info($app->appid ."\t". $app->name);
}
}
}

View file

@ -0,0 +1,75 @@
<?php
namespace Zeropingheroes\LancacheAutofill\Console\Commands\Steam;
use Illuminate\Console\Command;
use Illuminate\Database\Capsule\Manager as Capsule;
use GuzzleHttp\Exception\GuzzleException;
use GuzzleHttp\Client;
class UpdateAppList extends Command
{
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'steam:update-app-list';
/**
* The console command description.
*
* @var string
*/
protected $description = 'Get the latest list of apps from Steam';
/**
* The URL to get the list of Steam apps from.
*
* @var string
*/
const STEAM_APP_LIST_URL = 'https://api.steampowered.com/ISteamApps/GetAppList/v2';
/**
* Execute the console command.
*
* @return mixed
*/
public function handle()
{
$this->info('Clearing apps from database');
Capsule::table('steam_apps')->truncate();
$this->info('Downloading app list from Steam Web API');
$client = new Client();
$result = $client->request('GET', self::STEAM_APP_LIST_URL);
if ( $result->getStatusCode() != 200 )
{
$this->error('Steam Web API unreachable');
die();
}
$response = json_decode($result->getBody(), TRUE);
$apps = $response['applist']['apps'];
$appsChunked = array_chunk($apps, 500);
$bar = $this->output->createProgressBar(count($appsChunked));
$bar->setFormat("%bar% %percent%%");
$this->info('Inserting records into database');
foreach($appsChunked as $appChunk)
{
Capsule::table('steam_apps')->insert($appChunk);
$bar->advance();
}
$bar->finish();
$this->info(PHP_EOL . 'Done');
}
}