-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathubutv.mjs
executable file
·84 lines (77 loc) · 2.31 KB
/
ubutv.mjs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
#!/usr/bin/env node
import { writeFileSync } from 'fs'
import { exit } from 'process'
import { spawnSync } from 'child_process'
import { Readable } from 'stream'
import { JSDOM } from 'jsdom'
const cache = {}
const videoUrls = []
const CRAWL_URL = 'https://www.ubu.com/film/index.html'
const PLAYLIST_FILE = '/tmp/ubutv.m3u'
const MAX_VIDEOS = 5
function rand(val) {
return Math.floor(Math.random() * val)
}
function sleep(ms) {
return new Promise((resolve) => {
setTimeout(resolve, ms)
})
}
async function crawl(href) {
await sleep(3000)
const url = new URL(href)
if (!url.pathname.startsWith('/film')) {
return null
}
console.log('CRAWLING:', href)
let document = cache[href]
if (!document) {
let res
try {
res = await fetch(url.href)
} catch (err) {
return crawl(href)
}
const html = await res.text()
const dom = new JSDOM(html, { url: url.href })
cache[href] = document = dom.window.document
}
const links = Array.from(document.querySelectorAll(':any-link'))
const htmlLinks = links.filter(a =>
a.href.endsWith('.html') && a.pathname !== '/index.html')
const videoLinks = links.filter(a =>
a.href.match(/.(m4a|mp4|mkv|m4v)$/g))
if (videoLinks.length) {
return videoLinks[rand(videoLinks.length)].href
}
if (htmlLinks.length) {
return crawl(htmlLinks[rand(htmlLinks.length)].href)
}
return null
}
async function main() {
while (videoUrls.length < MAX_VIDEOS) {
let videoUrl
while (!(videoUrl = await crawl(CRAWL_URL))) {
console.log('RECRAWLING')
}
videoUrls.push(videoUrl)
const videoIndex = videoUrls.indexOf(videoUrl) + 1
console.log(`FOUND VIDEO (${videoIndex}/${MAX_VIDEOS}): ${videoUrl}`)
}
writeFileSync(PLAYLIST_FILE, videoUrls.join('\n'))
console.log(`LOOPING ${MAX_VIDEOS} VIDEOS`)
spawnSync('killall vlc', { shell: true })
let cmd
if (process.platform === 'linux') {
cmd = spawnSync(`vlc --no-interact --random -f ${PLAYLIST_FILE}`, { shell: true })
}
if (process.platform === 'darwin') {
cmd = spawnSync(`open -na VLC.app --args --no-interact --random -f ${PLAYLIST_FILE}`, { shell: true })
}
Readable.from(cmd.stdout).pipe(process.stdout)
Readable.from(cmd.stderr).pipe(process.stderr)
if (cmd.status !== 0) throw new Error(cmd.error ? cmd.error : cmd)
exit(0)
}
main()