Skip to content

Commit

Permalink
feat: downloader options extended with category
Browse files Browse the repository at this point in the history
baseURL changed to be nullable and this URL will be replaced by url generated using category

Signed-off-by: Kaan Yagci <[email protected]>
  • Loading branch information
kaanyagci committed Sep 30, 2023
1 parent f4f556d commit f7571b5
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 9 deletions.
1 change: 1 addition & 0 deletions __tests__/downloader.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ const TRACK_URL = 'https://www.chosic.com/download-audio/45401/';

test('Get track details', async () => {
const track = await downloadTrack({
category: undefined,
baseURL: TRACK_URL,
headless: false,
});
Expand Down
13 changes: 11 additions & 2 deletions src/downloader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { tmpdir } from 'os';
import { mkdirSync, createWriteStream, unlink } from 'fs';
import { join } from 'path';
import * as https from 'https';
const FREE_MUSIC_BASE_URL = 'https://www.chosic.com/free-music';

const ACCEPT_COOKIE_BUTTON_SELECTOR =
'//div[contains(@role,"dialog")]//button[contains(@mode, "primary")]';
Expand All @@ -24,16 +25,18 @@ const DOWNLOAD_BUTTON_WRAPPER_SELECTOR = `${MAIN_TRACK_SELECTOR}/div[contains(@c
const DOWNLOAD_BUTTON_SELECTOR = `${DOWNLOAD_BUTTON_WRAPPER_SELECTOR}/button[contains(@class,"download")]`;
const DOWNLOAD_LINK_SELECTOR = '//a[contains(@class,"download2")]';
const DOWNLOADER_TEMP_FOLDER_PREFIX = 'choisic-downloads';

type DownloaderOptions = {
baseURL: string;
category: string | undefined;
headless?: boolean;
browser?: Browser | undefined;
page?: Page | undefined;
bypassCookies?: boolean;
downloadFilePath?: string;
baseURL?: string;
};

type DownloaderConfig = Required<DownloaderOptions>;
type DownloaderConfig = Required<Omit<DownloaderOptions, 'category'>>;

export type TrackInfo = {
title: string;
Expand Down Expand Up @@ -215,6 +218,12 @@ async function downloadFile(
async function getDownloaderConfig(
options: DownloaderOptions,
): Promise<DownloaderConfig> {
if (options.baseURL === undefined) {
options.baseURL = FREE_MUSIC_BASE_URL;
}
if (options.category !== undefined) {
options.baseURL = join(options.baseURL, options.category);
}
if (options.headless === undefined) {
options.headless = true;
}
Expand Down
14 changes: 7 additions & 7 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,19 +1,19 @@
import {
cleanDownloader,
downloadTrack,
getTrackLinks,
} from './chosic-downloader';
import { cleanDownloader, downloadTrack, getTrackLinks } from './downloader';

async function main(headless: boolean) {
const { config, trackLinks } = await getTrackLinks({
baseURL: 'https://www.chosic.com/free-music/lofi',
category: 'lofi',
headless: headless,
});
await cleanDownloader(config);
console.log(`Number of track links: ${trackLinks.length}`);
const tracks = [];
for (const trackLink of trackLinks) {
const track = await downloadTrack({ baseURL: trackLink, headless });
const track = await downloadTrack({
category: undefined,
baseURL: trackLink,
headless,
});
tracks.push(track);
}
console.debug('Tracks');
Expand Down

0 comments on commit f7571b5

Please sign in to comment.