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 {
2020-01-24 19:02:50 +02:00
URL string
OutputPath string
BatchFilePath string
Debug bool
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-24 19:02:50 +02:00
batchFilePath := flag . String ( "batch-file" , "" , "File containing URLs/Usernames to download, one value per line. Lines starting with '#', are considered as comments and ignored." )
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 ( )
2020-01-24 19:02:50 +02:00
if len ( args ) < 1 && * batchFilePath == "" {
2020-01-19 17:54:16 +02:00
fmt . Println ( "Usage: tiktok-dl [OPTIONS] TIKTOK_USERNAME|TIKTOK_URL" )
2020-01-24 19:02:50 +02:00
fmt . Println ( " or: tiktok-dl [OPTIONS] -batch-file path/to/users.txt" )
2020-01-19 04:11:53 +02:00
os . Exit ( 2 )
}
2020-01-24 19:02:50 +02:00
if len ( args ) > 0 {
Config . URL = flag . Args ( ) [ len ( args ) - 1 ]
} else {
Config . URL = ""
}
2020-01-19 04:11:53 +02:00
Config . OutputPath = * outputPath
2020-01-24 19:02:50 +02:00
Config . BatchFilePath = * batchFilePath
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 {
2020-01-24 19:02:50 +02:00
return GetUsernameFromString ( Config . URL )
}
// GetUsernameFromString - Get's username from passed param
func GetUsernameFromString ( str string ) string {
if match := strings . Contains ( str , "/" ) ; ! match { // Not url
return strings . Replace ( str , "@" , "" , - 1 )
2020-01-21 23:46:30 +02:00
}
2020-01-24 19:02:50 +02:00
if match , _ := regexp . MatchString ( ".+tiktok\\.com/@.+" , str ) ; match { // URL
stripedSuffix := strings . Split ( str , "@" ) [ 1 ]
2020-01-21 23:46:30 +02:00
return strings . Split ( stripedSuffix , "/" ) [ 0 ]
}
panic ( "Could not recognise URL format" )
}