From 149fc48a00729307c1fedef295120808175c663d Mon Sep 17 00:00:00 2001 From: Zechariah Date: Tue, 28 Jun 2022 00:24:19 +0800 Subject: [PATCH] =?UTF-8?q?=E2=9C=A8=20add=20back=20year=20to=20album=20de?= =?UTF-8?q?tailed?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- docs/references/interfaces/AlbumDetailed.md | 2 ++ docs/references/interfaces/AlbumFull.md | 2 ++ src/index.ts | 1 + src/interfaces.ts | 4 +++- src/parsers/AlbumParser.ts | 10 ++++++++++ 5 files changed, 18 insertions(+), 1 deletion(-) diff --git a/docs/references/interfaces/AlbumDetailed.md b/docs/references/interfaces/AlbumDetailed.md index 8f01528..dc6b265 100644 --- a/docs/references/interfaces/AlbumDetailed.md +++ b/docs/references/interfaces/AlbumDetailed.md @@ -9,6 +9,7 @@ | `playlistId` | `string` | Playlist ID for Album | | `name` | `string` | Name | | `artists` | [ArtistBasic](./ArtistBasic.html)`[]` | Creators of the Album | +| `year` | `number \| null` | Year of release | | `thumbnails` | [ThumbnailFull](./ThumbnailFull.html)`[]` | Thumbnails | ## TypeScript Source Code @@ -20,6 +21,7 @@ interface AlbumDetailed { playlistId: string name: string artists: ArtistBasic[] + year: number | null thumbnails: ThumbnailFull[] } ``` diff --git a/docs/references/interfaces/AlbumFull.md b/docs/references/interfaces/AlbumFull.md index daf686d..947f9f9 100644 --- a/docs/references/interfaces/AlbumFull.md +++ b/docs/references/interfaces/AlbumFull.md @@ -9,6 +9,7 @@ | `playlistId` | `string` | Playlist ID for Album | | `name` | `string` | Name | | `artists` | [ArtistBasic](./ArtistBasic.html)`[]` | Creators of the Album | +| `year` | `number \| null` | Year of release | | `thumbnails` | [ThumbnailFull](./ThumbnailFull.html)`[]` | Thumbnails | | `description` | `string` | Description | | `songs` | [SongDetailed](./SongDetailed.html)`[]` | Songs in the Album | @@ -22,6 +23,7 @@ interface AlbumFull { playlistId: string name: string artists: ArtistBasic[] + year: number | null thumbnails: ThumbnailFull[] description: string songs: SongDetailed[] diff --git a/src/index.ts b/src/index.ts index 40b367a..7b899ee 100644 --- a/src/index.ts +++ b/src/index.ts @@ -65,6 +65,7 @@ export interface AlbumDetailed extends AlbumBasic { type: "ALBUM" playlistId: string artists: ArtistBasic[] + year: number | null thumbnails: ThumbnailFull[] } diff --git a/src/interfaces.ts b/src/interfaces.ts index a4e666e..5ec484a 100644 --- a/src/interfaces.ts +++ b/src/interfaces.ts @@ -1,4 +1,4 @@ -import { BOOLEAN, LIST, NUMBER, OBJECT, STRING } from "validate-any" +import { BOOLEAN, LIST, NULL, NUMBER, OBJECT, OR, STRING } from "validate-any" import ObjectValidator from "validate-any/dist/validators/ObjectValidator" import { @@ -54,6 +54,7 @@ export const ALBUM_DETAILED: ObjectValidator = OBJECT({ playlistId: STRING(), name: STRING(), artists: LIST(ARTIST_BASIC), + year: OR(NUMBER(), NULL()), thumbnails: LIST(THUMBNAIL_FULL) }) @@ -108,6 +109,7 @@ export const ALBUM_FULL: ObjectValidator = OBJECT({ playlistId: STRING(), name: STRING(), artists: LIST(ARTIST_BASIC), + year: OR(NUMBER(), NULL()), thumbnails: LIST(THUMBNAIL_FULL), description: STRING(), songs: LIST(SONG_DETAILED) diff --git a/src/parsers/AlbumParser.ts b/src/parsers/AlbumParser.ts index dc56a59..fcef4c5 100644 --- a/src/parsers/AlbumParser.ts +++ b/src/parsers/AlbumParser.ts @@ -25,6 +25,9 @@ export default class AlbumParser { ...albumBasic, playlistId: traverseString(data, "buttonRenderer", "playlistId")(), artists, + year: AlbumParser.processYear( + traverseString(data, "header", "subtitle", "text")(-1) + ), thumbnails, description: traverseString(data, "description", "text")(), songs: traverseList(data, "musicResponsiveListItemRenderer").map(item => @@ -49,6 +52,7 @@ export default class AlbumParser { artistId: traverseString(run, "browseId")(), name: traverseString(run, "text")() })), + year: AlbumParser.processYear(traverseString(flexColumns[1], "runs", "text")(-1)), name: traverseString(flexColumns[0], "runs", "text")(), thumbnails: traverseList(item, "thumbnails") }, @@ -64,6 +68,7 @@ export default class AlbumParser { playlistId: traverseString(item, "thumbnailOverlay", "playlistId")(), name: traverseString(item, "title", "text")(), artists: [artistBasic], + year: AlbumParser.processYear(traverseString(item, "subtitle", "text")(-1)), thumbnails: traverseList(item, "thumbnails") }, ALBUM_DETAILED @@ -78,9 +83,14 @@ export default class AlbumParser { playlistId: traverseString(item, "musicPlayButtonRenderer", "playlistId")(), name: traverseString(item, "title", "text")(), artists: [artistBasic], + year: AlbumParser.processYear(traverseString(item, "subtitle", "text")(-1)), thumbnails: traverseList(item, "thumbnails") }, ALBUM_DETAILED ) } + + private static processYear(year: string) { + return year.match(/^\d{4}$/) ? +year : null + } }