Skip to content

Commit

Permalink
chore: add types to some api and gw calls
Browse files Browse the repository at this point in the history
  • Loading branch information
bambanah committed Sep 2, 2024
1 parent 5c5ac47 commit 5fc3dd4
Show file tree
Hide file tree
Showing 4 changed files with 136 additions and 60 deletions.
27 changes: 5 additions & 22 deletions deemix/src/itemgen.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,21 +9,14 @@ const { map_user_playlist, map_track, map_album } = require("deezer-js").utils;
import { each } from "async";
import { Deezer } from "deezer-js";
import { Album } from "./types/Album";
import { APIAlbum, APITrack } from "deezer-js/src/api";

export async function generateTrackItem(
dz: Deezer,
id: string,
bitrate: number,
trackAPI?: {
id: any;
title: any;
album: { cover_small: string };
md5_image: any;
track_token: any;
artist: { name: any };
explicit_lyrics: any;
},
albumAPI?: any
trackAPI?: APITrack,
albumAPI?: APIAlbum
) {
// Get essential track info
if (!trackAPI) {
Expand Down Expand Up @@ -76,22 +69,12 @@ export async function generateTrackItem(

export async function generateAlbumItem(
dz: Deezer,
id: string | any[],
id: string,
bitrate: number,
rootArtist?: { id: any; name: any; picture_small: any }
) {
// Get essential album info
let albumAPI: {
id: any;
root_artist: any;
nb_tracks: number;
tracks: { data: string | any[] };
cover_small: string;
md5_image: any;
title: any;
artist: { name: any };
explicit_lyrics: any;
};
let albumAPI: APIAlbum;
if (String(id).startsWith("upc")) {
const upcs = [id.slice(4).toString()];
upcs.push(parseInt(upcs[0], 10).toString()); // Try UPC without leading zeros as well
Expand Down
66 changes: 37 additions & 29 deletions deemix/src/types/Track.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { Deezer, TrackFormats, utils } from "deezer-js";
import { AlbumDoesntExists, NoDataToParse } from "../errors";
import { FeaturesOption } from "../settings";
import { FeaturesOption, Settings } from "../settings";
import {
andCommaConcat,
changeCase,
Expand All @@ -15,6 +15,7 @@ import { VARIOUS_ARTISTS } from "./index";
import { Lyrics } from "./Lyrics";
import { Picture } from "./Picture";
import { Playlist } from "./Playlist";
import { APITrack } from "deezer-js/src/api";
const { map_track, map_album } = utils;

export const formatsName = {
Expand All @@ -41,7 +42,7 @@ class Track {
filesizes: Record<string, any>;
local: boolean;
mainArtist: Artist | null;
artist: { Main: any[] };
artist: { Main: any[]; Featured?: any[] };
artists: any[];
album: Album | null;
trackNumber: string;
Expand All @@ -54,7 +55,7 @@ class Track {
explicit: boolean;
ISRC: string;
replayGain: string;
playlist: null;
playlist: Playlist | null;
position: null;
searched: boolean;
bitrate: keyof typeof formatsName;
Expand All @@ -63,7 +64,7 @@ class Track {
mainArtistsString: string;
featArtistsString: string;
fullArtistsString: string;
urls: Record<(typeof formatsName)[keyof typeof formatsName], string>;
urls: Partial<Record<(typeof formatsName)[keyof typeof formatsName], string>>;
downloadURL: string;
rank: any;
artistString: any;
Expand Down Expand Up @@ -106,7 +107,7 @@ class Track {
this.urls = {};
}

parseEssentialData(trackAPI) {
parseEssentialData(trackAPI: APITrack) {
this.id = String(trackAPI.id);
this.duration = trackAPI.duration;
this.trackToken = trackAPI.track_token;
Expand All @@ -120,51 +121,58 @@ class Track {
this.urls = {};
}

async parseData(dz: Deezer, id, trackAPI, albumAPI, playlistAPI) {
async parseData(
dz: Deezer,
id,
existingTrack?: DeezerTrack,
albumAPI,
playlistAPI
) {
if (id) {
let trackAPI_new = await dz.gw.get_track_with_fallback(id);
trackAPI_new = map_track(trackAPI_new);
if (!trackAPI) trackAPI = {};
trackAPI = { ...trackAPI, ...trackAPI_new };
} else if (!trackAPI) {
const gwTrack = await dz.gw.get_track_with_fallback(id);
const newTrack = map_track(gwTrack);

if (!existingTrack) existingTrack = {};
existingTrack = { ...existingTrack, ...newTrack };
} else if (!existingTrack) {
throw new NoDataToParse();
}

this.parseEssentialData(trackAPI);
this.parseEssentialData(existingTrack);

// only public api has bpm
if (!trackAPI.bpm && !this.local) {
if (!existingTrack.bpm && !this.local) {
try {
const trackAPI_new = await dz.api.get_track(trackAPI.id);
trackAPI_new.release_date = trackAPI.release_date;
trackAPI = { ...trackAPI, ...trackAPI_new };
const trackAPI_new = await dz.api.get_track(existingTrack.id);
trackAPI_new.release_date = existingTrack.release_date;
existingTrack = { ...existingTrack, ...trackAPI_new };
} catch {
/* empty */
}
}

if (this.local) {
this.parseLocalTrackData(trackAPI);
this.parseLocalTrackData(existingTrack);
} else {
this.parseTrack(trackAPI);
this.parseTrack(existingTrack);

// Get Lyrics Data
if (!trackAPI.lyrics && this.lyrics.id !== "0") {
if (!existingTrack.lyrics && this.lyrics.id !== "0") {
try {
trackAPI.lyrics = await dz.gw.get_track_lyrics(this.id);
existingTrack.lyrics = await dz.gw.get_track_lyrics(this.id);
} catch {
this.lyrics.id = "0";
}
}
if (this.lyrics.id !== "0") {
this.lyrics.parseLyrics(trackAPI.lyrics);
this.lyrics.parseLyrics(existingTrack.lyrics);
}

// Parse Album Data
this.album = new Album(
trackAPI.album.id,
trackAPI.album.title,
trackAPI.album.md5_origin || ""
existingTrack.album.id,
existingTrack.album.title,
existingTrack.album.md5_origin || ""
);

// Get album Data
Expand Down Expand Up @@ -206,8 +214,8 @@ class Track {

// Fill missing data
if (this.album.date && !this.date) this.date = this.album.date;
if (trackAPI.genres) {
trackAPI.genres.forEach((genre) => {
if (existingTrack.genres) {
existingTrack.genres.forEach((genre) => {
if (!this.album.genre.includes(genre)) this.album.genre.push(genre);
});
}
Expand All @@ -221,7 +229,7 @@ class Track {
if (!this.artist.Main.length) {
this.artist.Main = [this.mainArtist.name];
}
this.position = trackAPI.position;
this.position = existingTrack.position;

if (playlistAPI) {
this.playlist = new Playlist(playlistAPI);
Expand Down Expand Up @@ -339,7 +347,7 @@ class Track {
}
}

async checkAndRenewTrackToken(dz) {
async checkAndRenewTrackToken(dz: Deezer) {
const now = new Date();
const expiration = new Date(this.trackTokenExpiration * 1000);
if (now > expiration) {
Expand All @@ -349,7 +357,7 @@ class Track {
}
}

applySettings(settings) {
applySettings(settings: Settings) {
// Check if should save the playlist as a compilation
if (settings.tags.savePlaylistAsCompilation && this.playlist) {
this.trackNumber = this.position;
Expand Down
82 changes: 77 additions & 5 deletions deezer-js/src/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,78 @@ export const SearchOrder = {
DURATION_DESC: "DURATION_DESC",
};

export interface APIArtist {
id: number;
name: string;
link: string;
share: string;
picture: string;
picture_small: string;
picture_medium: string;
picture_big: string;
picture_xl: string;
nb_album: number;
nb_fan: number;
radio: boolean;
tracklist: string;
role: string;
}

export interface APIAlbum {
id: string;
title: string;
link: string;
cover: string;
cover_small: string;
cover_medium: string;
cover_big: string;
cover_xl: string;
release_date: string; // Assuming the date is in string format (e.g., "YYYY-MM-DD")
}

export interface APITrack {
id: number;
readable: boolean;
title: string;
title_short: string;
title_version: string;
unseen: boolean;
isrc: string;
link: string;
share: string;
duration: number;
track_position: number;
disk_number: number;
rank: number;
release_date: string; // Assuming the date is in string format (e.g., "YYYY-MM-DD")
explicit_lyrics: boolean;
explicit_content_lyrics: number;
explicit_content_cover: number;
preview: string;
bpm: number;
gain: number;
available_countries: string[]; // List of countries as strings
alternative?: APITrack; // Assuming alternative is of type Track
contributors: APIContributor[]; // Assuming Contributor is an object
md5_image: string;
track_token: string;
artist: APIArtist;
album: APIAlbum;
}

export interface APIContributor {
id: number;
name: string;
link: string;
share: string;
picture: string;
picture_small: string;
picture_medium: string;
picture_big: string;
picture_xl: string;
role: string;
}

type APIArgs = Record<string | number, string | number>;

export class API {
Expand All @@ -41,7 +113,7 @@ export class API {
this.access_token = null;
}

async api_call(method: string, args: APIArgs = {}) {
async api_call(method: string, args: APIArgs = {}): Promise<unknown> {
if (this.access_token) args["access_token"] = this.access_token;

let result_json;
Expand Down Expand Up @@ -127,8 +199,8 @@ export class API {
return result_json;
}

get_album(album_id) {
return this.api_call(`album/${album_id}`);
get_album(album_id: string): Promise<APIAlbum> {
return this.api_call(`album/${album_id}`) as Promise<APIAlbum>;
}

get_album_by_UPC(upc) {
Expand Down Expand Up @@ -423,8 +495,8 @@ export class API {
return this.api_call("search/user", args);
}

get_track(song_id) {
return this.api_call(`track/${song_id}`);
get_track(song_id: string): Promise<APITrack> {
return this.api_call(`track/${song_id}`) as Promise<APITrack>;
}

get_track_by_ISRC(isrc) {
Expand Down
21 changes: 17 additions & 4 deletions deezer-js/src/gw.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,19 @@ export const PlaylistStatus = {
COLLABORATIVE: 2,
};

export interface GWTrack {
SNG_ID: number;
SNG_TITLE: string;
DURATION: number;
MD5_ORIGIN: number;
MEDIA_VERSION: number;
FILESIZE: number;
ALB_TITLE: string;
ALB_PICTURE: string;
ART_ID: number;
ART_NAME: string;
}

export const EMPTY_TRACK_OBJ = {
SNG_ID: 0,
SNG_TITLE: "",
Expand All @@ -26,7 +39,7 @@ export const EMPTY_TRACK_OBJ = {
ALB_PICTURE: "",
ART_ID: 0,
ART_NAME: "",
};
} satisfies GWTrack;

export class GW {
http_headers: any;
Expand All @@ -39,7 +52,7 @@ export class GW {
this.api_token = null;
}

async api_call(method: string, args?: any, params?: any) {
async api_call(method: string, args?: any, params?: any): Promise<any> {
if (typeof args === undefined) args = {};
if (typeof params === undefined) params = {};
if (!this.api_token && method !== "deezer.getUserData")
Expand Down Expand Up @@ -132,7 +145,7 @@ export class GW {
return this.api_call("deezer.getChildAccounts");
}

get_track(sng_id) {
get_track(sng_id): Promise<GWTrack> {
return this.api_call("song.getData", { SNG_ID: sng_id });
}

Expand Down Expand Up @@ -510,7 +523,7 @@ export class GW {
if (user_data.USER.USER_ID === user_id)
return this.get_my_favorite_tracks(options);
const limit = options.limit || 25;
let data = this.get_user_profile_page(user_id, "loved", { limit });
let data = await this.get_user_profile_page(user_id, "loved", { limit });
data = data.TAB.loved.data;
const result = [];
data.forEach((track) => {
Expand Down

0 comments on commit 5fc3dd4

Please sign in to comment.