Archived
1
0
Fork 0

Print the list of games

This commit is contained in:
Alexander Yakovlev 2020-04-10 10:35:55 +07:00
parent 95462b993b
commit 027ab478ec
Signed by: oreolek
GPG key ID: 1CDC4B7820C93BD3
4 changed files with 125 additions and 40 deletions

View file

@ -28,15 +28,15 @@ class Collect extends Command
* @var array
*/
protected $parsers = [
//'Anivisual',
//'Hyperbook',
//'HyperbookEn',
//'Apero',
//'Questbook',
//'Axma',
//'IFDB',
//'Itch',
//'Instead',
'Anivisual',
'Hyperbook',
'HyperbookEn',
'Apero',
'Questbook',
'Axma',
'IFDB',
'Itch',
'Instead',
'Steam',
/*
'Urq',

View file

@ -0,0 +1,62 @@
<?php
namespace App\Commands;
use LaravelZero\Framework\Commands\Command;
use Log;
use App\Models\Game;
class Freshlist extends Command
{
/**
* The signature of the command.
*
* @var string
*/
protected $signature = 'freshlist {keyword?}';
/**
* The description of the command.
*
* @var string
*/
protected $description = 'Print new games';
/**
* Execute the console command.
*
* @return mixed
*/
public function handle()
{
$now = date('Y-m-d');
$week_ago = date('Y-m-d', strtotime('1 week ago'));
$games = Game::with('authors')
->where('release_date', '<=', $now)
->where('release_date', '>', $week_ago)
->orderBy('source')
->get();
echo '<ul>';
foreach ($games as $game) {
echo '<li>';
$authors = $game->authors->toArray();
if (!empty($authors)) {
echo '<em>';
echo implode(', ', array_map(function($model) {
return trim($model['name']);
}, $authors));
echo '</em> — ';
}
echo "<a rel='nofollow' target='_blank' href='".trim($game->url)."'>«".htmlentities(trim($game->title))."»</a>";
$description = $game->short_description;
if (empty($description)) {
$description = $game->description;
}
if (!empty($description)) {
echo PHP_EOL.'<blockquote>'.htmlentities(html_entity_decode($description)).'</blockquote>';
}
echo '</li>'.PHP_EOL;
}
echo '</ul>';
}
}

View file

@ -20,6 +20,7 @@ namespace App;
use Illuminate\Support\Facades\Cache;
use \GuzzleHttp\Client as GuzzleClient;
use Log;
class Downloader {
/**
@ -37,32 +38,37 @@ class Downloader {
if (env('DEBUG') && Cache::has($url)) {
return Cache::get($url);
}
if ($post === []) {
$response = $this->client->request('GET', $url, [
'cookies' => $this->cookies,
]);
} else {
$response = $this->client->request('POST', $url, [
'form_params' => $post,
'cookies' => $this->cookies,
]);
try {
if ($post === []) {
$response = $this->client->request('GET', $url, [
'cookies' => $this->cookies,
]);
} else {
$response = $this->client->request('POST', $url, [
'form_params' => $post,
'cookies' => $this->cookies,
]);
}
} catch (\Exception $e) {
Log::warning($e->getMessage());
return '';
}
$resp = (string) $response->getBody();
Cache::put($url, $resp);
return $resp;
}
public function download($url, $outFile) {
$options = array(
CURLOPT_FILE => fopen($outFile, 'w'),
CURLOPT_TIMEOUT => 28800, // set this to 8 hours so we dont timeout on big files
CURLOPT_URL => $url
);
public function download($url, $outFile) {
$options = array(
CURLOPT_FILE => fopen($outFile, 'w'),
CURLOPT_TIMEOUT => 28800, // set this to 8 hours so we dont timeout on big files
CURLOPT_URL => $url
);
$ch = curl_init();
curl_setopt_array($ch, $options);
curl_exec($ch);
curl_close($ch);
$ch = curl_init();
curl_setopt_array($ch, $options);
curl_exec($ch);
curl_close($ch);
}
public function setCookies($cookies): void {

View file

@ -7,25 +7,42 @@ use App\Models\Author;
use App\Models\Language;
use App\Models\Tag;
use App\Models\Platform;
use App\Events\GameSaving;
class Game extends Model
{
protected $table = 'games';
public $guarded = ['id'];
public function authors() {
return $this->belongsToMany(Author::class, 'authors_games');
}
/**
* The "booted" method of the model.
*
* @return void
*/
protected static function booted()
{
static::saving(function ($game) {
// Replace non-breaking space with regular one
$game->title = str_replace(' ', ' ', $game->title);
$game->title = trim($game->title);
$game->description = str_replace(' ', ' ', $game->description);
$game->description = trim($game->description);
});
}
public function languages() {
return $this->belongsToMany(Language::class, 'languages_games');
}
public function authors() {
return $this->belongsToMany(Author::class, 'authors_games');
}
public function tags() {
return $this->belongsToMany(Tag::class, 'tags_games');
}
public function languages() {
return $this->belongsToMany(Language::class, 'languages_games');
}
public function platforms() {
return $this->belongsToMany(Platform::class, 'games_platforms', 'game_id', 'platform_id');
}
public function tags() {
return $this->belongsToMany(Tag::class, 'tags_games');
}
public function platforms() {
return $this->belongsToMany(Platform::class, 'games_platforms', 'game_id', 'platform_id');
}
}