2021-09-30 23:48:56 +09:30
|
|
|
package download
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"io"
|
2022-01-05 23:56:12 +10:30
|
|
|
"log"
|
|
|
|
"net/url"
|
2022-01-06 16:19:22 +10:30
|
|
|
"os"
|
2021-09-30 23:48:56 +09:30
|
|
|
"os/exec"
|
|
|
|
"regexp"
|
|
|
|
"strconv"
|
|
|
|
"strings"
|
|
|
|
"sync"
|
2022-04-06 20:35:28 +09:30
|
|
|
"sync/atomic"
|
2021-11-21 16:19:49 +10:30
|
|
|
"time"
|
2021-09-30 23:48:56 +09:30
|
|
|
|
|
|
|
"github.com/tardisx/gropple/config"
|
|
|
|
)
|
|
|
|
|
|
|
|
type Download struct {
|
|
|
|
Id int `json:"id"`
|
|
|
|
Url string `json:"url"`
|
2021-10-26 22:48:16 +10:30
|
|
|
PopupUrl string `json:"popup_url"`
|
2022-01-06 16:19:22 +10:30
|
|
|
Process *os.Process `json:"-"`
|
2021-09-30 23:48:56 +09:30
|
|
|
ExitCode int `json:"exit_code"`
|
|
|
|
State string `json:"state"`
|
|
|
|
DownloadProfile config.DownloadProfile `json:"download_profile"`
|
|
|
|
Finished bool `json:"finished"`
|
2021-11-21 16:19:49 +10:30
|
|
|
FinishedTS time.Time `json:"finished_ts"`
|
2021-09-30 23:48:56 +09:30
|
|
|
Files []string `json:"files"`
|
2022-04-09 15:13:22 +09:30
|
|
|
PlaylistCurrent int `json:"playlist_current"`
|
|
|
|
PlaylistTotal int `json:"playlist_total"`
|
2021-09-30 23:48:56 +09:30
|
|
|
Eta string `json:"eta"`
|
|
|
|
Percent float32 `json:"percent"`
|
|
|
|
Log []string `json:"log"`
|
|
|
|
Config *config.Config
|
2022-01-05 23:56:12 +10:30
|
|
|
mutex sync.Mutex
|
2021-09-30 23:48:56 +09:30
|
|
|
}
|
|
|
|
|
2021-11-21 16:19:49 +10:30
|
|
|
type Downloads []*Download
|
|
|
|
|
2022-01-06 21:37:30 +10:30
|
|
|
var CanStopDownload = false
|
|
|
|
|
2022-04-06 20:35:28 +09:30
|
|
|
var downloadId int32 = 0
|
|
|
|
|
2021-11-21 16:19:49 +10:30
|
|
|
// StartQueued starts any downloads that have been queued, we would not exceed
|
|
|
|
// maxRunning. If maxRunning is 0, there is no limit.
|
|
|
|
func (dls Downloads) StartQueued(maxRunning int) {
|
2022-01-05 23:56:12 +10:30
|
|
|
active := make(map[string]int)
|
2021-11-21 16:19:49 +10:30
|
|
|
|
|
|
|
for _, dl := range dls {
|
2022-01-05 23:56:12 +10:30
|
|
|
|
|
|
|
dl.mutex.Lock()
|
|
|
|
|
2021-11-21 16:19:49 +10:30
|
|
|
if dl.State == "downloading" {
|
2022-01-05 23:56:12 +10:30
|
|
|
active[dl.domain()]++
|
2021-11-21 16:19:49 +10:30
|
|
|
}
|
2022-01-05 23:56:12 +10:30
|
|
|
dl.mutex.Unlock()
|
|
|
|
|
2021-11-21 16:19:49 +10:30
|
|
|
}
|
|
|
|
|
2022-01-05 23:56:12 +10:30
|
|
|
for _, dl := range dls {
|
|
|
|
|
|
|
|
dl.mutex.Lock()
|
|
|
|
|
|
|
|
if dl.State == "queued" && (maxRunning == 0 || active[dl.domain()] < maxRunning) {
|
|
|
|
dl.State = "downloading"
|
|
|
|
active[dl.domain()]++
|
2022-01-06 16:19:22 +10:30
|
|
|
log.Printf("Starting download for id:%d (%s)", dl.Id, dl.Url)
|
2022-01-05 23:56:12 +10:30
|
|
|
dl.mutex.Unlock()
|
|
|
|
go func() { dl.Begin() }()
|
|
|
|
} else {
|
|
|
|
dl.mutex.Unlock()
|
2021-11-21 16:19:49 +10:30
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
// Cleanup removes old downloads from the list. Hardcoded to remove them one hour
|
|
|
|
// completion.
|
|
|
|
func (dls Downloads) Cleanup() Downloads {
|
|
|
|
newDLs := Downloads{}
|
|
|
|
for _, dl := range dls {
|
2022-01-05 23:56:12 +10:30
|
|
|
|
|
|
|
dl.mutex.Lock()
|
|
|
|
|
2021-11-21 16:19:49 +10:30
|
|
|
if dl.Finished && time.Since(dl.FinishedTS) > time.Duration(time.Hour) {
|
|
|
|
// do nothing
|
|
|
|
} else {
|
|
|
|
newDLs = append(newDLs, dl)
|
|
|
|
}
|
2022-01-05 23:56:12 +10:30
|
|
|
dl.mutex.Unlock()
|
|
|
|
|
2021-11-21 16:19:49 +10:30
|
|
|
}
|
|
|
|
return newDLs
|
|
|
|
}
|
|
|
|
|
|
|
|
// Queue queues a download
|
|
|
|
func (dl *Download) Queue() {
|
2022-01-05 23:56:12 +10:30
|
|
|
|
|
|
|
dl.mutex.Lock()
|
|
|
|
defer dl.mutex.Unlock()
|
|
|
|
|
2021-11-21 16:19:49 +10:30
|
|
|
dl.State = "queued"
|
2022-01-05 23:56:12 +10:30
|
|
|
|
|
|
|
}
|
|
|
|
|
2022-04-06 20:35:28 +09:30
|
|
|
func NewDownload(conf *config.Config, url string) *Download {
|
|
|
|
atomic.AddInt32(&downloadId, 1)
|
|
|
|
dl := Download{
|
|
|
|
Config: conf,
|
|
|
|
|
|
|
|
Id: int(downloadId),
|
|
|
|
Url: url,
|
|
|
|
PopupUrl: fmt.Sprintf("/fetch/%d", int(downloadId)),
|
|
|
|
State: "choose profile",
|
|
|
|
Finished: false,
|
|
|
|
Eta: "?",
|
|
|
|
Percent: 0.0,
|
|
|
|
Log: make([]string, 0, 1000),
|
|
|
|
}
|
|
|
|
return &dl
|
|
|
|
}
|
|
|
|
|
2022-01-05 23:56:12 +10:30
|
|
|
func (dl *Download) Stop() {
|
2022-01-06 21:37:30 +10:30
|
|
|
if !CanStopDownload {
|
|
|
|
log.Print("attempted to stop download on a platform that it is not currently supported on - please report this as a bug")
|
|
|
|
os.Exit(1)
|
|
|
|
}
|
2022-01-05 23:56:12 +10:30
|
|
|
log.Printf("stopping the download")
|
|
|
|
dl.mutex.Lock()
|
2022-01-06 16:19:22 +10:30
|
|
|
dl.Log = append(dl.Log, "aborted by user")
|
2022-01-05 23:56:12 +10:30
|
|
|
defer dl.mutex.Unlock()
|
2022-01-06 16:19:22 +10:30
|
|
|
dl.Process.Kill()
|
2022-01-05 23:56:12 +10:30
|
|
|
}
|
|
|
|
|
|
|
|
func (dl *Download) domain() string {
|
|
|
|
|
|
|
|
// note that we expect to already have the mutex locked by the caller
|
|
|
|
url, err := url.Parse(dl.Url)
|
|
|
|
if err != nil {
|
|
|
|
log.Printf("Unknown domain for url: %s", dl.Url)
|
|
|
|
return "unknown"
|
|
|
|
}
|
|
|
|
|
|
|
|
return url.Hostname()
|
|
|
|
|
2021-11-21 16:19:49 +10:30
|
|
|
}
|
|
|
|
|
2021-09-30 23:48:56 +09:30
|
|
|
// Begin starts a download, by starting the command specified in the DownloadProfile.
|
|
|
|
// It blocks until the download is complete.
|
|
|
|
func (dl *Download) Begin() {
|
2022-01-05 23:56:12 +10:30
|
|
|
|
|
|
|
dl.mutex.Lock()
|
|
|
|
|
2021-11-21 16:19:49 +10:30
|
|
|
dl.State = "downloading"
|
2021-09-30 23:48:56 +09:30
|
|
|
cmdSlice := []string{}
|
|
|
|
cmdSlice = append(cmdSlice, dl.DownloadProfile.Args...)
|
2021-11-21 16:19:49 +10:30
|
|
|
|
2022-01-06 16:19:22 +10:30
|
|
|
// only add the url if it's not empty or an example URL. This helps us with testing
|
|
|
|
if !(dl.Url == "" || strings.Contains(dl.domain(), "example.org")) {
|
2021-11-21 16:19:49 +10:30
|
|
|
cmdSlice = append(cmdSlice, dl.Url)
|
|
|
|
}
|
2021-09-30 23:48:56 +09:30
|
|
|
|
|
|
|
cmd := exec.Command(dl.DownloadProfile.Command, cmdSlice...)
|
|
|
|
cmd.Dir = dl.Config.Server.DownloadPath
|
|
|
|
|
|
|
|
stdout, err := cmd.StdoutPipe()
|
|
|
|
if err != nil {
|
|
|
|
dl.State = "failed"
|
|
|
|
dl.Finished = true
|
2021-11-21 16:19:49 +10:30
|
|
|
dl.FinishedTS = time.Now()
|
2021-09-30 23:48:56 +09:30
|
|
|
dl.Log = append(dl.Log, fmt.Sprintf("error setting up stdout pipe: %v", err))
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
stderr, err := cmd.StderrPipe()
|
|
|
|
if err != nil {
|
|
|
|
dl.State = "failed"
|
|
|
|
dl.Finished = true
|
2021-11-21 16:19:49 +10:30
|
|
|
dl.FinishedTS = time.Now()
|
2021-09-30 23:48:56 +09:30
|
|
|
dl.Log = append(dl.Log, fmt.Sprintf("error setting up stderr pipe: %v", err))
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-01-06 16:19:22 +10:30
|
|
|
log.Printf("Executing command: %v", cmd)
|
2021-09-30 23:48:56 +09:30
|
|
|
err = cmd.Start()
|
|
|
|
if err != nil {
|
|
|
|
dl.State = "failed"
|
|
|
|
dl.Finished = true
|
2021-11-21 16:19:49 +10:30
|
|
|
dl.FinishedTS = time.Now()
|
|
|
|
dl.Log = append(dl.Log, fmt.Sprintf("error starting command '%s': %v", dl.DownloadProfile.Command, err))
|
2021-09-30 23:48:56 +09:30
|
|
|
return
|
|
|
|
}
|
2022-01-06 16:19:22 +10:30
|
|
|
dl.Process = cmd.Process
|
2021-09-30 23:48:56 +09:30
|
|
|
|
|
|
|
var wg sync.WaitGroup
|
|
|
|
|
2022-01-05 23:56:12 +10:30
|
|
|
dl.mutex.Unlock()
|
|
|
|
|
2021-09-30 23:48:56 +09:30
|
|
|
wg.Add(2)
|
|
|
|
go func() {
|
|
|
|
defer wg.Done()
|
|
|
|
dl.updateDownload(stdout)
|
|
|
|
}()
|
|
|
|
|
|
|
|
go func() {
|
|
|
|
defer wg.Done()
|
|
|
|
dl.updateDownload(stderr)
|
|
|
|
}()
|
|
|
|
|
|
|
|
wg.Wait()
|
|
|
|
cmd.Wait()
|
|
|
|
|
2022-01-05 23:56:12 +10:30
|
|
|
dl.mutex.Lock()
|
2022-01-06 16:19:22 +10:30
|
|
|
log.Printf("Process finished for id: %d (%v)", dl.Id, cmd)
|
2022-01-05 23:56:12 +10:30
|
|
|
|
2021-09-30 23:48:56 +09:30
|
|
|
dl.State = "complete"
|
|
|
|
dl.Finished = true
|
2021-11-21 16:19:49 +10:30
|
|
|
dl.FinishedTS = time.Now()
|
2021-09-30 23:48:56 +09:30
|
|
|
dl.ExitCode = cmd.ProcessState.ExitCode()
|
|
|
|
|
|
|
|
if dl.ExitCode != 0 {
|
|
|
|
dl.State = "failed"
|
|
|
|
}
|
2022-01-05 23:56:12 +10:30
|
|
|
dl.mutex.Unlock()
|
2021-09-30 23:48:56 +09:30
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
func (dl *Download) updateDownload(r io.Reader) {
|
|
|
|
// XXX not sure if we might get a partial line?
|
|
|
|
buf := make([]byte, 1024)
|
|
|
|
for {
|
|
|
|
n, err := r.Read(buf)
|
|
|
|
if n > 0 {
|
|
|
|
s := string(buf[:n])
|
|
|
|
lines := strings.Split(s, "\n")
|
|
|
|
|
|
|
|
for _, l := range lines {
|
|
|
|
|
|
|
|
if l == "" {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2022-01-05 23:56:12 +10:30
|
|
|
dl.mutex.Lock()
|
|
|
|
|
2021-09-30 23:48:56 +09:30
|
|
|
// append the raw log
|
|
|
|
dl.Log = append(dl.Log, l)
|
|
|
|
|
2022-01-05 23:56:12 +10:30
|
|
|
dl.mutex.Unlock()
|
|
|
|
|
2021-09-30 23:48:56 +09:30
|
|
|
// look for the percent and eta and other metadata
|
|
|
|
dl.updateMetadata(l)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (dl *Download) updateMetadata(s string) {
|
|
|
|
|
2022-01-05 23:56:12 +10:30
|
|
|
dl.mutex.Lock()
|
|
|
|
|
|
|
|
defer dl.mutex.Unlock()
|
|
|
|
|
2021-09-30 23:48:56 +09:30
|
|
|
// [download] 49.7% of ~15.72MiB at 5.83MiB/s ETA 00:07
|
2022-04-09 17:46:03 +09:30
|
|
|
// [download] 99.3% of ~1.42GiB at 320.87KiB/s ETA 00:07 (frag 212/214)
|
|
|
|
etaRE := regexp.MustCompile(`download.+ETA +(\d\d:\d\d(?::\d\d)?)`)
|
2021-09-30 23:48:56 +09:30
|
|
|
matches := etaRE.FindStringSubmatch(s)
|
|
|
|
if len(matches) == 2 {
|
|
|
|
dl.Eta = matches[1]
|
|
|
|
dl.State = "downloading"
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
percentRE := regexp.MustCompile(`download.+?([\d\.]+)%`)
|
|
|
|
matches = percentRE.FindStringSubmatch(s)
|
|
|
|
if len(matches) == 2 {
|
|
|
|
p, err := strconv.ParseFloat(matches[1], 32)
|
|
|
|
if err == nil {
|
|
|
|
dl.Percent = float32(p)
|
|
|
|
} else {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// This appears once per destination file
|
|
|
|
// [download] Destination: Filename with spaces and other punctuation here be careful!.mp4
|
|
|
|
filename := regexp.MustCompile(`download.+?Destination: (.+)$`)
|
|
|
|
matches = filename.FindStringSubmatch(s)
|
|
|
|
if len(matches) == 2 {
|
|
|
|
dl.Files = append(dl.Files, matches[1])
|
|
|
|
}
|
|
|
|
|
|
|
|
// This means a file has been "created" by merging others
|
|
|
|
// [ffmpeg] Merging formats into "Toto - Africa (Official HD Video)-FTQbiNvZqaY.mp4"
|
|
|
|
mergedFilename := regexp.MustCompile(`Merging formats into "(.+)"$`)
|
|
|
|
matches = mergedFilename.FindStringSubmatch(s)
|
|
|
|
if len(matches) == 2 {
|
|
|
|
dl.Files = append(dl.Files, matches[1])
|
|
|
|
}
|
|
|
|
|
|
|
|
// This means a file has been deleted
|
|
|
|
// Gross - this time it's unquoted and has trailing guff
|
|
|
|
// Deleting original file Toto - Africa (Official HD Video)-FTQbiNvZqaY.f137.mp4 (pass -k to keep)
|
|
|
|
// This is very fragile
|
|
|
|
deletedFile := regexp.MustCompile(`Deleting original file (.+) \(pass -k to keep\)$`)
|
|
|
|
matches = deletedFile.FindStringSubmatch(s)
|
|
|
|
if len(matches) == 2 {
|
|
|
|
// find the index
|
|
|
|
for i, f := range dl.Files {
|
|
|
|
if f == matches[1] {
|
|
|
|
dl.Files = append(dl.Files[:i], dl.Files[i+1:]...)
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2022-04-09 15:13:22 +09:30
|
|
|
|
|
|
|
// [download] Downloading video 1 of 3
|
|
|
|
playlistDetails := regexp.MustCompile(`Downloading video (\d+) of (\d+)`)
|
|
|
|
matches = playlistDetails.FindStringSubmatch(s)
|
|
|
|
if len(matches) == 3 {
|
|
|
|
total, _ := strconv.ParseInt(matches[2], 10, 32)
|
|
|
|
current, _ := strconv.ParseInt(matches[1], 10, 32)
|
|
|
|
dl.PlaylistTotal = int(total)
|
|
|
|
dl.PlaylistCurrent = int(current)
|
|
|
|
}
|
|
|
|
|
|
|
|
// [Site] user: Downloading JSON metadata page 2
|
|
|
|
metadataDL := regexp.MustCompile(`Downloading JSON metadata page (\d+)`)
|
|
|
|
matches = metadataDL.FindStringSubmatch(s)
|
|
|
|
if len(matches) == 2 {
|
|
|
|
dl.State = "Downloading metadata, page " + matches[1]
|
|
|
|
}
|
|
|
|
|
2021-09-30 23:48:56 +09:30
|
|
|
}
|