HummingBirdAnimeClient/src/AnimeClient.php

327 lines
6.6 KiB
PHP
Raw Normal View History

2016-10-20 22:09:36 -04:00
<?php declare(strict_types=1);
2015-11-16 19:30:04 -05:00
/**
* Hummingbird Anime List Client
2015-11-16 19:30:04 -05:00
*
2018-08-22 13:48:27 -04:00
* An API client for Kitsu to manage anime and manga watch lists
2015-11-16 19:30:04 -05:00
*
2020-03-11 16:26:17 -04:00
* PHP version 7.2
2016-08-30 10:01:18 -04:00
*
2015-11-16 19:30:04 -05:00
* @package HummingbirdAnimeClient
2016-08-30 10:01:18 -04:00
* @author Timothy J. Warren <tim@timshomepage.net>
2020-01-08 15:39:49 -05:00
* @copyright 2015 - 2020 Timothy J. Warren
2016-08-30 10:01:18 -04:00
* @license http://www.opensource.org/licenses/mit-license.html MIT License
2019-12-06 09:16:35 -05:00
* @version 4.2
* @link https://git.timshomepage.net/timw4mail/HummingBirdAnimeClient
2015-11-16 19:30:04 -05:00
*/
namespace Aviat\AnimeClient;
2018-11-29 11:00:50 -05:00
use function Amp\Promise\wait;
2020-03-11 16:26:17 -04:00
use Amp\Http\Client\Response;
use Amp\Http\Client\HttpClient;
use Amp\Http\Client\HttpClientBuilder;
2018-11-29 11:00:50 -05:00
use Aviat\Ion\ConfigInterface;
2018-08-20 13:01:16 -04:00
use Yosymfony\Toml\{Toml, TomlBuilder};
// ----------------------------------------------------------------------------
//! TOML Functions
// ----------------------------------------------------------------------------
/**
* Load configuration options from .toml files
*
* @param string $path - Path to load config
* @return array
*/
function loadToml(string $path): array
2017-12-06 14:40:13 -05:00
{
$output = [];
$files = glob("{$path}/*.toml");
foreach ($files as $file)
{
$key = str_replace('.toml', '', basename($file));
if ($key === 'admin-override')
{
continue;
}
$config = Toml::parseFile($file);
if ($key === 'config')
{
foreach($config as $name => $value)
{
$output[$name] = $value;
}
continue;
}
$output[$key] = $config;
}
return $output;
2017-12-06 14:40:13 -05:00
}
/**
* Load config from one specific TOML file
*
* @param string $filename
* @return array
*/
function loadTomlFile(string $filename): array
{
return Toml::parseFile($filename);
}
/**
* Recursively create a toml file from a data array
*
* @param TomlBuilder $builder
* @param iterable $data
* @param null $parentKey
*/
function _iterateToml(TomlBuilder $builder, iterable $data, $parentKey = NULL): void
2018-08-20 13:01:16 -04:00
{
foreach ($data as $key => $value)
{
if ($value === NULL)
{
continue;
}
if (is_scalar($value) || isSequentialArray($value))
{
// $builder->addTable('');
$builder->addValue($key, $value);
continue;
}
$newKey = ($parentKey !== NULL)
? "{$parentKey}.{$key}"
: $key;
if ( ! isSequentialArray($value))
{
$builder->addTable($newKey);
}
_iterateToml($builder, $value, $newKey);
}
}
/**
* Serialize config data into a Toml file
*
* @param mixed $data
* @return string
*/
function arrayToToml(iterable $data): string
2018-08-20 13:01:16 -04:00
{
$builder = new TomlBuilder();
_iterateToml($builder, $data);
return $builder->getTomlString();
}
/**
* Serialize toml back to an array
*
* @param string $toml
* @return array
*/
function tomlToArray(string $toml): array
{
return Toml::parse($toml);
}
// ----------------------------------------------------------------------------
//! Misc Functions
// ----------------------------------------------------------------------------
/**
* Is the array sequential, not associative?
*
* @param mixed $array
* @return bool
*/
function isSequentialArray($array): bool
{
if ( ! is_array($array))
{
return FALSE;
}
$i = 0;
foreach ($array as $k => $v)
{
if ($k !== $i++)
{
return FALSE;
}
}
return TRUE;
}
/**
* Check that folder permissions are correct for proper operation
*
* @param ConfigInterface $config
* @return array
*/
function checkFolderPermissions(ConfigInterface $config): array
{
$errors = [];
$publicDir = $config->get('asset_dir');
$pathMap = [
'app/config' => realpath(__DIR__ . '/../app/config'),
'app/logs' => realpath(__DIR__ . '/../app/logs'),
'public/images/avatars' => "{$publicDir}/images/avatars",
'public/images/anime' => "{$publicDir}/images/anime",
'public/images/characters' => "{$publicDir}/images/characters",
'public/images/manga' => "{$publicDir}/images/manga",
'public/images/people' => "{$publicDir}/images/people",
];
foreach ($pathMap as $pretty => $actual)
{
// Make sure the folder exists first
if ( ! is_dir($actual))
{
$errors['missing'][] = $pretty;
continue;
}
$writable = is_writable($actual) && is_executable($actual);
if ( ! $writable)
{
$errors['writable'][] = $pretty;
}
}
return $errors;
}
/**
* Get an API Client, with better defaults
*
* @return DefaultClient
*/
function getApiClient ()
{
static $client;
if ($client === NULL)
{
2020-03-11 16:26:17 -04:00
$client = HttpClientBuilder::buildDefault();
}
return $client;
}
2018-11-29 11:00:50 -05:00
/**
2020-03-11 16:26:17 -04:00
* Simplify making a request with Http\Client
2018-11-29 11:00:50 -05:00
*
* @param $request
* @return Response
* @throws \Throwable
*/
function getResponse ($request): Response
{
$client = getApiClient();
return wait($client->request($request));
2018-11-29 11:00:50 -05:00
}
/**
* Generate the path for the cached image from the original image
*
* @param string $kitsuUrl
* @param bool $webp
* @return string
*/
function getLocalImg ($kitsuUrl, $webp = TRUE): string
{
if ( ! is_string($kitsuUrl))
{
return 'images/placeholder.webp';
}
$parts = parse_url($kitsuUrl);
if ($parts === FALSE)
{
return 'images/placeholder.webp';
}
$file = basename($parts['path']);
$fileParts = explode('.', $file);
$ext = array_pop($fileParts);
$ext = $webp ? 'webp' : $ext;
$segments = explode('/', trim($parts['path'], '/'));
$type = $segments[0] === 'users' ? $segments[1] : $segments[0];
$id = $segments[count($segments) - 2];
return implode('/', ['images', $type, "{$id}.{$ext}"]);
}
/**
* Create a transparent placeholder image
*
* @param string $path
* @param int $width
* @param int $height
* @param string $text
*/
2018-11-09 10:38:35 -05:00
function createPlaceholderImage ($path, $width, $height, $text = 'Image Unavailable'): void
{
$width = $width ?? 200;
$height = $height ?? 200;
$img = imagecreatetruecolor($width, $height);
imagealphablending($img, TRUE);
$path = rtrim($path, '/');
// Background is the first color by default
$fillColor = imagecolorallocatealpha($img, 255, 255, 255, 127);
imagefill($img, 0, 0, $fillColor);
$textColor = imagecolorallocate($img, 64, 64, 64);
imagealphablending($img, TRUE);
// Generate placeholder text
$fontSize = 10;
$fontWidth = imagefontwidth($fontSize);
$fontHeight = imagefontheight($fontSize);
2018-11-09 10:38:35 -05:00
$length = \strlen($text);
$textWidth = $length * $fontWidth;
$fxPos = (int) ceil((imagesx($img) - $textWidth) / 2);
$fyPos = (int) ceil((imagesy($img) - $fontHeight) / 2);
// Add the image text
imagestring($img, $fontSize, $fxPos, $fyPos, $text, $textColor);
// Save the images
imagesavealpha($img, TRUE);
imagepng($img, $path . '/placeholder.png', 9);
imagedestroy($img);
$pngImage = imagecreatefrompng($path . '/placeholder.png');
imagealphablending($pngImage, TRUE);
imagesavealpha($pngImage, TRUE);
imagewebp($pngImage, $path . '/placeholder.webp');
imagedestroy($pngImage);
}