Improved status output

Added `-quiet` flag

Move out error messages to separate file
This commit is contained in:
Pijus Kamandulis
2020-02-08 01:51:17 +02:00
parent 673bbe1340
commit 1b3f985f42
21 changed files with 182 additions and 82 deletions

View File

@@ -1,11 +1,9 @@
package models
package config
import (
"flag"
"fmt"
"os"
"regexp"
"strings"
)
// Config - Runtime configuration
@@ -15,6 +13,7 @@ var Config struct {
BatchFilePath string
Debug bool
MetaData bool
Quiet bool
Deadline int
}
@@ -24,6 +23,7 @@ func GetConfig() {
batchFilePath := flag.String("batch-file", "", "File containing URLs/Usernames to download, one value per line. Lines starting with '#', are considered as comments and ignored.")
debug := flag.Bool("debug", false, "Enables debug mode")
metadata := flag.Bool("metadata", false, "Write video metadata to a .json file")
quiet := flag.Bool("quiet", false, "Supress output")
deadline := flag.Int("deadline", 1500, "Sets the timout for scraper logic in seconds (used as a workaround for 'context deadline exceeded' error)")
flag.Parse()
@@ -43,24 +43,6 @@ func GetConfig() {
Config.BatchFilePath = *batchFilePath
Config.Debug = *debug
Config.MetaData = *metadata
Config.Quiet = *quiet
Config.Deadline = *deadline
}
// GetUsername - Get's username from passed URL param
func GetUsername() string {
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)
}
if match, _ := regexp.MatchString(".+tiktok\\.com/@.+", str); match { // URL
stripedSuffix := strings.Split(str, "@")[1]
return strings.Split(stripedSuffix, "/")[0]
}
panic("Could not recognise URL format")
}

View File

@@ -1,36 +0,0 @@
package models
import (
testUtil "../unitTestUtil"
"testing"
)
func TestGetUsername(t *testing.T) {
testCaseDelegate := func(t *testing.T, url string, username string) {
tu := testUtil.TestUtil{T: t}
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)
}

View File

@@ -1,8 +1,9 @@
package models
import (
res "../resources"
utils "../utils"
"encoding/json"
"fmt"
"os"
"strings"
)
@@ -46,21 +47,16 @@ func (u Upload) GetUploadID() string {
func (u Upload) WriteToFile(outputPath string) {
bytes, err := json.Marshal(u)
if err != nil {
fmt.Printf("Could not serialize json for video: %s", u.GetUploadID())
fmt.Println()
utils.Logf(res.ErrorCouldNotSerializeJSON, u.GetUploadID())
panic(err)
}
// Create the file
out, err := os.Create(outputPath)
if err != nil {
panic(err)
}
utils.CheckErr(err)
defer out.Close()
// Write to file
_, err = out.Write(bytes)
if err != nil {
panic(err)
}
utils.CheckErr(err)
}