2020-01-19 02:11:53 +00:00
package models
import (
"flag"
"fmt"
"os"
2020-01-21 21:46:30 +00:00
"regexp"
"strings"
2020-01-19 02:11:53 +00:00
)
// Config - Runtime configuration
var Config struct {
2020-01-24 17:02:50 +00:00
URL string
OutputPath string
BatchFilePath string
Debug bool
MetaData bool
2020-01-30 16:59:34 +00:00
Deadline int
2020-01-19 02:11:53 +00:00
}
// GetConfig - Returns Config object
func GetConfig ( ) {
outputPath := flag . String ( "output" , "./downloads" , "Output path" )
2020-01-24 17:02:50 +00: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 17:42:34 +00:00
debug := flag . Bool ( "debug" , false , "Enables debug mode" )
metadata := flag . Bool ( "metadata" , false , "Write video metadata to a .json file" )
2020-01-30 16:59:34 +00:00
deadline := flag . Int ( "deadline" , 1500 , "Sets the timout for scraper logic in seconds (used as a workaround for 'context deadline exceeded' error)" )
2020-01-19 02:11:53 +00:00
flag . Parse ( )
args := flag . Args ( )
2020-01-24 17:02:50 +00:00
if len ( args ) < 1 && * batchFilePath == "" {
2020-01-19 15:54:16 +00:00
fmt . Println ( "Usage: tiktok-dl [OPTIONS] TIKTOK_USERNAME|TIKTOK_URL" )
2020-01-24 17:02:50 +00:00
fmt . Println ( " or: tiktok-dl [OPTIONS] -batch-file path/to/users.txt" )
2020-01-19 02:11:53 +00:00
os . Exit ( 2 )
}
2020-01-24 17:02:50 +00:00
if len ( args ) > 0 {
Config . URL = flag . Args ( ) [ len ( args ) - 1 ]
} else {
Config . URL = ""
}
2020-01-19 02:11:53 +00:00
Config . OutputPath = * outputPath
2020-01-24 17:02:50 +00:00
Config . BatchFilePath = * batchFilePath
2020-01-19 14:43:32 +00:00
Config . Debug = * debug
2020-01-20 17:42:34 +00:00
Config . MetaData = * metadata
2020-01-30 16:59:34 +00:00
Config . Deadline = * deadline
2020-01-19 02:11:53 +00:00
}
2020-01-21 21:46:30 +00:00
// GetUsername - Get's username from passed URL param
func GetUsername ( ) string {
2020-01-24 17:02:50 +00: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 21:46:30 +00:00
}
2020-01-24 17:02:50 +00:00
if match , _ := regexp . MatchString ( ".+tiktok\\.com/@.+" , str ) ; match { // URL
stripedSuffix := strings . Split ( str , "@" ) [ 1 ]
2020-01-21 21:46:30 +00:00
return strings . Split ( stripedSuffix , "/" ) [ 0 ]
}
panic ( "Could not recognise URL format" )
}