Skip to content

Commit

Permalink
Merge pull request #23 from ipkpjersi/develop
Browse files Browse the repository at this point in the history
Added MAL custom CSS export
  • Loading branch information
ipkpjersi authored Jun 16, 2024
2 parents 10f6319 + 027aafd commit cb34a86
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 1 deletion.
5 changes: 4 additions & 1 deletion app/Http/Controllers/AnimeController.php
Original file line number Diff line number Diff line change
Expand Up @@ -711,7 +711,7 @@ public function exportAnimeList(Request $request, AnimeListExportService $export

// Validation
$request->validate([
'export_type' => 'required|in:myanimelist,arigatou',
'export_type' => 'required|in:myanimelist,arigatou,myanimelistcss',
]);

$userId = Auth::id();
Expand All @@ -727,6 +727,9 @@ public function exportAnimeList(Request $request, AnimeListExportService $export
} elseif ($exportType === 'arigatou') {
$fileName = "{$fileNameBase}.json";
Storage::put('exports/'.$fileName, $result['output']);
} elseif ($exportType === 'myanimelistcss') {
$fileName = "{$fileNameBase}.css";
Storage::put('exports/'.$fileName, $result['output']);
} else {
return redirect()->back()->with('message', 'Export failed due to unknown file type.');
}
Expand Down
48 changes: 48 additions & 0 deletions app/Services/AnimeListExportService.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ public function export($fileType, $userId, $logger = null)
return $this->exportToMyAnimeList($userId, $logger);
} elseif ($fileType === 'arigatou') {
return $this->exportToArigatou($userId, $logger);
} elseif ($fileType === 'myanimelistcss') {
return $this->exportToMyAnimeListCss($userId, $logger);
} else {
// Unknown file type
return "";
Expand Down Expand Up @@ -115,4 +117,50 @@ private function exportToArigatou($userId, $logger = null)
$duration = microtime(true) - $startTime;
return ["total" => $total, "duration" => $duration, "output" => $formattedJson];
}

private function exportToMyAnimeListCss($userId, $logger = null)
{
$startTime = microtime(true);
$animeList = AnimeUser::with('anime')
->where('user_id', $userId)
->whereHas('watch_status', function ($query) {
$query->where('status', 'COMPLETED');
})
->where('sort_order', '>=', 1)
->orderBy('sort_order', 'ASC')
->get();
$total = count($animeList);
$cssOutput = "";
//These seem to be the min and max orders MAL allows, starting from -1000 and going up to 3000.
$minOrder = -1000;
$maxOrder = 3000;
$currentOrder = $minOrder;
foreach ($animeList as $animeUser) {
$source = collect(explode(', ', $animeUser->anime->sources))->first(function ($source) {
return str_contains($source, 'myanimelist.net/anime/');
});
if ($source) {
preg_match('/myanimelist\.net\/anime\/(\d+)/', $source, $matches);
$animeId = $matches[1] ?? null;
if ($animeId) {
//If we wanted to use just the provided sort order, from 1 to 3000, we could just use sort_order.
//$order = $animeUser->sort_order;
//Technically, by default the order is limited from 1 to 3000, maybe we want more anime total, so let's scale it from min to max for a total of 4000 anime.
$order = $currentOrder;
$cssOutput .= ".list-container .list-block .completed table.list-table > tbody.list-item:has(a[href*=\"anime/{$animeId}/\"]) {\n";
$cssOutput .= " visibility: unset;\n";
$cssOutput .= " height: unset;\n";
$cssOutput .= " order: {$order};\n";
$cssOutput .= "}\n";
$currentOrder++;
}
}
if ($currentOrder > $maxOrder) {
exit("You have exceeded the maximum total anime for MAL custom sorting, congratulations! This isn't very easily achievable.");
}
}

$duration = microtime(true) - $startTime;
return ["total" => $total, "duration" => $duration, "output" => $cssOutput];
}
}
1 change: 1 addition & 0 deletions resources/views/exportanimelist.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
<select class="shadow appearance-none border rounded py-2 px-8 text-gray-700 leading-tight focus:outline-none focus:shadow-outline" name="export_type" id="export_type">
<option value="myanimelist">MyAnimeList</option>
<option value="arigatou">ArigatouAnimeTracker</option>
<option value="myanimelistcss">MyAnimeList Custom CSS</option>
</select>
<button type="submit" class="p-2 bg-blue-500 hover:bg-blue-700 text-white rounded-md mt-2">Export</button>
</form>
Expand Down

0 comments on commit cb34a86

Please sign in to comment.