aboutsummaryrefslogtreecommitdiffhomepage
path: root/server/lib/schedulers/youtube-dl-update-scheduler.ts
blob: da47378e845ee54d2ac73ca5bace9cd86699069a (plain) (blame)
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
// Thanks: https://github.com/przemyslawpluta/node-youtube-dl/blob/master/lib/downloader.js
// We rewrote it to avoid sync calls

import { AbstractScheduler } from './abstract-scheduler'
import { SCHEDULER_INTERVALS_MS } from '../../initializers'
import { logger } from '../../helpers/logger'
import * as request from 'request'
import { createWriteStream, writeFile } from 'fs-extra'
import { join } from 'path'
import { mkdirpPromise, root } from '../../helpers/core-utils'

export class YoutubeDlUpdateScheduler extends AbstractScheduler {

  private static instance: AbstractScheduler

  protected schedulerIntervalMs = SCHEDULER_INTERVALS_MS.youtubeDLUpdate

  private constructor () {
    super()
  }

  async execute () {
    logger.info('Updating youtubeDL binary.')

    const binDirectory = join(root(), 'node_modules', 'youtube-dl', 'bin')
    const bin = join(binDirectory, 'youtube-dl')
    const detailsPath = join(binDirectory, 'details')
    const url = 'https://yt-dl.org/downloads/latest/youtube-dl'

    await mkdirpPromise(binDirectory)

    return new Promise(res => {
      request.get(url, { followRedirect: false }, (err, result) => {
        if (err) {
          logger.error('Cannot update youtube-dl.', { err })
          return res()
        }

        if (result.statusCode !== 302) {
          logger.error('youtube-dl update error: did not get redirect for the latest version link. Status %d', result.statusCode)
          return res()
        }

        const url = result.headers.location
        const downloadFile = request.get(url)
        const newVersion = /yt-dl\.org\/downloads\/(\d{4}\.\d\d\.\d\d(\.\d)?)\/youtube-dl/.exec(url)[ 1 ]

        downloadFile.on('response', result => {
          if (result.statusCode !== 200) {
            logger.error('Cannot update youtube-dl: new version response is not 200, it\'s %d.', result.statusCode)
            return res()
          }

          downloadFile.pipe(createWriteStream(bin, { mode: 493 }))
        })

        downloadFile.on('error', err => {
          logger.error('youtube-dl update error.', { err })
          return res()
        })

        downloadFile.on('end', () => {
          const details = JSON.stringify({ version: newVersion, path: bin, exec: 'youtube-dl' })
          writeFile(detailsPath, details, { encoding: 'utf8' }, err => {
            if (err) {
              logger.error('youtube-dl update error: cannot write details.', { err })
              return res()
            }

            logger.info('youtube-dl updated to version %s.', newVersion)
            return res()
          })
        })
      })
    })
  }

  static get Instance () {
    return this.instance || (this.instance = new this())
  }
}