More refactoring of History transformers
All checks were successful
timw4mail/HummingBirdAnimeClient/pipeline/head This commit looks good

This commit is contained in:
Timothy Warren 2020-04-22 17:53:25 -04:00
parent f804cc66fb
commit 212b34ac4c
3 changed files with 35 additions and 90 deletions

View File

@ -21,5 +21,11 @@ use Aviat\AnimeClient\API\Mapping\AnimeWatchingStatus;
class AnimeHistoryTransformer extends HistoryTransformer { class AnimeHistoryTransformer extends HistoryTransformer {
protected string $type = 'anime'; protected string $type = 'anime';
protected string $progressAction = 'Watched episode';
protected string $smallAggregateAction = 'Watched episodes';
protected string $largeAggregateAction = 'Marathoned episodes';
protected array $statusMap = AnimeWatchingStatus::KITSU_TO_TITLE; protected array $statusMap = AnimeWatchingStatus::KITSU_TO_TITLE;
} }

View File

@ -27,6 +27,21 @@ abstract class HistoryTransformer {
*/ */
protected string $type; protected string $type;
/**
* @var string The message for watching/reading a single episode/chapter
*/
protected string $progressAction;
/**
* @var string The message for going though a small number of media in a series
*/
protected string $smallAggregateAction;
/**
* @var string The message for going through a large number of media in a series
*/
protected string $largeAggregateAction;
/** /**
* @var array The mapping of api status to display status * @var array The mapping of api status to display status
*/ */
@ -101,24 +116,24 @@ abstract class HistoryTransformer {
if (count($entries) > 1) if (count($entries) > 1)
{ {
$episodes = []; $items = [];
$updated = []; $updated = [];
foreach ($entries as $e) foreach ($entries as $e)
{ {
$episodes[] = max($e['original']['attributes']['changedData']['progress']); $items[] = max($e['original']['attributes']['changedData']['progress']);
$updated[] = $e['updated']; $updated[] = $e['updated'];
} }
$firstEpisode = min($episodes); $firstItem = min($items);
$lastEpisode = max($episodes); $lastItem = max($items);
$firstUpdate = min($updated); $firstUpdate = min($updated);
$lastUpdate = max($updated); $lastUpdate = max($updated);
$title = $entries[0]['title']; $title = $entries[0]['title'];
$action = (count($entries) > 3) $action = (count($entries) > 3)
? "Marathoned episodes {$firstEpisode}-{$lastEpisode}" ? "{$this->largeAggregateAction} {$firstItem}-{$lastItem}"
: "Watched episodes {$firstEpisode}-{$lastEpisode}"; : "{$this->smallAggregateAction} {$firstItem}-{$lastItem}";
$output[] = HistoryItem::from([ $output[] = HistoryItem::from([
'action' => $action, 'action' => $action,
@ -147,14 +162,10 @@ abstract class HistoryTransformer {
$data = $entry['relationships'][$this->type][$id]['attributes']; $data = $entry['relationships'][$this->type][$id]['attributes'];
$title = $this->linkTitle($data); $title = $this->linkTitle($data);
$imgUrl = "images/{$this->type}/{$id}.webp"; $imgUrl = "images/{$this->type}/{$id}.webp";
$episode = max($entry['attributes']['changedData']['progress']); $item = max($entry['attributes']['changedData']['progress']);
$action = ($this->type === 'anime')
? "Watched episode {$episode}"
: "Read chapter {$episode}";
return HistoryItem::from([ return HistoryItem::from([
'action' => $action, 'action' => "{$this->progressAction} {$item}",
'coverImg' => $imgUrl, 'coverImg' => $imgUrl,
'kind' => 'progressed', 'kind' => 'progressed',
'original' => $entry, 'original' => $entry,

View File

@ -17,87 +17,15 @@
namespace Aviat\AnimeClient\API\Kitsu\Transformer; namespace Aviat\AnimeClient\API\Kitsu\Transformer;
use Aviat\AnimeClient\API\Mapping\MangaReadingStatus; use Aviat\AnimeClient\API\Mapping\MangaReadingStatus;
use Aviat\AnimeClient\Types\HistoryItem;
class MangaHistoryTransformer extends HistoryTransformer { class MangaHistoryTransformer extends HistoryTransformer {
protected string $type = 'manga'; protected string $type = 'manga';
protected string $progressAction = 'Read chapter';
protected string $smallAggregateAction = 'Read chapters';
protected string $largeAggregateAction = 'Blew through chapters';
protected array $statusMap = MangaReadingStatus::KITSU_TO_TITLE; protected array $statusMap = MangaReadingStatus::KITSU_TO_TITLE;
/**
* 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)
{
$chapters = [];
$updated = [];
foreach ($entries as $e)
{
$chapters[] = max($e['original']['attributes']['changedData']['progress']);
$updated[] = $e['updated'];
}
$firstChapter = min($chapters);
$lastChapter = max($chapters);
$firstUpdate = min($updated);
$lastUpdate = max($updated);
$title = $entries[0]['title'];
$action = (count($entries) > 3)
? "Marathoned chapters {$firstChapter}-{$lastChapter}"
: "Watched chapters {$firstChapter}-{$lastChapter}";
$output[] = HistoryItem::from([
'action' => $action,
'coverImg' => $entries[0]['coverImg'],
'dateRange' => [$firstUpdate, $lastUpdate],
'isAggregate' => true,
'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;
}
} }