-
Notifications
You must be signed in to change notification settings - Fork 0
/
dmgbot.nim
43 lines (32 loc) · 1.22 KB
/
dmgbot.nim
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
import nimpy
import os
import re
from osproc import execCmdEx
import strutils
let webvtt = pyImport("webvtt")
let py = pyBuiltinsModule()
const outputPath = "./data/sub"
const outputExt = ".en.vtt"
let youtubeUrl = paramStr(1)
var youtubeDlCmd: string = "youtube-dl --write-auto-sub --sub-lang en --skip-download --get-title -o " & outputPath & ".vtt " & youtubeUrl
echo "Cmd: " & youtubeDlCmd
let (title, errorCode) = execCmdEx(youtubeDlCmd)
discard errorCode
echo "Title: [" & title & "]"
var filename = title[0 ..< title.find(" - ")]
filename = filename.toLowerAscii()
filename = filename.replace(re"[^a-zA-Z0-9]", "-")
echo "Filename: [" & filename & "]"
let file = open("./output/" & filename & ".txt", fmWrite)
file.writeLine(title)
file.writeLine("==============================")
file.writeLine("https://www.patreon.com/goodtradesbrigade/posts")
file.writeLine("Subtitles downloaded and cleaned by DMG Bot (https://github.com/accidentalrebel/dmg-bot)")
file.writeLine("==============================\n")
let lines = webvtt.read(outputPath & outputExt)
var line: string;
for i in countup(1, py.len(lines).to(int) - 1, 2):
line = lines[i].text.to(string)
line = replace(line,"\n","")
file.writeLine(line)
file.close()