js
复制代码
//check-video.js
const sections = [
{
content_length_text: '01:42:37',
content_length: 6157,
index: 1,
items: [
{
can_be_previewed: true,
content_summary: '03:07',
id: 42059630,
title: 'What Is Social Media Marketing',
object_index: 1,
item_type: 'lecture',
},
{
can_be_previewed: true,
content_summary: '03:19',
id: 42059642,
title: 'Social Media Marketing - How to Use Social Platforms to Grow Your Business',
object_index: 2,
item_type: 'lecture',
},
{
can_be_previewed: true,
content_summary: '03:09',
id: 42059658,
title: 'Social Media Marketing Course - Top Social Platforms',
object_index: 3,
item_type: 'lecture',
}],
lecture_count: 21,
title: 'Social Media Marketing',
}]
// npm 安装依赖
const axios = require('axios')
function buildUrl(item, sitem) {
const className = item.title // 一级目录
const subClassName = sitem.title //二级标题
return `/${className}/${subClassName}.mp4`
}
async function checkVideoHead(url) {
try {
const response = await axios.head(url)
if (response.status === 200 && response.headers['content-type'].startsWith('video/')) {
return { url, playable: true }
} else {
return { url, playable: false }
}
} catch (err) {
return { url, playable: false }
}
}
;(async () => {
const allResults = []
let count = 0 ;
for (const section of sections) {
for (const sitem of section.items) {
const url = buildUrl(section, sitem)
const result = await checkVideoHead(url)
allResults.push(result)
count ++;
console.log(count,`${result.playable ? '✅' : '❌'} ${url}`)
}
}
})()