mirror of
https://github.com/apricote/Listory.git
synced 2026-01-13 21:21:02 +00:00
feat: add top-artists report
This commit is contained in:
parent
6a6ba493f6
commit
6fc10c40ca
18 changed files with 345 additions and 30 deletions
|
|
@ -1,11 +1,13 @@
|
|||
import { formatISO, parseISO } from "date-fns";
|
||||
import { qs } from "../util/queryString";
|
||||
import { Listen } from "./entities/listen";
|
||||
import { ListenReportItem } from "./entities/listen-report-item";
|
||||
import { ListenReportOptions } from "./entities/listen-report-options";
|
||||
import { Pagination } from "./entities/pagination";
|
||||
import { PaginationOptions } from "./entities/pagination-options";
|
||||
import { TopArtistsItem } from "./entities/top-artists-item";
|
||||
import { TopArtistsOptions } from "./entities/top-artists-options";
|
||||
import { User } from "./entities/user";
|
||||
import { qs } from "../util/queryString";
|
||||
|
||||
export class UnauthenticatedError extends Error {}
|
||||
|
||||
|
|
@ -99,10 +101,42 @@ export const getListensReport = async (
|
|||
throw new UnauthenticatedError(`No token or token expired`);
|
||||
}
|
||||
default: {
|
||||
throw new Error(`Unable to getRecentListens: ${res.status}`);
|
||||
throw new Error(`Unable to getListensReport: ${res.status}`);
|
||||
}
|
||||
}
|
||||
|
||||
const rawItems: { count: number; date: string }[] = (await res.json()).items;
|
||||
return rawItems.map(({ count, date }) => ({ count, date: parseISO(date) }));
|
||||
};
|
||||
|
||||
export const getTopArtists = async (
|
||||
options: TopArtistsOptions
|
||||
): Promise<TopArtistsItem[]> => {
|
||||
const { timePreset, customTimeStart, customTimeEnd } = options;
|
||||
|
||||
const res = await fetch(
|
||||
`/api/v1/reports/top-artists?${qs({
|
||||
timePreset,
|
||||
customTimeStart: formatISO(customTimeStart),
|
||||
customTimeEnd: formatISO(customTimeEnd),
|
||||
})}`,
|
||||
{
|
||||
headers: getDefaultHeaders(),
|
||||
}
|
||||
);
|
||||
|
||||
switch (res.status) {
|
||||
case 200: {
|
||||
break;
|
||||
}
|
||||
case 401: {
|
||||
throw new UnauthenticatedError(`No token or token expired`);
|
||||
}
|
||||
default: {
|
||||
throw new Error(`Unable to getTopArtists: ${res.status}`);
|
||||
}
|
||||
}
|
||||
|
||||
const items: TopArtistsItem[] = (await res.json()).items;
|
||||
return items;
|
||||
};
|
||||
|
|
|
|||
7
frontend/src/api/entities/album.ts
Normal file
7
frontend/src/api/entities/album.ts
Normal file
|
|
@ -0,0 +1,7 @@
|
|||
import { SpotifyInfo } from "./spotify-info";
|
||||
|
||||
export interface Album {
|
||||
id: string;
|
||||
name: string;
|
||||
spotify?: SpotifyInfo;
|
||||
}
|
||||
7
frontend/src/api/entities/artist.ts
Normal file
7
frontend/src/api/entities/artist.ts
Normal file
|
|
@ -0,0 +1,7 @@
|
|||
import { SpotifyInfo } from "./spotify-info";
|
||||
|
||||
export interface Artist {
|
||||
id: string;
|
||||
name: string;
|
||||
spotify?: SpotifyInfo;
|
||||
}
|
||||
|
|
@ -1,32 +1,7 @@
|
|||
import { Track } from "./track";
|
||||
|
||||
export interface Listen {
|
||||
id: string;
|
||||
playedAt: string;
|
||||
track: Track;
|
||||
}
|
||||
|
||||
interface Track {
|
||||
id: string;
|
||||
name: string;
|
||||
album: Album;
|
||||
artists: Artist[];
|
||||
spotify?: SpotifyInfo;
|
||||
}
|
||||
|
||||
interface Album {
|
||||
id: string;
|
||||
name: string;
|
||||
spotify?: SpotifyInfo;
|
||||
}
|
||||
|
||||
interface Artist {
|
||||
id: string;
|
||||
name: string;
|
||||
spotify?: SpotifyInfo;
|
||||
}
|
||||
|
||||
interface SpotifyInfo {
|
||||
id: string;
|
||||
uri: string;
|
||||
type: string;
|
||||
href: string;
|
||||
}
|
||||
|
|
|
|||
6
frontend/src/api/entities/spotify-info.ts
Normal file
6
frontend/src/api/entities/spotify-info.ts
Normal file
|
|
@ -0,0 +1,6 @@
|
|||
export interface SpotifyInfo {
|
||||
id: string;
|
||||
uri: string;
|
||||
type: string;
|
||||
href: string;
|
||||
}
|
||||
9
frontend/src/api/entities/time-preset.enum.ts
Normal file
9
frontend/src/api/entities/time-preset.enum.ts
Normal file
|
|
@ -0,0 +1,9 @@
|
|||
export enum TimePreset {
|
||||
LAST_7_DAYS = "last_7_days",
|
||||
LAST_30_DAYS = "last_30_days",
|
||||
LAST_90_DAYS = "last_90_days",
|
||||
LAST_180_DAYS = "last_180_days",
|
||||
LAST_365_DAYS = "last_365_days",
|
||||
ALL_TIME = "all_time",
|
||||
CUSTOM = "custom",
|
||||
}
|
||||
6
frontend/src/api/entities/top-artists-item.ts
Normal file
6
frontend/src/api/entities/top-artists-item.ts
Normal file
|
|
@ -0,0 +1,6 @@
|
|||
import { Artist } from "./artist";
|
||||
|
||||
export interface TopArtistsItem {
|
||||
artist: Artist;
|
||||
count: number;
|
||||
}
|
||||
7
frontend/src/api/entities/top-artists-options.ts
Normal file
7
frontend/src/api/entities/top-artists-options.ts
Normal file
|
|
@ -0,0 +1,7 @@
|
|||
import { TimePreset } from "./time-preset.enum";
|
||||
|
||||
export interface TopArtistsOptions {
|
||||
timePreset: TimePreset;
|
||||
customTimeStart: Date;
|
||||
customTimeEnd: Date;
|
||||
}
|
||||
11
frontend/src/api/entities/track.ts
Normal file
11
frontend/src/api/entities/track.ts
Normal file
|
|
@ -0,0 +1,11 @@
|
|||
import { Album } from "./album";
|
||||
import { Artist } from "./artist";
|
||||
import { SpotifyInfo } from "./spotify-info";
|
||||
|
||||
export interface Track {
|
||||
id: string;
|
||||
name: string;
|
||||
album: Album;
|
||||
artists: Artist[];
|
||||
spotify?: SpotifyInfo;
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue