Last active
April 16, 2022 20:20
-
-
Save maccyber/133d02428df3ea4b9e6494c24d5357e8 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import http from 'http' | |
import {readFile, stat, unlink} from 'fs/promises' | |
import {createWriteStream} from 'fs' | |
const fileContent = await readFile('./list', 'utf-8') | |
const listOfUrls = fileContent | |
.split(/\r?\n/) | |
.filter((line) => line) | |
listOfUrls.forEach(downloadFile) | |
async function downloadFile(url, i) { | |
const filename = `list_${i}.m3u` | |
http.get(url, (response) => { | |
if (response.statusCode !== 200) { | |
console.error(`${url}: ${response.statusCode}`) | |
return | |
} | |
const file = createWriteStream(filename) | |
response.pipe(file) | |
file.on('finish', async () => { | |
file.close() | |
const {size} = await stat(filename) | |
const content = await readFile(filename, 'utf-8') | |
if (content.slice(0, 7) !== '#EXTM3U') await unlink(filename) | |
console.log(`${url}: Downloaded to ${filename} - Size: ${size}`) | |
}) | |
}) | |
.on('error', (error) => console.error(`${url}, ${error.message}`)) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
30s streams: