tiktok-dl/workflows/downloadVideo.go

59 lines
1.4 KiB
Go
Raw Permalink Normal View History

2020-01-21 21:46:30 +00:00
package workflows
import (
2020-03-22 00:10:24 +00:00
"fmt"
"regexp"
2020-01-21 21:46:30 +00:00
client "../client"
models "../models"
config "../models/config"
2020-02-25 18:12:01 +00:00
res "../resources"
2020-01-21 21:46:30 +00:00
utils "../utils"
2020-03-21 22:22:08 +00:00
fileio "../utils/fileio"
2020-03-22 00:10:24 +00:00
log "../utils/log"
2020-01-21 21:46:30 +00:00
)
// CanUseDownloadSingleVideo - Check's if DownloadSingleVideo can be used for parameter
func CanUseDownloadSingleVideo(url string) bool {
match, _ := regexp.MatchString("\\/@.+\\/video\\/[0-9]+", url)
return match
}
// DownloadSingleVideo - Downloads single video
func DownloadSingleVideo(url string) {
username := utils.GetUsernameFromString(url)
2020-02-25 18:12:01 +00:00
upload, err := client.GetVideoDetails(url)
if err != nil {
2020-03-22 00:10:24 +00:00
log.LogErr(res.ErrorCouldNotGetUserUploads, err.Error())
return
}
if utils.IsItemInArchive(upload) {
2020-02-25 18:12:01 +00:00
return
}
downloadDir := fmt.Sprintf("%s/%s", config.Config.OutputPath, username)
2020-01-21 21:46:30 +00:00
2020-03-21 22:22:08 +00:00
fileio.InitOutputDirectory(downloadDir)
2020-01-21 21:46:30 +00:00
downloadVideo(upload, downloadDir)
2020-03-22 00:10:24 +00:00
log.Log("[1/1] Downloaded\n")
2020-01-21 21:46:30 +00:00
}
// DownloadVideo - Downloads one video
func downloadVideo(upload models.Upload, downloadDir string) {
uploadID := upload.GetUploadID()
downloadPath := fmt.Sprintf("%s/%s.mp4", downloadDir, uploadID)
2020-03-21 22:22:08 +00:00
if fileio.CheckIfExists(downloadPath) {
2020-01-21 21:46:30 +00:00
return
}
utils.DownloadFile(downloadPath, upload.URL)
if config.Config.MetaData {
2020-01-21 21:46:30 +00:00
metadataPath := fmt.Sprintf("%s/%s.json", downloadDir, uploadID)
upload.WriteToFile(metadataPath)
}
2020-03-22 00:10:24 +00:00
utils.AddItemToArchive(upload.GetUploadID())
2020-01-21 21:46:30 +00:00
}