aboutsummaryrefslogtreecommitdiff
path: root/src/lib/Tools/Wrapped.svelte
blob: f8879814e1ab1c50526a4fc61c44dbb78ff93ea9 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
<script lang="ts">
	import userIdentity from '../../stores/userIdentity.js';
	import {
		userIdentity as getUserIdentity,
		type AniListAuthorisation
	} from '$lib/AniList/identity';
	import { onMount } from 'svelte';
	import { wrapped } from '$lib/AniList/wrapped.js';
	import { activityHistory, fillMissingDays } from '$lib/AniList/activity.js';
	import { Type, mediaListCollection, type Media } from '$lib/AniList/media.js';
	import anime from '../../stores/anime.js';
	import lastPruneTimes from '../../stores/lastPruneTimes.js';
	import manga from '../../stores/manga.js';
	import Error from '$lib/Error.svelte';
	import { domToBlob } from 'modern-screenshot';

	export let user: AniListAuthorisation;

	let currentUserIdentity = { name: '', id: -1 };
	let episodes = 0;
	let chapters = 0;
	let minutesWatched = 0;
	let animeList: Media[] | undefined = undefined;
	let mangaList: Media[] | undefined = undefined;
	let abbreviateTitles = true;
	let maxAbbreviateLength = 40;
	let transparency = false;
	let darkTheme = true;
	let watermark = false;
	let includeMusic = false;
	let includeSpecials = false;
	let includeRepeats = false;
	let width = 980;

	$: {
		includeMusic = includeMusic;
		includeSpecials = includeSpecials;
		includeRepeats = includeRepeats;

		update().then(() => {});
	}

	onMount(async () => {
		if (user !== undefined) {
			if ($userIdentity === '') {
				userIdentity.set(JSON.stringify(await getUserIdentity(user)));
			}

			currentUserIdentity = JSON.parse($userIdentity);
			currentUserIdentity.name = currentUserIdentity.name;
		} else {
			currentUserIdentity.id = -2;
		}

		await update();
	});

	const update = async () => {
		const currentYear = new Date(Date.now()).getFullYear();

		animeList = (
			await mediaListCollection(
				user,
				currentUserIdentity,
				Type.Anime,
				$anime,
				$lastPruneTimes.anime,
				true,
				true
			)
		)
			.filter(
				(item, index, self) =>
					self.findIndex((itemToCompare) => itemToCompare.id === item.id) === index &&
					(includeMusic ? true : item.format !== 'MUSIC') &&
					(includeRepeats ? true : item.mediaListEntry?.repeat === 0) &&
					(item.mediaListEntry?.startedAt.year === currentYear ||
						item.mediaListEntry?.completedAt.year === currentYear)
			)
			.sort((a, b) => {
				if (a.mediaListEntry?.score === undefined) {
					return 1;
				} else if (b.mediaListEntry?.score === undefined) {
					return -1;
				} else {
					return b.mediaListEntry?.score - a.mediaListEntry?.score;
				}
			});
		mangaList = (
			await mediaListCollection(
				user,
				currentUserIdentity,
				Type.Manga,
				$manga,
				$lastPruneTimes.manga,
				true,
				true
			)
		)
			.filter(
				(item, index, self) =>
					self.findIndex((itemToCompare) => itemToCompare.id === item.id) === index &&
					(includeRepeats ? true : item.mediaListEntry?.repeat === 0) &&
					(item.mediaListEntry?.startedAt.year === currentYear ||
						item.mediaListEntry?.completedAt.year === currentYear)
			)
			.sort((a, b) => {
				if (a.mediaListEntry?.score === undefined) {
					return 1;
				} else if (b.mediaListEntry?.score === undefined) {
					return -1;
				} else {
					return b.mediaListEntry?.score - a.mediaListEntry?.score;
				}
			});

		episodes = 0;
		minutesWatched = 0;
		chapters = 0;

		for (const media of animeList) {
			episodes += media.mediaListEntry?.progress || 0;
			minutesWatched += (media.mediaListEntry?.progress || 0) * media.duration || 0;
		}

		for (const media of mangaList) {
			chapters += media.mediaListEntry?.progress || 0;
		}
	};

	/* eslint-disable @typescript-eslint/no-explicit-any */
	// const year = (statistic: { startYears: any }) =>
	// 	statistic.startYears.find((y: { startYear: number }) => y.startYear === 2023);

	const screenshot = async (dark = false) => {
		let element = document.querySelector('.categories-grid') as HTMLElement;

		element.classList.add('invert');

		if (dark) {
			// element.classList.add('light-theme');
			darkTheme = false;
		}

		if (element !== null) {
			domToBlob(element, {
				style: {
					backgroundColor: transparency ? 'transparent' : dark ? '#0b1622' : '#edf1f5'
				},
				quality: 1,
				scale: 2,
				fetch: {
					requestInit: {
						mode: 'cors'
					},
					bypassingCache: true
				}
			}).then((blob) => {
				const downloadWrapper = document.createElement('a');
				const wrappedImageButton = document.getElementById(
					'wrapped-image-download'
				) as HTMLAnchorElement;
				const image = document.createElement('img');
				const object = (window.URL || window.webkitURL || window || {}).createObjectURL(blob);

				// downloadWrapper.download = `due_dot_moe_wrapped_${dark ? 'dark' : 'light'}.png`;
				downloadWrapper.href = object;
				downloadWrapper.target = '_blank';
				image.src = object;

				downloadWrapper.appendChild(image);

				if (wrappedImageButton !== null) {
					wrappedImageButton.href = object;
				}

				const wrappedFinal = document.getElementById('wrapped-final');

				if (wrappedFinal !== null) {
					wrappedFinal.innerHTML = '';
					wrappedFinal.appendChild(downloadWrapper);
				}

				downloadWrapper.click();
			});

			await new Promise((resolve) => setTimeout(resolve, 1000));

			element.classList.remove('invert');
			darkTheme = true;
			// element.classList.remove('light-theme');
		}
	};

	const abbreviate = (string: string, maxLength = 40, enabled = true) => {
		if (!enabled) {
			return string;
		}

		if (string.length <= maxLength) {
			return string;
		}

		return string.slice(0, maxLength - 3) + ' …';
	};
</script>

<div id="wrapped" style={`width: ${width}px`}>
	{#if currentUserIdentity.id === -2}
		Please log in to view this page.
	{:else if currentUserIdentity.id !== -1}
		{#await wrapped(user, currentUserIdentity)}
			Loading ...
		{:then wrapped}
			<div class:light-theme={darkTheme} class="categories-grid">
				<div class="grid-item image-grid avatar-grid">
					<a href={`https://anilist.co/user/${currentUserIdentity.name}`} target="_blank">
						<img src={wrapped.avatar.large} alt="User Avatar" />
					</a>
					<div>
						<div>
							<a href={`https://anilist.co/user/${currentUserIdentity.name}`} target="_blank">
								<b>
									{currentUserIdentity.name}
								</b>
							</a>
						</div>
						<div>
							Status Posts: {wrapped.activities.statusCount}
						</div>
						<div>
							Messages: {wrapped.activities.messageCount}
						</div>
						<div>
							Days Active: {#await activityHistory(currentUserIdentity)}
								Loading ...
							{:then activities}
								{#if activities === undefined}
									Loading ...
								{:else}
									{fillMissingDays(activities, true).filter((a) => a.amount !== 0).length}/365
								{/if}
							{/await}
						</div>
					</div>
				</div>
				<div class="category-grid pure-category">
					<div class="grid-item">
						<b>Anime</b>
					</div>
					<div class="grid-item">
						Time Watched: {((minutesWatched || 0) / 60 / 24).toFixed(2)} days
					</div>
					<div class="grid-item">
						Completed: {animeList?.length}
					</div>
					<div class="grid-item">Episodes: {episodes}</div>
				</div>
				<div class="category-grid pure-category">
					<div class="grid-item">
						<b>Manga</b>
					</div>
					<div class="grid-item">
						Time Read: {((chapters * 8.58) / 60 / 24).toFixed(2)} days
					</div>
					<div class="grid-item">
						Completed: {mangaList?.length}
					</div>
					<div class="grid-item">
						Chapters: {chapters}
					</div>
				</div>
				<div class="category-grid pure-category">
					<div class="grid-item image-grid">
						{#if animeList !== undefined}
							<a href={`https://anilist.co/anime/${animeList[0].id}`} target="_blank">
								<img
									src={animeList[0].coverImage.extraLarge}
									alt="Highest Rated Anime Cover"
									class="cover-image"
								/>
							</a>
							<div>
								<b>Highest Rated Anime</b>
								<ol>
									{#each animeList?.slice(0, 5) as anime}
										<li>
											<a href={`https://anilist.co/anime/${anime.id}`} target="_blank">
												{abbreviate(
													anime.title.english || anime.title.romaji || anime.title.native,
													maxAbbreviateLength,
													abbreviateTitles
												)}
											</a>
										</li>
									{/each}
								</ol>
							</div>
						{:else}
							Loading ...
						{/if}
					</div>
				</div>
				<div class="category-grid pure-category">
					<div class="grid-item image-grid">
						{#if mangaList !== undefined}
							<a href={`https://anilist.co/manga/${mangaList[0].id}`} target="_blank">
								<img
									src={mangaList[0].coverImage.extraLarge}
									alt="Highest Rated Manga Cover"
									class="cover-image"
								/>
							</a>
							<div>
								<b>Highest Rated Manga</b>
								<ol>
									{#each mangaList?.slice(0, 5) as manga}
										<li>
											<a href={`https://anilist.co/manga/${manga.id}`} target="_blank">
												{abbreviate(
													manga.title.english || manga.title.romaji || manga.title.native,
													maxAbbreviateLength,
													abbreviateTitles
												)}
											</a>
										</li>
									{/each}
								</ol>
							</div>
						{:else}
							Loading ...
						{/if}
					</div>
				</div>
				{#if watermark}
					<div class="category-grid pure-category" id="watermark">due.moe/wrapped</div>
				{/if}
			</div>

			<br />

			<p>
				Don't be alarmed! The background will appear as its inverse in the preview, but will be
				corrected in the final image. Media covers may not appear on mobile Apple devices.
			</p>

			<div id="options">
				<input type="checkbox" bind:checked={watermark} /> Enable watermark<br />
				<input type="checkbox" bind:checked={transparency} /> Enable background transparency<br />
				<input type="checkbox" bind:checked={includeMusic} /> Include music<br />
				<input type="checkbox" bind:checked={includeRepeats} /> Include rewatches & rereads<br />
				<input type="checkbox" bind:checked={includeSpecials} /> Include specials and OVAs<br />
				Width ({width} px): <input type="range" min="1" max="1920" bind:value={width} /><br />
				<input type="checkbox" bind:checked={abbreviateTitles} /> Fit long titles<br />
				{#if abbreviateTitles}
					Maximum title length <input type="range" min="4" bind:value={maxAbbreviateLength} /><br />
				{/if}
			</div>

			<br />

			<ul>
				<li>
					<a href={'#'} on:click={() => screenshot(true)}>Generate dark themed image</a>
				</li>
				<li>
					<a href={'#'} on:click={() => screenshot()}>Generate light themed image</a>
				</li>
				<li>
					<a href={'#'} target="_blank" id="wrapped-image-download">
						Download generated image (generate first)
					</a>
				</li>
			</ul>

			<br />

			<div id="wrapped-final" />
		{:catch}
			<Error />
		{/await}
	{:else}
		Loading ...
	{/if}
</div>

<style>
	@import url('http://fonts.googleapis.com/css?family=Roboto:300,400,500,700');
	@import url('https://fonts.googleapis.com/css?family=Overpass:400,600,700,800');

	.categories-grid {
		display: flex;
		flex-wrap: wrap;
		row-gap: 1.5em;
		column-gap: 1.5em;
		padding: 2%;
		justify-content: center;
		/* filter: invert(0) !important; */
		background-color: #0b1622;
		font-family: Roboto, -apple-system, BlinkMacSystemFont, Segoe UI, Oxygen, Ubuntu, Cantarell,
			Fira Sans, Droid Sans, Helvetica Neue, sans-serif;
	}

	.categories-grid b {
		font-family: Overpass, -apple-system, BlinkMacSystemFont, Segoe UI, Oxygen, Ubuntu, Cantarell,
			Fira Sans, Droid Sans, Helvetica Neue, sans-serif !important;
		font-weight: 600 !important;
	}

	.category-grid,
	.image-grid {
		background-color: #151f2e;
		border-radius: 4px;
		color: rgb(159, 173, 189) !important;
	}

	.pure-category,
	.avatar-grid {
		padding: 1.5%;
	}

	.category-grid {
		display: grid;
	}

	.image-grid {
		display: flex;
		column-gap: 1em;
		flex-wrap: wrap;
	}

	.image-grid img {
		height: 6em;
		border-radius: 3px;
	}

	.cover-image {
		height: 8.75em !important;
	}

	.categories-grid a {
		text-decoration: none;
		color: unset;
	}

	.light-theme .categories-grid {
		background-color: #edf1f5 !important;
	}

	.light-theme .category-grid {
		background-color: #fafafa !important;
		color: rgb(92, 114, 138) !important;
	}

	.light-theme .image-grid {
		background-color: #fafafa !important;
		color: rgb(92, 114, 138) !important;
	}

	:global(.invert *) {
		filter: invert(0) !important;
	}

	ol {
		margin: 0 !important;
	}

	#watermark {
		color: rgb(61, 180, 242) !important;
	}
</style>