2020-04-22 12:38:59 -04:00
|
|
|
<?php declare(strict_types=1);
|
|
|
|
/**
|
|
|
|
* Hummingbird Anime List Client
|
|
|
|
*
|
|
|
|
* An API client for Kitsu to manage anime and manga watch lists
|
|
|
|
*
|
2021-02-04 11:57:01 -05:00
|
|
|
* PHP version 8
|
2020-04-22 12:38:59 -04:00
|
|
|
*
|
|
|
|
* @package HummingbirdAnimeClient
|
|
|
|
* @author Timothy J. Warren <tim@timshomepage.net>
|
2021-01-13 01:52:03 -05:00
|
|
|
* @copyright 2015 - 2021 Timothy J. Warren
|
2020-04-22 12:38:59 -04:00
|
|
|
* @license http://www.opensource.org/licenses/mit-license.html MIT License
|
2020-12-10 17:06:50 -05:00
|
|
|
* @version 5.2
|
2020-04-22 12:38:59 -04:00
|
|
|
* @link https://git.timshomepage.net/timw4mail/HummingBirdAnimeClient
|
|
|
|
*/
|
|
|
|
|
|
|
|
namespace Aviat\AnimeClient\API\Kitsu\Transformer;
|
|
|
|
|
|
|
|
use Aviat\AnimeClient\Types\HistoryItem;
|
|
|
|
use DateTimeImmutable;
|
|
|
|
use DateTimeInterface;
|
|
|
|
use DateTimeZone;
|
|
|
|
|
|
|
|
abstract class HistoryTransformer {
|
|
|
|
/**
|
|
|
|
* @var string The media type
|
|
|
|
*/
|
|
|
|
protected string $type;
|
|
|
|
|
2020-04-22 17:53:25 -04:00
|
|
|
/**
|
|
|
|
* @var string The message for watching/reading a single episode/chapter
|
|
|
|
*/
|
|
|
|
protected string $progressAction;
|
|
|
|
|
|
|
|
/**
|
2020-04-24 14:14:52 -04:00
|
|
|
* @var string The message for rewatching/rereading episode(s)/chapter(s)
|
2020-04-22 17:53:25 -04:00
|
|
|
*/
|
2020-04-24 14:14:52 -04:00
|
|
|
protected string $reconsumeAction;
|
2020-04-22 17:53:25 -04:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @var string The message for going through a large number of media in a series
|
|
|
|
*/
|
|
|
|
protected string $largeAggregateAction;
|
|
|
|
|
2020-04-24 14:14:52 -04:00
|
|
|
/**
|
|
|
|
* @var string The status for items you are rewatching/rereading
|
|
|
|
*/
|
|
|
|
protected string $reconsumingStatus;
|
|
|
|
|
2020-04-22 12:38:59 -04:00
|
|
|
/**
|
|
|
|
* @var array The mapping of api status to display status
|
|
|
|
*/
|
|
|
|
protected array $statusMap;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Convert raw history
|
|
|
|
*
|
|
|
|
* @param array $data
|
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
public function transform(array $data): array
|
|
|
|
{
|
2020-08-24 19:17:41 -04:00
|
|
|
$base = $data['data']['findProfileBySlug']['libraryEvents']['nodes'] ?? [];
|
2020-04-22 12:38:59 -04:00
|
|
|
$output = [];
|
|
|
|
|
2020-08-24 19:17:41 -04:00
|
|
|
foreach ($base as $entry)
|
2020-04-22 12:38:59 -04:00
|
|
|
{
|
2020-08-24 19:17:41 -04:00
|
|
|
if ( ! (strtolower($entry['media']['__typename']) === $this->type))
|
2020-04-22 12:38:59 -04:00
|
|
|
{
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2020-08-24 19:17:41 -04:00
|
|
|
// Hide private library entries
|
|
|
|
if ($entry['libraryEntry']['private'] === true)
|
|
|
|
{
|
|
|
|
continue;
|
|
|
|
}
|
2020-04-22 12:38:59 -04:00
|
|
|
|
2020-08-24 19:17:41 -04:00
|
|
|
$kind = strtolower($entry['kind']);
|
|
|
|
|
|
|
|
if ($kind === 'progressed' && ! empty($entry['changedData']['progress']))
|
2020-04-22 12:38:59 -04:00
|
|
|
{
|
2020-04-24 14:18:35 -04:00
|
|
|
$transformed = $this->transformProgress($entry);
|
|
|
|
if ($transformed !== NULL)
|
|
|
|
{
|
|
|
|
$output[] = $transformed;
|
|
|
|
}
|
2020-04-22 12:38:59 -04:00
|
|
|
}
|
|
|
|
else if ($kind === 'updated')
|
|
|
|
{
|
|
|
|
$output[] = $this->transformUpdated($entry);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return $this->aggregate($output);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Combine consecutive 'progressed' events
|
|
|
|
*
|
|
|
|
* @param array $singles
|
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
protected function aggregate (array $singles): array
|
|
|
|
{
|
|
|
|
$output = [];
|
|
|
|
|
|
|
|
$count = count($singles);
|
|
|
|
for ($i = 0; $i < $count; $i++)
|
|
|
|
{
|
|
|
|
$entries = [];
|
|
|
|
$entry = $singles[$i];
|
|
|
|
$prevTitle = $entry['title'];
|
|
|
|
$nextId = $i;
|
|
|
|
$next = $singles[$nextId];
|
|
|
|
while (
|
|
|
|
$next['kind'] === 'progressed' &&
|
|
|
|
$next['title'] === $prevTitle
|
|
|
|
) {
|
|
|
|
$entries[] = $next;
|
|
|
|
$prevTitle = $next['title'];
|
|
|
|
|
|
|
|
if ($nextId + 1 < $count)
|
|
|
|
{
|
|
|
|
$nextId++;
|
|
|
|
$next = $singles[$nextId];
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (count($entries) > 1)
|
|
|
|
{
|
2020-04-22 17:53:25 -04:00
|
|
|
$items = [];
|
2020-04-22 12:38:59 -04:00
|
|
|
$updated = [];
|
|
|
|
|
|
|
|
foreach ($entries as $e)
|
|
|
|
{
|
2020-08-24 19:17:41 -04:00
|
|
|
$progressItem = $e['original']['changedData']['progress'];
|
2020-04-24 14:14:52 -04:00
|
|
|
$items[] = array_pop($progressItem);
|
2020-04-22 12:38:59 -04:00
|
|
|
$updated[] = $e['updated'];
|
|
|
|
}
|
2020-04-22 17:53:25 -04:00
|
|
|
$firstItem = min($items);
|
|
|
|
$lastItem = max($items);
|
2020-04-22 12:38:59 -04:00
|
|
|
$firstUpdate = min($updated);
|
|
|
|
$lastUpdate = max($updated);
|
|
|
|
|
|
|
|
$title = $entries[0]['title'];
|
|
|
|
|
2020-04-24 14:14:52 -04:00
|
|
|
if ($this->isReconsuming($entries[0]['original']))
|
|
|
|
{
|
|
|
|
$action = "{$this->reconsumeAction}s {$firstItem}-{$lastItem}";
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
$action = (count($entries) > 3)
|
|
|
|
? "{$this->largeAggregateAction} {$firstItem}-{$lastItem}"
|
|
|
|
: "{$this->progressAction}s {$firstItem}-{$lastItem}";
|
|
|
|
}
|
2020-04-22 12:38:59 -04:00
|
|
|
|
|
|
|
$output[] = HistoryItem::from([
|
|
|
|
'action' => $action,
|
|
|
|
'coverImg' => $entries[0]['coverImg'],
|
|
|
|
'dateRange' => [$firstUpdate, $lastUpdate],
|
|
|
|
'isAggregate' => true,
|
2020-04-24 14:14:52 -04:00
|
|
|
'original' => $entries,
|
2020-04-22 12:38:59 -04:00
|
|
|
'title' => $title,
|
|
|
|
'updated' => $entries[0]['updated'],
|
|
|
|
'url' => $entries[0]['url'],
|
|
|
|
]);
|
|
|
|
|
|
|
|
// Skip the rest of the aggregate in the main loop
|
|
|
|
$i += count($entries) - 1;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
$output[] = $entry;
|
|
|
|
}
|
|
|
|
|
|
|
|
return $output;
|
|
|
|
}
|
|
|
|
|
2020-04-24 14:18:35 -04:00
|
|
|
protected function transformProgress (array $entry): ?HistoryItem
|
2020-04-22 12:38:59 -04:00
|
|
|
{
|
2020-08-24 19:17:41 -04:00
|
|
|
$id = $entry['media']['id'];
|
|
|
|
$data = $entry['media'];
|
2020-04-22 12:38:59 -04:00
|
|
|
$title = $this->linkTitle($data);
|
|
|
|
$imgUrl = "images/{$this->type}/{$id}.webp";
|
2020-08-24 19:17:41 -04:00
|
|
|
$item = end($entry['changedData']['progress']);
|
2020-04-24 14:14:52 -04:00
|
|
|
|
2020-04-24 14:18:35 -04:00
|
|
|
// No showing episode 0 nonsense
|
|
|
|
if (((int)$item) === 0)
|
|
|
|
{
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2020-04-24 14:14:52 -04:00
|
|
|
$action = ($this->isReconsuming($entry))
|
|
|
|
? "{$this->reconsumeAction} {$item}"
|
|
|
|
: "{$this->progressAction} {$item}";
|
2020-04-22 12:38:59 -04:00
|
|
|
|
|
|
|
return HistoryItem::from([
|
2020-04-24 14:14:52 -04:00
|
|
|
'action' => $action,
|
2020-04-22 12:38:59 -04:00
|
|
|
'coverImg' => $imgUrl,
|
|
|
|
'kind' => 'progressed',
|
|
|
|
'original' => $entry,
|
|
|
|
'title' => $title,
|
2020-08-24 19:17:41 -04:00
|
|
|
'updated' => $this->parseDate($entry['updatedAt']),
|
2020-04-22 12:38:59 -04:00
|
|
|
'url' => $this->getUrl($data),
|
|
|
|
]);
|
|
|
|
}
|
|
|
|
|
|
|
|
protected function transformUpdated($entry): HistoryItem
|
|
|
|
{
|
2020-08-24 19:17:41 -04:00
|
|
|
$id = $entry['media']['id'];
|
|
|
|
$data = $entry['media'];
|
2020-04-22 12:38:59 -04:00
|
|
|
$title = $this->linkTitle($data);
|
|
|
|
$imgUrl = "images/{$this->type}/{$id}.webp";
|
|
|
|
|
2020-08-24 19:17:41 -04:00
|
|
|
$kind = array_key_first($entry['changedData']);
|
2020-04-22 12:38:59 -04:00
|
|
|
|
|
|
|
if ($kind === 'status')
|
|
|
|
{
|
2020-08-24 19:17:41 -04:00
|
|
|
$status = array_pop($entry['changedData']['status']);
|
2020-04-22 12:38:59 -04:00
|
|
|
$statusName = $this->statusMap[$status];
|
|
|
|
|
2020-04-24 14:14:52 -04:00
|
|
|
if ($this->isReconsuming($entry))
|
|
|
|
{
|
|
|
|
$statusName = ($statusName === 'Completed')
|
|
|
|
? "Finished {$this->reconsumingStatus}"
|
|
|
|
: $this->reconsumingStatus;
|
|
|
|
}
|
|
|
|
|
2020-04-22 12:38:59 -04:00
|
|
|
return HistoryItem::from([
|
|
|
|
'action' => $statusName,
|
|
|
|
'coverImg' => $imgUrl,
|
|
|
|
'kind' => 'updated',
|
|
|
|
'original' => $entry,
|
|
|
|
'title' => $title,
|
2020-08-24 19:17:41 -04:00
|
|
|
'updated' => $this->parseDate($entry['updatedAt']),
|
2020-04-22 12:38:59 -04:00
|
|
|
'url' => $this->getUrl($data),
|
|
|
|
]);
|
|
|
|
}
|
|
|
|
|
|
|
|
return $entry;
|
|
|
|
}
|
|
|
|
|
|
|
|
protected function linkTitle (array $data): string
|
|
|
|
{
|
2020-08-24 19:17:41 -04:00
|
|
|
return $data['titles']['canonical'];
|
2020-04-22 12:38:59 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
protected function parseDate (string $date): DateTimeImmutable
|
|
|
|
{
|
|
|
|
$dateTime = DateTimeImmutable::createFromFormat(
|
2020-08-24 19:17:41 -04:00
|
|
|
DateTimeInterface::RFC3339,
|
2020-04-22 12:38:59 -04:00
|
|
|
$date
|
|
|
|
);
|
|
|
|
|
|
|
|
return $dateTime->setTimezone(new DateTimeZone(date_default_timezone_get()));
|
|
|
|
}
|
|
|
|
|
|
|
|
protected function getUrl (array $data): string
|
|
|
|
{
|
|
|
|
return "/{$this->type}/details/{$data['slug']}";
|
|
|
|
}
|
2020-04-24 14:14:52 -04:00
|
|
|
|
|
|
|
protected function isReconsuming ($entry): bool
|
|
|
|
{
|
2020-08-24 19:17:41 -04:00
|
|
|
return $entry['libraryEntry']['reconsuming'];
|
2020-04-24 14:14:52 -04:00
|
|
|
}
|
2020-04-22 12:38:59 -04:00
|
|
|
}
|