7c27c13642c5ab4880812637b332f3059dec2230
[oweals/peertube.git] / server / tools / peertube-watch.ts
1 import * as program from 'commander'
2 import { join } from 'path'
3 import { execSync } from 'child_process'
4
5 program
6   .name('watch')
7   .arguments('<url>')
8   .option('-g, --gui <player>', 'player type', /^(airplay|stdout|chromecast|mpv|vlc|mplayer|xbmc)$/i, 'vlc')
9   .option('-r, --resolution <res>', 'video resolution', '480')
10   .on('--help', function () {
11     console.log('  Available Players:')
12     console.log()
13     console.log('    - mpv')
14     console.log('    - mplayer')
15     console.log('    - vlc')
16     console.log('    - stdout')
17     console.log('    - xbmc')
18     console.log('    - airplay')
19     console.log('    - chromecast')
20     console.log()
21     console.log()
22     console.log('  Examples:')
23     console.log()
24     console.log('    $ peertube watch -g mpv https://peertube.cpy.re/videos/watch/e8a1af4e-414a-4d58-bfe6-2146eed06d10')
25     console.log('    $ peertube watch --gui stdout https://peertube.cpy.re/videos/watch/e8a1af4e-414a-4d58-bfe6-2146eed06d10')
26     console.log('    $ peertube watch https://peertube.cpy.re/videos/watch/e8a1af4e-414a-4d58-bfe6-2146eed06d10')
27     console.log()
28   })
29   .action((url, cmd) => {
30     run(url, cmd)
31       .catch(err => {
32         console.error(err)
33         process.exit(-1)
34       })
35   })
36   .parse(process.argv)
37
38 async function run (url: string, program: any) {
39   if (!url) {
40     console.error('<url> positional argument is required.')
41     process.exit(-1)
42   }
43
44   const cmd = 'node ' + join(__dirname, 'node_modules', 'webtorrent-hybrid', 'bin', 'cmd.js')
45   const args = ` --${program.gui} ` +
46     url.replace('videos/watch', 'download/torrents') +
47     `-${program.resolution}.torrent`
48
49   execSync(cmd + args)
50 }