mirror of https://github.com/pikami/tiktok-dl.git
Improved parameter parsing
This commit is contained in:
parent
320e044f3c
commit
6e0e39ada2
49
main.go
49
main.go
|
@ -1,12 +1,8 @@
|
||||||
package main
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
client "./client"
|
|
||||||
models "./models"
|
models "./models"
|
||||||
utils "./utils"
|
workflows "./workflows"
|
||||||
"fmt"
|
|
||||||
"regexp"
|
|
||||||
"strings"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
|
@ -14,49 +10,16 @@ func main() {
|
||||||
url := models.Config.URL
|
url := models.Config.URL
|
||||||
|
|
||||||
// Single video
|
// Single video
|
||||||
match, _ := regexp.MatchString("\\/@.+\\/video\\/[0-9]+", url)
|
if workflows.CanUseDownloadSingleVideo(url) {
|
||||||
if match {
|
workflows.DownloadSingleVideo(url)
|
||||||
getUsernameFromVidURLRegex, _ := regexp.Compile("com\\/@.*")
|
|
||||||
parts := strings.Split(getUsernameFromVidURLRegex.FindString(url), "/")
|
|
||||||
username := parts[1][1:]
|
|
||||||
upload := client.GetVideoDetails(url)
|
|
||||||
downloadDir := fmt.Sprintf("%s/%s", models.Config.OutputPath, username)
|
|
||||||
|
|
||||||
utils.InitOutputDirectory(downloadDir)
|
|
||||||
downloadVideo(upload, downloadDir)
|
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
// Tiktok user
|
// Tiktok user
|
||||||
downloadUser()
|
if workflows.CanUseDownloadUser(url) {
|
||||||
}
|
workflows.DownloadUser(models.GetUsername())
|
||||||
|
|
||||||
func downloadVideo(upload models.Upload, downloadDir string) {
|
|
||||||
uploadID := upload.GetUploadID()
|
|
||||||
downloadPath := fmt.Sprintf("%s/%s.mp4", downloadDir, uploadID)
|
|
||||||
|
|
||||||
if utils.CheckIfExists(downloadPath) {
|
|
||||||
fmt.Println("Upload '" + uploadID + "' already downloaded, skipping")
|
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
fmt.Println("Downloading upload item '" + uploadID + "' to " + downloadPath)
|
panic("Could not recognise URL format")
|
||||||
utils.DownloadFile(downloadPath, upload.URL)
|
|
||||||
|
|
||||||
if models.Config.MetaData {
|
|
||||||
metadataPath := fmt.Sprintf("%s/%s.json", downloadDir, uploadID)
|
|
||||||
upload.WriteToFile(metadataPath)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func downloadUser() {
|
|
||||||
username := models.Config.URL
|
|
||||||
downloadDir := fmt.Sprintf("%s/%s", models.Config.OutputPath, username)
|
|
||||||
uploads := client.GetUserUploads(username)
|
|
||||||
|
|
||||||
utils.InitOutputDirectory(downloadDir)
|
|
||||||
|
|
||||||
for _, upload := range uploads {
|
|
||||||
downloadVideo(upload, downloadDir)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -4,6 +4,8 @@ import (
|
||||||
"flag"
|
"flag"
|
||||||
"fmt"
|
"fmt"
|
||||||
"os"
|
"os"
|
||||||
|
"regexp"
|
||||||
|
"strings"
|
||||||
)
|
)
|
||||||
|
|
||||||
// Config - Runtime configuration
|
// Config - Runtime configuration
|
||||||
|
@ -32,3 +34,17 @@ func GetConfig() {
|
||||||
Config.Debug = *debug
|
Config.Debug = *debug
|
||||||
Config.MetaData = *metadata
|
Config.MetaData = *metadata
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// GetUsername - Get's username from passed URL param
|
||||||
|
func GetUsername() string {
|
||||||
|
if match := strings.Contains(Config.URL, "/"); !match { // Not url
|
||||||
|
return strings.Replace(Config.URL, "@", "", -1)
|
||||||
|
}
|
||||||
|
|
||||||
|
if match, _ := regexp.MatchString(".+tiktok\\.com/@.+", Config.URL); match { // URL
|
||||||
|
stripedSuffix := strings.Split(Config.URL, "@")[1]
|
||||||
|
return strings.Split(stripedSuffix, "/")[0]
|
||||||
|
}
|
||||||
|
|
||||||
|
panic("Could not recognise URL format")
|
||||||
|
}
|
||||||
|
|
|
@ -0,0 +1,27 @@
|
||||||
|
package workflows
|
||||||
|
|
||||||
|
import (
|
||||||
|
client "../client"
|
||||||
|
models "../models"
|
||||||
|
utils "../utils"
|
||||||
|
"fmt"
|
||||||
|
"strings"
|
||||||
|
)
|
||||||
|
|
||||||
|
// CanUseDownloadUser - Test's if this workflow can be used for parameter
|
||||||
|
func CanUseDownloadUser(url string) bool {
|
||||||
|
match := strings.Contains(url, "/")
|
||||||
|
return !match
|
||||||
|
}
|
||||||
|
|
||||||
|
// DownloadUser - Download all user's videos
|
||||||
|
func DownloadUser(username string) {
|
||||||
|
downloadDir := fmt.Sprintf("%s/%s", models.Config.OutputPath, username)
|
||||||
|
uploads := client.GetUserUploads(username)
|
||||||
|
|
||||||
|
utils.InitOutputDirectory(downloadDir)
|
||||||
|
|
||||||
|
for _, upload := range uploads {
|
||||||
|
downloadVideo(upload, downloadDir)
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,44 @@
|
||||||
|
package workflows
|
||||||
|
|
||||||
|
import (
|
||||||
|
client "../client"
|
||||||
|
models "../models"
|
||||||
|
utils "../utils"
|
||||||
|
"fmt"
|
||||||
|
"regexp"
|
||||||
|
)
|
||||||
|
|
||||||
|
// 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 := models.GetUsername()
|
||||||
|
upload := client.GetVideoDetails(url)
|
||||||
|
downloadDir := fmt.Sprintf("%s/%s", models.Config.OutputPath, username)
|
||||||
|
|
||||||
|
utils.InitOutputDirectory(downloadDir)
|
||||||
|
downloadVideo(upload, downloadDir)
|
||||||
|
}
|
||||||
|
|
||||||
|
// DownloadVideo - Downloads one video
|
||||||
|
func downloadVideo(upload models.Upload, downloadDir string) {
|
||||||
|
uploadID := upload.GetUploadID()
|
||||||
|
downloadPath := fmt.Sprintf("%s/%s.mp4", downloadDir, uploadID)
|
||||||
|
|
||||||
|
if utils.CheckIfExists(downloadPath) {
|
||||||
|
fmt.Println("Upload '" + uploadID + "' already downloaded, skipping")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
fmt.Println("Downloading upload item '" + uploadID + "' to " + downloadPath)
|
||||||
|
utils.DownloadFile(downloadPath, upload.URL)
|
||||||
|
|
||||||
|
if models.Config.MetaData {
|
||||||
|
metadataPath := fmt.Sprintf("%s/%s.json", downloadDir, uploadID)
|
||||||
|
upload.WriteToFile(metadataPath)
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue