2020-04-21 19:22:56 -04:00
|
|
|
<?php declare(strict_types=1);
|
|
|
|
/**
|
|
|
|
* Hummingbird Anime List Client
|
|
|
|
*
|
|
|
|
* An API client for Kitsu to manage anime and manga watch lists
|
|
|
|
*
|
|
|
|
* PHP version 7.4
|
|
|
|
*
|
|
|
|
* @package HummingbirdAnimeClient
|
|
|
|
* @author Timothy J. Warren <tim@timshomepage.net>
|
|
|
|
* @copyright 2015 - 2020 Timothy J. Warren
|
|
|
|
* @license http://www.opensource.org/licenses/mit-license.html MIT License
|
|
|
|
* @version 5
|
|
|
|
* @link https://git.timshomepage.net/timw4mail/HummingBirdAnimeClient
|
|
|
|
*/
|
|
|
|
|
|
|
|
namespace Aviat\AnimeClient\API\Kitsu\Transformer;
|
|
|
|
|
|
|
|
use Aviat\AnimeClient\API\Mapping\AnimeWatchingStatus;
|
|
|
|
use Aviat\AnimeClient\Types\HistoryItem;
|
|
|
|
use Aviat\Ion\Di\ContainerAware;
|
2020-04-22 11:39:44 -04:00
|
|
|
use DateTimeImmutable;
|
|
|
|
use DateTimeInterface;
|
|
|
|
use DateTimeZone;
|
2020-04-21 19:22:56 -04:00
|
|
|
|
|
|
|
class AnimeHistoryTransformer {
|
|
|
|
use ContainerAware;
|
|
|
|
|
|
|
|
protected array $skipList = [];
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Convert raw history
|
|
|
|
*
|
|
|
|
* @param array $data
|
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
public function transform(array $data): array
|
|
|
|
{
|
|
|
|
$output = [];
|
|
|
|
|
|
|
|
foreach ($data as $id => $entry)
|
|
|
|
{
|
|
|
|
if ( ! isset($entry['relationships']['anime']))
|
|
|
|
{
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (in_array($id, $this->skipList, FALSE))
|
|
|
|
{
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2020-04-22 07:53:52 -04:00
|
|
|
$kind = $entry['attributes']['kind'];
|
|
|
|
|
2020-04-22 11:39:44 -04:00
|
|
|
if ($kind === 'progressed' && ! empty($entry['attributes']['changedData']['progress']))
|
2020-04-21 19:22:56 -04:00
|
|
|
{
|
|
|
|
$output[] = $this->transformProgress($entry);
|
|
|
|
}
|
2020-04-22 07:53:52 -04:00
|
|
|
else if ($kind === 'updated')
|
2020-04-21 19:22:56 -04:00
|
|
|
{
|
|
|
|
$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++)
|
|
|
|
{
|
2020-04-21 20:37:42 -04:00
|
|
|
$entries = [];
|
2020-04-21 19:22:56 -04:00
|
|
|
$entry = $singles[$i];
|
2020-04-21 20:10:01 -04:00
|
|
|
$prevTitle = $entry['title'];
|
2020-04-21 20:37:42 -04:00
|
|
|
$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;
|
2020-04-21 19:22:56 -04:00
|
|
|
}
|
2020-04-21 20:37:42 -04:00
|
|
|
|
|
|
|
break;
|
2020-04-21 19:22:56 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
if (count($entries) > 1)
|
|
|
|
{
|
|
|
|
$episodes = [];
|
2020-04-22 11:39:44 -04:00
|
|
|
$updated = [];
|
2020-04-21 19:22:56 -04:00
|
|
|
|
|
|
|
foreach ($entries as $e)
|
|
|
|
{
|
|
|
|
$episodes[] = max($e['original']['attributes']['changedData']['progress']);
|
2020-04-22 11:39:44 -04:00
|
|
|
$updated[] = $e['updated'];
|
2020-04-21 19:22:56 -04:00
|
|
|
}
|
|
|
|
$firstEpisode = min($episodes);
|
|
|
|
$lastEpisode = max($episodes);
|
2020-04-22 11:39:44 -04:00
|
|
|
$firstUpdate = min($updated);
|
|
|
|
$lastUpdate = max($updated);
|
2020-04-21 19:22:56 -04:00
|
|
|
|
|
|
|
$title = $entries[0]['title'];
|
|
|
|
|
|
|
|
$action = (count($entries) > 3)
|
2020-04-22 11:39:44 -04:00
|
|
|
? "Marathoned episodes {$firstEpisode}-{$lastEpisode}"
|
|
|
|
: "Watched episodes {$firstEpisode}-{$lastEpisode}";
|
2020-04-21 19:22:56 -04:00
|
|
|
|
2020-04-22 11:39:44 -04:00
|
|
|
$output[] = HistoryItem::from([
|
2020-04-21 19:22:56 -04:00
|
|
|
'action' => $action,
|
|
|
|
'coverImg' => $entries[0]['coverImg'],
|
2020-04-22 11:39:44 -04:00
|
|
|
'dateRange' => [$firstUpdate, $lastUpdate],
|
2020-04-21 19:22:56 -04:00
|
|
|
'isAggregate' => true,
|
2020-04-22 11:39:44 -04:00
|
|
|
'title' => $title,
|
2020-04-21 19:22:56 -04:00
|
|
|
'updated' => $entries[0]['updated'],
|
|
|
|
]);
|
|
|
|
|
|
|
|
// Skip the rest of the aggregate in the main loop
|
2020-04-21 20:37:42 -04:00
|
|
|
$i += count($entries) - 1;
|
2020-04-21 19:22:56 -04:00
|
|
|
continue;
|
|
|
|
}
|
2020-04-22 11:39:44 -04:00
|
|
|
|
|
|
|
$output[] = $entry;
|
2020-04-21 19:22:56 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
return $output;
|
|
|
|
}
|
|
|
|
|
2020-04-22 11:39:44 -04:00
|
|
|
protected function transformProgress ($entry): HistoryItem
|
2020-04-21 19:22:56 -04:00
|
|
|
{
|
|
|
|
$animeId = array_keys($entry['relationships']['anime'])[0];
|
|
|
|
$animeData = $entry['relationships']['anime'][$animeId]['attributes'];
|
|
|
|
$title = $this->linkTitle($animeData);
|
|
|
|
$imgUrl = 'images/anime/' . $animeId . '.webp';
|
|
|
|
$episode = max($entry['attributes']['changedData']['progress']);
|
|
|
|
|
2020-04-22 11:39:44 -04:00
|
|
|
return HistoryItem::from([
|
|
|
|
'action' => "Watched episode {$episode}",
|
2020-04-21 19:22:56 -04:00
|
|
|
'coverImg' => $imgUrl,
|
|
|
|
'kind' => 'progressed',
|
|
|
|
'original' => $entry,
|
|
|
|
'title' => $title,
|
2020-04-22 11:39:44 -04:00
|
|
|
'updated' => $this->parseDate($entry['attributes']['updatedAt']),
|
2020-04-21 19:22:56 -04:00
|
|
|
]);
|
|
|
|
}
|
|
|
|
|
2020-04-22 11:39:44 -04:00
|
|
|
protected function transformUpdated($entry): HistoryItem
|
2020-04-21 19:22:56 -04:00
|
|
|
{
|
|
|
|
$animeId = array_keys($entry['relationships']['anime'])[0];
|
|
|
|
$animeData = $entry['relationships']['anime'][$animeId]['attributes'];
|
|
|
|
$title = $this->linkTitle($animeData);
|
|
|
|
$imgUrl = 'images/anime/' . $animeId . '.webp';
|
|
|
|
|
|
|
|
$kind = array_key_first($entry['attributes']['changedData']);
|
|
|
|
|
|
|
|
if ($kind === 'status')
|
|
|
|
{
|
|
|
|
$status = array_pop($entry['attributes']['changedData']['status']);
|
|
|
|
$statusName = AnimeWatchingStatus::KITSU_TO_TITLE[$status];
|
|
|
|
|
|
|
|
if ($statusName === 'Completed')
|
|
|
|
{
|
2020-04-22 11:39:44 -04:00
|
|
|
return HistoryItem::from([
|
|
|
|
'action' => 'Completed',
|
2020-04-21 19:22:56 -04:00
|
|
|
'coverImg' => $imgUrl,
|
|
|
|
'kind' => 'updated',
|
|
|
|
'original' => $entry,
|
|
|
|
'title' => $title,
|
2020-04-22 11:39:44 -04:00
|
|
|
'updated' => $this->parseDate($entry['attributes']['updatedAt']),
|
2020-04-21 19:22:56 -04:00
|
|
|
]);
|
|
|
|
}
|
|
|
|
|
2020-04-22 11:39:44 -04:00
|
|
|
return HistoryItem::from([
|
|
|
|
'action' => "Set status to {$statusName}",
|
2020-04-21 19:22:56 -04:00
|
|
|
'coverImg' => $imgUrl,
|
|
|
|
'kind' => 'updated',
|
|
|
|
'original' => $entry,
|
|
|
|
'title' => $title,
|
2020-04-22 11:39:44 -04:00
|
|
|
'updated' => $this->parseDate($entry['attributes']['updatedAt']),
|
2020-04-21 19:22:56 -04:00
|
|
|
]);
|
|
|
|
}
|
|
|
|
|
|
|
|
return $entry;
|
|
|
|
}
|
|
|
|
|
|
|
|
protected function linkTitle (array $animeData): string
|
|
|
|
{
|
|
|
|
$url = '/anime/details/' . $animeData['slug'];
|
|
|
|
|
|
|
|
$helper = $this->getContainer()->get('html-helper');
|
|
|
|
return $helper->a($url, $animeData['canonicalTitle'], ['id' => $animeData['slug']]);
|
|
|
|
}
|
2020-04-22 11:39:44 -04:00
|
|
|
|
|
|
|
protected function parseDate (string $date): DateTimeImmutable
|
|
|
|
{
|
|
|
|
$dateTime = DateTimeImmutable::createFromFormat(
|
|
|
|
DateTimeInterface::RFC3339_EXTENDED,
|
|
|
|
$date
|
|
|
|
);
|
|
|
|
|
|
|
|
return $dateTime->setTimezone(new DateTimeZone(date_default_timezone_get()));
|
|
|
|
}
|
2020-04-21 19:22:56 -04:00
|
|
|
}
|