Created
January 14, 2020 04:52
-
-
Save skippednote/c58f3e65c65c27e904b30b2f04a2cdc2 to your computer and use it in GitHub Desktop.
Sort videos downloaded from O'Reily Media as per their table of contents
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
const toc = require("./toc.json"); | |
const fs = require("fs").promises; | |
tocU = toc.map(i => { | |
i.key = i.key.replace("/", "_"); | |
i.entries = i.entries.map(e => e.replace("?", "").replace("/", "_")); | |
return i; | |
}); | |
async function getVideosFromDirectory() { | |
const files = await fs.readdir("./manning-sls"); | |
const mp4s = files.filter(i => i.includes(".mp4")); | |
const videos = mp4s.reduce((acc, v) => { | |
const regex = /(.*)(-.*)$/; | |
acc[regex.exec(v)[1]] = v; | |
return acc; | |
}, {}); | |
return videos; | |
} | |
async function createSectionAndMoveFiles(key, entries, videos) { | |
await fs.mkdir(`./dump1/${key}`); | |
for await (const e of entries) { | |
const video = videos[e.slice(3)]; | |
await fs.copyFile(`./manning-sls/${video}`, `./dump1/${key}/${e}.mp4`); | |
} | |
} | |
async function main() { | |
const videos = await getVideosFromDirectory(); | |
for await (const { key, entries } of tocU) { | |
await createSectionAndMoveFiles(key, entries, videos); | |
} | |
} | |
main(); |
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
const toc = require("./toc.json"); | |
const fs = require("fs"); | |
const videos = fs | |
.readdirSync("./sls") | |
.filter(i => i.includes(".mp4")) | |
.reduce((acc, v) => { | |
const regex = /(.*)(-.*)$/; | |
acc[regex.exec(v)[1]] = v; | |
return acc; | |
}, {}); | |
fs.writeFileSync("./videos.json", JSON.stringify(videos, null, 4)); | |
toc.forEach(({ key, entries }) => { | |
fs.mkdirSync(`./dump/${key.replace("/", "_")}`); | |
entries.forEach(e => { | |
const video = | |
videos[ | |
e | |
.slice(3) | |
.replace("?", "") | |
.replace("/", "_") | |
]; | |
console.log(video, e); | |
fs.copyFileSync( | |
`./sls/${video}`, | |
`./dump/${key.replace("/", "_")}/${e | |
.replace("/", "_") | |
.replace("?", "")}.mp4` | |
); | |
}); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment