Compare commits

..

3 Commits
v0.0 ... master

Author SHA1 Message Date
bel 740f02bf6b Set metadata and fix dl path 2020-02-01 20:20:07 +00:00
bel cb0eea6140 add email notifications on failure 2020-01-30 03:27:49 +00:00
bel 1f14a2b147 More logging and dont dupe dl 2020-01-30 01:36:51 +00:00
7 changed files with 138 additions and 39 deletions

13
Dockerfile Executable file
View File

@ -0,0 +1,13 @@
FROM frolvlad/alpine-glibc:alpine-3.9_glibc-2.29
RUN apk update && apk add --no-cache ca-certificates
RUN mkdir -p /var/log
WORKDIR /main
COPY . .
ENV GOPATH=""
ENV MNT="/mnt/"
ENTRYPOINT ["/main/exec-youtuber"]
CMD []

56
config/config.go Normal file → Executable file
View File

@ -1,20 +1,46 @@
package config package config
var ( import (
Feed = "https://www.youtube.com/feeds/videos.xml?channel_id=UCwX0AEx-qIhQ9kgtlNhyIXw" "local/args"
Root = "/tmp" "local/sandbox/contact/contact"
"os"
) )
/* var (
config = { Feed string
"path" : "../TV/Shepherds_Chapel" , Root string
"rss" : "https://www.youtube.com/feeds/videos.xml?channel_id=UCwX0AEx-qIhQ9kgtlNhyIXw" , To string
"last" : 0 , Subject string
"lasttime" : datetime.datetime(1999,1,1) , Emailer *contact.Emailer
"onlynew" : False , )
"delay" : 12 ,
"clear" : False , func init() {
"logloc" : "./log" , os.Setenv("LC_ALL", "C.UTF-8")
"idle" : False ,
as := args.NewArgSet()
as.Append(args.STRING, "to", "message recipient", "squeaky2x3@gmail.com")
as.Append(args.STRING, "subject", "message subject", "youtuber error")
as.Append(args.STRING, "from", "message sender", "breellocaldev@gmail.com")
as.Append(args.STRING, "password", "message sender password", "ML3WQRFSqe9rQ8qNkm")
as.Append(args.STRING, "smtp", "smtp server:port", "smtp.gmail.com:465")
as.Append(args.STRING, "pop3", "pop3 server:port", "pop.gmail.com:995")
as.Append(args.STRING, "feed", "feed URL", "https://www.youtube.com/feeds/videos.xml?channel_id=UCwX0AEx-qIhQ9kgtlNhyIXw")
as.Append(args.STRING, "root", "root to save videos", "/tmp")
if err := as.Parse(); err != nil {
panic(err)
}
Feed = as.Get("feed").GetString()
Root = as.Get("root").GetString()
To = as.Get("to").GetString()
Subject = as.Get("subject").GetString()
Emailer = &contact.Emailer{
From: as.Get("from").GetString(),
SMTP: as.Get("smtp").GetString(),
POP3: as.Get("pop3").GetString(),
Password: as.Get("password").GetString(),
}
} }
*/

0
feed/feed.go Normal file → Executable file
View File

0
feed/item.go Normal file → Executable file
View File

45
main.go Normal file → Executable file
View File

@ -1,6 +1,7 @@
package main package main
import ( import (
"errors"
"fmt" "fmt"
"local/youtuber/config" "local/youtuber/config"
"local/youtuber/feed" "local/youtuber/feed"
@ -9,19 +10,38 @@ import (
"os" "os"
"path" "path"
"strings" "strings"
"time"
) )
func main() { func main() {
os.Setenv("LC_ALL", "C.UTF-8") client, err := youtubedl.New()
results, err := feed.Fetch(config.Feed)
if err != nil { if err != nil {
panic(err) panic(err)
} }
log.Println(results) interval := time.Hour * 4
for true {
err := do(client)
if err != nil {
log.Println(err)
if err := config.Emailer.Send(
config.To,
config.Subject,
fmt.Sprintf("error getting youtuber: %v", err),
); err != nil {
panic(err)
}
}
log.Printf("sleeping until %v", time.Now().Add(interval))
time.Sleep(interval)
}
}
youtubedl, err := youtubedl.New() func do(client *youtubedl.Client) error {
errs := ""
results, err := feed.Fetch(config.Feed)
if err != nil { if err != nil {
panic(err) return err
} }
for _, result := range results { for _, result := range results {
@ -32,10 +52,17 @@ func main() {
result.Date.Day(), result.Date.Day(),
strings.Join(strings.Split(result.Title, " ")[2:], "_"), strings.Join(strings.Split(result.Title, " ")[2:], "_"),
) )
if err := youtubedl.Download(result.Link, path.Join(config.Root, target)); err != nil { target = path.Join(config.Root, target)
panic(err) if _, err := os.Stat(strings.ReplaceAll(target, "%(ext)s", "mp4")); !os.IsNotExist(err) {
log.Printf("already exists: %s", target)
continue
}
if err := client.Download(result.Link, target); err != nil {
errs += ", " + err.Error()
} }
return
} }
log.Println(youtubedl) if errs != "" {
return errors.New(errs)
}
return nil
} }

40
youtubedl/client.go Normal file → Executable file
View File

@ -15,15 +15,41 @@ func New() (*Client, error) {
} }
func (c *Client) Download(video, local string) error { func (c *Client) Download(video, local string) error {
cmd := exec.Command("youtube-dl", "-f", "22", "-o", local, "--write-sub", "--write-auto-sub", "--sub-format", "srt", video) errs := []error{}
log.Println(cmd.Path, cmd.Args) for _, extras := range [][]string{
out, err := cmd.CombinedOutput() []string{"--add-metadata", "--metadata-from-title", ".*[0-9]/[0-9][0-9] (?P<title>.+)"},
if err != nil { []string{},
err = fmt.Errorf("%v: %s", err, out) } {
args := append([]string{
"youtube-dl",
"-f",
"22",
"-o",
local,
"--write-sub",
"--write-auto-sub",
"--sub-format",
"srt",
}, extras...)
args = append(args, video)
cmd := exec.Command(args[0], args[1:]...)
log.Println(cmd.Path, cmd.Args)
out, err := cmd.CombinedOutput()
if err != nil {
log.Printf("failed with %v: %s", err, out)
errs = append(errs, fmt.Errorf("%v: %s", err, out))
} else {
log.Printf("passed with %s", out)
errs = nil
break
}
}
if len(errs) > 0 {
err := fmt.Errorf("%v", errs)
return err return err
} }
cmd = exec.Command("python3", "-m", "vtt_to_srt", path.Dir(local)) cmd := exec.Command("python3", "-m", "vtt_to_srt", path.Dir(local))
out, err = cmd.CombinedOutput() out, err := cmd.CombinedOutput()
if err != nil { if err != nil {
err = fmt.Errorf("%v: %s", err, out) err = fmt.Errorf("%v: %s", err, out)
return err return err

23
youtubedl/install.go Normal file → Executable file
View File

@ -2,11 +2,12 @@ package youtubedl
import ( import (
"errors" "errors"
"log"
"os/exec" "os/exec"
) )
func install() error { func install() error {
if err := installPyPip3(); err != nil { if err := installPyPip3FFMPEG(); err != nil {
return err return err
} }
if err := installYtdl(); err != nil { if err := installYtdl(); err != nil {
@ -18,26 +19,32 @@ func install() error {
return nil return nil
} }
func installPyPip3() error { func installPyPip3FFMPEG() error {
hasPy := exec.Command("python3", "--version") hasPy := exec.Command("python3", "--version")
hasPip := exec.Command("pip3", "--version") hasPip := exec.Command("pip3", "--version")
hasFFMPEG := exec.Command("which", "ffmpeg")
if err := hasPy.Run(); err != nil { if err := hasPy.Run(); err != nil {
} else if err := hasPip.Run(); err != nil { } else if err := hasPip.Run(); err != nil {
} else if err := hasFFMPEG.Run(); err != nil {
} else { } else {
return nil return nil
} }
for _, combo := range [][]string{ for _, combo := range [][]string{
[]string{"sudo", "apt", "install", "python3", "pip3"}, []string{"sudo", "apt", "install", "python3", "pip3", "ffmpeg"},
[]string{"apt", "install", "python3", "pip3"}, []string{"apt", "install", "python3", "pip3", "ffmpeg"},
[]string{"apk", "add", "python3", "py3-pip"}, []string{"apk", "add", "python3", "py3-pip", "ffmpeg"},
[]string{"sudo", "apk", "add", "python3", "py3-pip"}, []string{"sudo", "apk", "add", "python3", "py3-pip", "ffmpeg"},
[]string{"/sbin/apk", "add", "python3", "py3-pip", "ffmpeg"},
[]string{"sudo", "/sbin/apk", "add", "python3", "py3-pip", "ffmpeg"},
} { } {
cmd := exec.Command(combo[0], combo[1:]...) cmd := exec.Command(combo[0], combo[1:]...)
if err := cmd.Run(); err == nil { err := cmd.Run()
if err == nil {
return nil return nil
} }
log.Println("%v: %v", err, combo)
} }
return errors.New("cannot get python3 and pip3") return errors.New("cannot get python3 and pip3 and ffmpeg")
} }
func installYtdl() error { func installYtdl() error {