Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: skip already downloaded songs extremely quickly #54

Merged
merged 1 commit into from
Sep 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
473 changes: 105 additions & 368 deletions deemix/src/downloader.ts

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions deemix/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { Deezer } from "deezer-js";
import got from "got";
import downloader from "./downloader";
import { Downloader } from "./downloader";
import { LinkNotRecognized, LinkNotSupported } from "./errors";
import {
generateAlbumItem,
Expand Down Expand Up @@ -112,4 +112,4 @@ export * as types from "./types";
export * as utils from "./utils";

// Exporting the organized objects
export { downloader, generateDownloadObject, itemgen, parseLink };
export { Downloader, generateDownloadObject, itemgen, parseLink };
5 changes: 3 additions & 2 deletions deemix/src/types/Album.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { APIAlbum } from "deezer-js/src/api";
import { removeDuplicateArtists, removeFeatures } from "../utils";
import { Artist } from "./Artist";
import { CustomDate } from "./CustomDate";
Expand Down Expand Up @@ -59,7 +60,7 @@ export class Album {
this.isPlaylist = false;
}

parseAlbum(albumAPI) {
parseAlbum(albumAPI: APIAlbum) {
this.title = albumAPI.title;

// Getting artist image ID
Expand All @@ -85,7 +86,7 @@ export class Album {
}

albumAPI.contributors.forEach((artist) => {
const isVariousArtists = String(artist.id) === VARIOUS_ARTISTS;
const isVariousArtists = artist.id === VARIOUS_ARTISTS;
const isMainArtist = artist.role === "Main";

if (isVariousArtists) {
Expand Down
6 changes: 3 additions & 3 deletions deemix/src/types/Artist.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,14 @@ import { Picture } from "./Picture";
import { VARIOUS_ARTISTS } from "./index";

export class Artist {
id: string;
id: number;
name: string;
pic: Picture;
role: string;
save: boolean;

constructor(art_id = "0", name = "", role = "", pic_md5 = "") {
this.id = String(art_id);
constructor(art_id: number = 0, name = "", role = "", pic_md5 = "") {
this.id = art_id;
this.name = name;
this.pic = new Picture(pic_md5, "artist");
this.role = role;
Expand Down
2 changes: 1 addition & 1 deletion deemix/src/types/DownloadObjects.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import BasePlugin from "@/plugins/base";
export class IDownloadObject {
type: any;
id: any;
bitrate: any;
bitrate: string;
title: any;
artist: any;
cover: any;
Expand Down
54 changes: 28 additions & 26 deletions deemix/src/types/Track.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,12 @@ import { VARIOUS_ARTISTS } from "./index";
import { Lyrics } from "./Lyrics";
import { Picture } from "./Picture";
import { Playlist } from "./Playlist";
import { APITrack } from "deezer-js/src/api";
import {
APIAlbum,
APIPlaylist,
APITrack,
EnrichedAPITrack,
} from "deezer-js/src/api";
const { map_track, map_album } = utils;

export const formatsName = {
Expand All @@ -32,21 +37,21 @@ export const formatsName = {
class Track {
id: string;
title: string;
MD5: string;
mediaVersion: string;
MD5?: string;
mediaVersion?: number;
trackToken: string;
trackTokenExpiration: number;
trackTokenExpiration?: string;
duration: number;
fallbackID: string;
fallbackID: number;
albumsFallback: any[];
filesizes: Record<string, any>;
local: boolean;
mainArtist: Artist | null;
artist: { Main: any[]; Featured?: any[] };
artists: any[];
album: Album | null;
trackNumber: string;
discNumber: string;
trackNumber: number;
discNumber: number;
date: CustomDate;
lyrics: Lyrics | null;
bpm: number;
Expand All @@ -73,20 +78,18 @@ class Track {
this.id = "0";
this.title = "";
this.MD5 = "";
this.mediaVersion = "";
this.trackToken = "";
this.trackTokenExpiration = 0;
this.duration = 0;
this.fallbackID = "0";
this.fallbackID = 0;
this.albumsFallback = [];
this.filesizes = {};
this.local = false;
this.mainArtist = null;
this.artist = { Main: [] };
this.artists = [];
this.album = null;
this.trackNumber = "0";
this.discNumber = "0";
this.trackNumber = 0;
this.discNumber = 0;
this.date = new CustomDate();
this.lyrics = null;
this.bpm = 0;
Expand All @@ -107,39 +110,38 @@ class Track {
this.urls = {};
}

parseEssentialData(trackAPI: APITrack) {
parseEssentialData(trackAPI: EnrichedAPITrack) {
this.id = String(trackAPI.id);
this.duration = trackAPI.duration;
this.trackToken = trackAPI.track_token;
this.trackTokenExpiration = trackAPI.track_token_expire;
this.MD5 = trackAPI.md5_origin;
this.mediaVersion = trackAPI.media_version;
this.filesizes = trackAPI.filesizes;
this.fallbackID = "0";
if (trackAPI.fallback_id) this.fallbackID = trackAPI.fallback_id;
this.fallbackID = trackAPI.fallback_id ?? 0;
this.local = parseInt(this.id) < 0;
this.urls = {};
}

async parseData(
dz: Deezer,
id,
existingTrack?: DeezerTrack,
albumAPI,
playlistAPI
existingTrack?: APITrack,
albumAPI: APIAlbum,
playlistAPI: APIPlaylist,
refetch: boolean = true
) {
if (id) {
if (id && refetch) {
const gwTrack = await dz.gw.get_track_with_fallback(id);
const newTrack = map_track(gwTrack);

if (!existingTrack) existingTrack = {};
this.parseEssentialData(newTrack);

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

this.parseEssentialData(existingTrack);

// only public api has bpm
if (!existingTrack.bpm && !this.local) {
try {
Expand Down Expand Up @@ -256,7 +258,7 @@ class Track {
this.album.mainArtist = this.mainArtist;
}

parseTrack(trackAPI: any) {
parseTrack(trackAPI: APITrack) {
this.title = trackAPI.title;

this.discNumber = trackAPI.disk_number;
Expand All @@ -266,7 +268,7 @@ class Track {
this.replayGain = generateReplayGainString(trackAPI.gain);
this.ISRC = trackAPI.isrc;
this.trackNumber = trackAPI.track_position;
this.contributors = trackAPI.song_contributors;
this.contributors = trackAPI.contributors;
this.rank = trackAPI.rank;
this.bpm = trackAPI.bpm;

Expand All @@ -286,8 +288,8 @@ class Track {
this.date.fixDayMonth();
}

trackAPI.contributors.forEach((artist: any) => {
const isVariousArtists = String(artist.id) === VARIOUS_ARTISTS;
trackAPI.contributors?.forEach((artist: any) => {
const isVariousArtists = artist.id === VARIOUS_ARTISTS;
const isMainArtist = artist.role === "Main";

if (trackAPI.contributors.length > 1 && isVariousArtists) return;
Expand Down
2 changes: 1 addition & 1 deletion deemix/src/types/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
export const VARIOUS_ARTISTS = "5080";
export const VARIOUS_ARTISTS = 5080;

export * from "./Album";
export * from "./Artist";
Expand Down
95 changes: 95 additions & 0 deletions deemix/src/utils/downloadImage.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
import { TrackFormats, utils } from "deezer-js";
import {
createWriteStream,
existsSync,
mkdirSync,
readFileSync,
unlinkSync,
} from "fs";
import { HTTPError, ReadError, TimeoutError, default as got } from "got";
import { tmpdir } from "os";
import { OverwriteOption } from "../settings";
import { USER_AGENT_HEADER, pipeline } from "../utils";

const TEMPDIR = tmpdir() + "/deemix-imgs";
mkdirSync(TEMPDIR, { recursive: true });

export async function downloadImage(
url: string,
path: string,
overwrite = OverwriteOption.DONT_OVERWRITE
) {
if (
existsSync(path) &&
![
OverwriteOption.OVERWRITE,
OverwriteOption.ONLY_TAGS,
OverwriteOption.KEEP_BOTH,
].includes(overwrite)
) {
const file = readFileSync(path);
if (file.length !== 0) return path;
unlinkSync(path);
}
let timeout: NodeJS.Timeout | null = null;
let error = "";

const downloadStream = got
.stream(url, {
headers: { "User-Agent": USER_AGENT_HEADER },
https: { rejectUnauthorized: false },
})
.on("data", function () {
clearTimeout(timeout);
timeout = setTimeout(() => {
error = "DownloadTimeout";
downloadStream.destroy();
}, 5000);
});
const fileWriterStream = createWriteStream(path);

timeout = setTimeout(() => {
error = "DownloadTimeout";
downloadStream.destroy();
}, 5000);

try {
await pipeline(downloadStream, fileWriterStream);
} catch (e) {
unlinkSync(path);
if (e instanceof HTTPError) {
if (url.includes("images.dzcdn.net")) {
const urlBase = url.slice(0, url.lastIndexOf("/") + 1);
const pictureURL = url.slice(urlBase.length);
const pictureSize = parseInt(
pictureURL.slice(0, pictureURL.indexOf("x"))
);
if (pictureSize > 1200) {
return downloadImage(
urlBase +
pictureURL.replace(`${pictureSize}x${pictureSize}`, "1200x1200"),
path,
overwrite
);
}
}
return null;
}
if (
e instanceof ReadError ||
e instanceof TimeoutError ||
[
"ESOCKETTIMEDOUT",
"ERR_STREAM_PREMATURE_CLOSE",
"ETIMEDOUT",
"ECONNRESET",
].includes(e.code) ||
(downloadStream.destroyed && error === "DownloadTimeout")
) {
return downloadImage(url, path, overwrite);
}
console.trace(e);
throw e;
}
return path;
}
Loading
Loading