feat: cache checker

This commit is contained in:
Mateusz Pstrucha committed 2021-11-30 21:54:20 +01:00
1 parent 62258b4535
commit 741d8fff6b
3 files changed
+81 -7

No files matched your search

+7
View File
@@ -25,6 +25,10 @@ if (major < minMajor || (major == minMajor && minor < minMinor)) {
// Parse arguments
const args = yargs(process.argv.slice(2))
.option('missing', {
alias: 'm',
type: 'boolean',
})
.option('tid', {
alias: 'T',
})
@@ -80,4 +84,7 @@ switch (args._[0]) {
})
})
break
case 'cache':
cache({ missingOnly: args.m })
break
}
+50
View File
@@ -0,0 +1,50 @@
import { limit } from '../env.js'
import yesno from 'yesno'
import { getTop } from './modules/calls.js'
import { convert, download, check } from './modules/youtube.js'
import { parse } from './modules/parse.js'
import { logTrack } from './modules/log.js'
import Time from './classes/Time.js'
export default async function cache({ missingOnly }) {
const tracks = await getTop() // tids
.then((tids) => tids.slice(0, limit))
.then(convert) // Tracks
.then(parse) // chart
.then((chart) => chart.flat()) // Tracks
let missing = []
for (let track of tracks) {
const target = `./cache/${track.tid}.mp4`
const { size, daysAgo } = check(target)
if (size) {
if (missingOnly) continue
logTrack(track, {
color: daysAgo >= 7 ? 'magenta' : 'green',
meta: `${(size / 1000 / 1000).toFixed(1)}MB used ${daysAgo.toFixed(
0
)} days ago`,
})
} else {
logTrack(track, { color: 'red', meta: 'Not cached' })
missing.push(track)
}
}
if (missing.length > 0)
if (
await yesno({
question: `Download ${missing.length} missing tracks?`,
defaultValue: false,
})
)
for (let track of missing) {
download(track)
}
}
+24 -7
View File
@@ -1,5 +1,11 @@
import spotifyToYt from 'spotify-to-yt'
import fs from 'fs'
import {
createWriteStream,
existsSync,
mkdirSync,
renameSync,
statSync,
} from 'fs'
import ytdl from 'ytdl-core'
import { logAction, logError } from './log.js'
@@ -32,8 +38,8 @@ function fetch(url, target, progress, end) {
const temp = target + '.tmp',
final = target
if (!fs.existsSync(cache)) {
fs.mkdirSync(cache)
if (!existsSync(cache)) {
mkdirSync(cache)
}
let stream = ytdl(url, {
@@ -43,7 +49,7 @@ function fetch(url, target, progress, end) {
return true
},
}),
file = fs.createWriteStream(temp)
file = createWriteStream(temp)
let written = 0
@@ -56,7 +62,7 @@ function fetch(url, target, progress, end) {
stream.on('end', () => {
file.close(() => {
fs.renameSync(temp, final)
renameSync(temp, final)
end(final, written)
resolve(final)
})
@@ -64,10 +70,21 @@ function fetch(url, target, progress, end) {
})
}
function check(path) {
if (existsSync(path)) {
let { size, atime } = statSync(path),
daysAgo = (new Date().getTime() - atime.getTime()) / 1024 / 60 / 60 / 24
return { size, daysAgo }
} else {
return { size: null, daysAgo: null }
}
}
async function download(track) {
const target = `${cache}/${track.tid}.mp4`
if (fs.existsSync(target)) return target
if (existsSync(target)) return target
return await fetch(
track.url,
@@ -86,4 +103,4 @@ async function download(track) {
})
}
export { convert, download }
export { convert, download, check }