tiktok-dl/models/config.go

51 lines
1.1 KiB
Go
Raw Normal View History

2020-01-19 04:11:53 +02:00
package models
import (
"flag"
"fmt"
"os"
2020-01-21 23:46:30 +02:00
"regexp"
"strings"
2020-01-19 04:11:53 +02:00
)
// Config - Runtime configuration
var Config struct {
URL string
2020-01-19 04:11:53 +02:00
OutputPath string
2020-01-19 16:43:32 +02:00
Debug bool
2020-01-20 19:42:34 +02:00
MetaData bool
2020-01-19 04:11:53 +02:00
}
// GetConfig - Returns Config object
func GetConfig() {
outputPath := flag.String("output", "./downloads", "Output path")
2020-01-20 19:42:34 +02:00
debug := flag.Bool("debug", false, "Enables debug mode")
metadata := flag.Bool("metadata", false, "Write video metadata to a .json file")
2020-01-19 04:11:53 +02:00
flag.Parse()
args := flag.Args()
if len(args) < 1 {
fmt.Println("Usage: tiktok-dl [OPTIONS] TIKTOK_USERNAME|TIKTOK_URL")
2020-01-19 04:11:53 +02:00
os.Exit(2)
}
Config.URL = flag.Args()[len(args)-1]
2020-01-19 04:11:53 +02:00
Config.OutputPath = *outputPath
2020-01-19 16:43:32 +02:00
Config.Debug = *debug
2020-01-20 19:42:34 +02:00
Config.MetaData = *metadata
2020-01-19 04:11:53 +02:00
}
2020-01-21 23:46:30 +02:00
// 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")
}