mirror of
https://github.com/pikami/tiktok-dl.git
synced 2026-01-07 19:45:21 +00:00
Improved status output
Added `-quiet` flag Move out error messages to separate file
This commit is contained in:
12
utils/checkErr.go
Normal file
12
utils/checkErr.go
Normal file
@@ -0,0 +1,12 @@
|
||||
package utils
|
||||
|
||||
import (
|
||||
"log"
|
||||
)
|
||||
|
||||
// CheckErr - Checks if error and log
|
||||
func CheckErr(err error) {
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
}
|
||||
@@ -10,22 +10,15 @@ import (
|
||||
func DownloadFile(outputPath string, url string) {
|
||||
// Get the data
|
||||
resp, err := http.Get(url)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
CheckErr(err)
|
||||
defer resp.Body.Close()
|
||||
|
||||
// Create the file
|
||||
out, err := os.Create(outputPath)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
CheckErr(err)
|
||||
defer out.Close()
|
||||
|
||||
// Write the body to file
|
||||
_, err = io.Copy(out, resp.Body)
|
||||
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
CheckErr(err)
|
||||
}
|
||||
|
||||
@@ -37,9 +37,7 @@ func ReadFileToString(path string) string {
|
||||
// ReadFileLineByLine - Reads file line by line and calls delegate
|
||||
func ReadFileLineByLine(path string, delegate delegateString) {
|
||||
file, err := os.Open(path)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
CheckErr(err)
|
||||
defer file.Close()
|
||||
|
||||
scanner := bufio.NewScanner(file)
|
||||
|
||||
28
utils/getUsername.go
Normal file
28
utils/getUsername.go
Normal file
@@ -0,0 +1,28 @@
|
||||
package utils
|
||||
|
||||
import (
|
||||
config "../models/config"
|
||||
res "../resources"
|
||||
"fmt"
|
||||
"regexp"
|
||||
"strings"
|
||||
)
|
||||
|
||||
// GetUsername - Get's username from passed URL param
|
||||
func GetUsername() string {
|
||||
return GetUsernameFromString(config.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)
|
||||
}
|
||||
|
||||
if match, _ := regexp.MatchString(".+tiktok\\.com/@.+", str); match { // URL
|
||||
stripedSuffix := strings.Split(str, "@")[1]
|
||||
return strings.Split(stripedSuffix, "/")[0]
|
||||
}
|
||||
|
||||
panic(fmt.Sprintf(res.ErrorCouldNotRecogniseURL, str))
|
||||
}
|
||||
37
utils/getUsername_test.go
Normal file
37
utils/getUsername_test.go
Normal file
@@ -0,0 +1,37 @@
|
||||
package utils
|
||||
|
||||
import (
|
||||
config "../models/config"
|
||||
testUtil "../unitTestUtil"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestGetUsername(t *testing.T) {
|
||||
testCaseDelegate := func(t *testing.T, url string, username string) {
|
||||
tu := testUtil.TestUtil{T: t}
|
||||
config.Config.URL = url
|
||||
actual := GetUsername()
|
||||
tu.AssertString(actual, username, "Username")
|
||||
}
|
||||
|
||||
testVideoURL := func(t *testing.T) {
|
||||
testCaseDelegate(t, "https://www.tiktok.com/@some_username/video/0000000000000000000", "some_username")
|
||||
}
|
||||
|
||||
testProfileURL := func(t *testing.T) {
|
||||
testCaseDelegate(t, "https://www.tiktok.com/@some_username", "some_username")
|
||||
}
|
||||
|
||||
testPlainUsername := func(t *testing.T) {
|
||||
testCaseDelegate(t, "some_username", "some_username")
|
||||
}
|
||||
|
||||
testAtUsername := func(t *testing.T) {
|
||||
testCaseDelegate(t, "@some_username", "some_username")
|
||||
}
|
||||
|
||||
t.Run("Video URL", testVideoURL)
|
||||
t.Run("Username URL", testProfileURL)
|
||||
t.Run("Plain username", testPlainUsername)
|
||||
t.Run("Username with @ suffix", testAtUsername)
|
||||
}
|
||||
25
utils/log.go
Normal file
25
utils/log.go
Normal file
@@ -0,0 +1,25 @@
|
||||
package utils
|
||||
|
||||
import (
|
||||
config "../models/config"
|
||||
"fmt"
|
||||
)
|
||||
|
||||
// Log - Write to std out
|
||||
func Log(a ...interface{}) {
|
||||
if !config.Config.Quiet {
|
||||
fmt.Println(a...)
|
||||
}
|
||||
}
|
||||
|
||||
// Logf - Write formated text
|
||||
func Logf(format string, a ...interface{}) {
|
||||
if !config.Config.Quiet {
|
||||
fmt.Printf(format, a...)
|
||||
}
|
||||
}
|
||||
|
||||
// LogFatal - Write error and panic
|
||||
func LogFatal(format string, a ...interface{}) {
|
||||
panic(fmt.Sprintf(format, a...))
|
||||
}
|
||||
@@ -2,14 +2,11 @@ package utils
|
||||
|
||||
import (
|
||||
"io/ioutil"
|
||||
"log"
|
||||
)
|
||||
|
||||
// ReadFileAsString - Returns contents of given file
|
||||
func ReadFileAsString(fileName string) string {
|
||||
content, err := ioutil.ReadFile(fileName)
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
CheckErr(err)
|
||||
return string(content)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user