-
+
diff --git a/src/lib/server/helpers.ts b/src/lib/server/helpers.ts
index b334c5b..4b42bbb 100644
--- a/src/lib/server/helpers.ts
+++ b/src/lib/server/helpers.ts
@@ -1,22 +1,20 @@
import formats from '$lib/common/supportedFormats.json';
+import winston from 'winston';
+import { dev } from '$app/environment';
-const formatMime = new Map(Object.entries(formats))
+export const logger = winston.createLogger({
+ level: dev ? 'debug' : 'error',
+ format: winston.format.json(),
+ transports: [new winston.transports.Console()]
+});
+const formatMime = new Map(Object.entries(formats));
export const isURLValid = (url: string) => {
- try {
- new URL(url)
- } catch {
- return false
- }
+ try {
+ new URL(url);
+ } catch {
+ return false;
+ }
- return true;
-}
+ return true;
+};
export const mimeTypeMap = formatMime;
-export const contentTypeFromFormat = (format: string): string => {
- const toReturn: string | undefined = formatMime.get(format)
-
- if (!toReturn) {
- throw new Error("Unsupported format")
- }
-
- return toReturn;
-}
\ No newline at end of file
diff --git a/src/lib/server/ytdlp.ts b/src/lib/server/ytdlp.ts
index 0d6b107..bbeecab 100644
--- a/src/lib/server/ytdlp.ts
+++ b/src/lib/server/ytdlp.ts
@@ -2,9 +2,10 @@ import { create } from 'youtube-dl-exec';
import { env } from '$env/dynamic/private';
import { spawn } from 'node:child_process';
import supportedFormats from '$lib/common/supportedFormats.json';
-import { mimeTypeMap } from '$lib/server/helpers';
+import { logger, mimeTypeMap } from '$lib/server/helpers';
const YTDLP_PATH: string = env.YTDLP_PATH as string;
+const HTTPS_PROXY: string = env.v as string;
export const ytdl = create(YTDLP_PATH);
@@ -16,7 +17,8 @@ export async function getYouTubeMetadata(link: string) {
dumpSingleJson: true,
noCheckCertificates: true,
noWarnings: true,
- preferFreeFormats: true
+ preferFreeFormats: true,
+ proxy: HTTPS_PROXY ? HTTPS_PROXY : ''
});
}
@@ -24,33 +26,55 @@ export async function getYouTubeMetadata(link: string) {
* Streams the YouTube video/audio using youtube-dl-exec
*/
export function streamYouTube(link: string, format: string): ReadableStream
{
- const mimeType: string | undefined = mimeTypeMap.get(format)
-
+ logger.debug(`Starting to stream: ${link}`);
+ const mimeType: string | undefined = mimeTypeMap.get(format);
+
if (!mimeType) {
- throw new Error("Unsupported format");
+ throw new Error('Unsupported format');
}
+ logger.debug(`Given format is compatible: ${mimeType}`);
return new ReadableStream({
start(controller) {
- const args = [
- '-o',
- '-',
- ].filter(Boolean);
+ const args = ['--no-write-thumbnail', '-o', '-'];
- if(mimeType?.includes('audio')) {
- args.push(...['--extract-audio', '--embed-metadata', '--embed-thumbnail', '--audio-format', format])
- } else if (mimeType.includes('video')) {
- args.push(...['--embed-metadata', '--embed-thumbnail', '--format', format])
+ if (HTTPS_PROXY) {
+ args.push('--proxy', HTTPS_PROXY);
}
- console.info(`${YTDLP_PATH} ${args.join(' ')} ${link}`)
+ if (mimeType?.includes('audio')) {
+ args.push(
+ ...['--extract-audio', '--embed-metadata', '--embed-thumbnail', '--audio-format', format]
+ );
+ } else if (mimeType.includes('video')) {
+ args.push(...['--embed-metadata', '--embed-thumbnail', '--format', format]);
+ }
- const process = spawn('yt-dlp', [...args, link], { stdio: ['ignore', 'pipe', 'ignore'] });
+ const cmd = `${YTDLP_PATH} ${args.join(' ')} ${link}`;
+ logger.debug(`Running: ${cmd}`);
- process.stdout.on('data', (chunk) => controller.enqueue(chunk));
- process.stdout.on('end', () => controller.close());
+ const process = spawn(YTDLP_PATH, [...args, link], {
+ cwd: '/tmp',
+ stdio: ['ignore', 'pipe', 'pipe']
+ });
+
+ process.stdout.on('data', (chunk) => {
+ try {
+ controller.enqueue(chunk);
+ } catch (ex) {
+ process.kill();
+ }
+ });
+ process.stderr.on('data', (chunk) => logger.debug(chunk.toString()));
+ process.stdout.on('end', () => {
+ try {
+ controller.close();
+ } catch (ex) {
+ logger.error(ex);
+ }
+ });
process.stdout.on('error', (err) => {
- console.error('Stream error:', err);
+ logger.error('Stream error:', err);
controller.error(err);
});
}
diff --git a/src/routes/+layout.svelte b/src/routes/+layout.svelte
index 61642b3..0d97a1a 100644
--- a/src/routes/+layout.svelte
+++ b/src/routes/+layout.svelte
@@ -17,4 +17,11 @@
transform: rotate(180deg);
display: inline-block;
}
+
+ @media screen and (max-height: 600px) {
+ /* Your CSS rules here */
+ footer {
+ display: none;
+ }
+ }
diff --git a/src/routes/+page.server.ts b/src/routes/+page.server.ts
new file mode 100644
index 0000000..5b66645
--- /dev/null
+++ b/src/routes/+page.server.ts
@@ -0,0 +1,3 @@
+export const prerender = true;
+export const ssr = true;
+export const csr = true;
diff --git a/src/routes/+page.svelte b/src/routes/+page.svelte
index 34f5934..07793c0 100644
--- a/src/routes/+page.svelte
+++ b/src/routes/+page.svelte
@@ -10,42 +10,49 @@
let showModal = $state(false);
let href = $state('');
let disabled = $state(true);
- let metadata = $state(true);
+ let metadata = $state(false);
let downloading = $state(false);
+ let logs = $state('');
+ let logId = undefined;
const formats = Object.keys(supportedFormats).map((f) => {
return { value: f, label: f.toUpperCase() };
});
+ const sources = [
+ { value: 'youtube', label: 'YouTube' },
+ { value: 'youtube', label: 'Any Other Website' }
+ ];
const toggleModal = () => {
showModal = !showModal;
};
onMount(() => {
- document.cookie = 'downloading=0'
+ document.cookie = 'downloading=0';
});
+ const readLogs = () => {
+ logId = setInterval(() => {
+ logs += "We're downloading
";
+ }, 2000);
+ };
const onClick = () => {
+ let checkIterations = 0;
link = '';
downloading = true;
- document.cookie = 'downloading=1'
+ document.cookie = 'downloading=1';
+
+ readLogs();
const id = setInterval(() => {
- if (document.cookie.includes('downloading=0')) {
- console.log("FINITO!")
- downloading = false && clearInterval(id);
+ if (document.cookie.includes('downloading=0') || checkIterations > 3) {
+ downloading = false && clearInterval(id) && clearInterval(logId);
}
+ checkIterations++;
}, 1000);
};
const createAnchor = () => {
- console.log({
- source,
- link,
- format,
- metadata
- });
-
if (!(source && link && format)) {
disabled = true;
return;
@@ -68,7 +75,8 @@
searchParams.append('source', source);
searchParams.append('link', link);
searchParams.append('format', format);
- searchParams.append('metadata', metadata);
+
+ if (metadata) searchParams.append('metadata', '1');
href = `/download?${searchParams.toString()}`;
};
@@ -83,12 +91,20 @@
}
});
+
-
+
+ {@html logs}
@@ -104,46 +120,42 @@
-
🐙 Scaricatore 🐙
+
🐙 Scaricatore 🐙