Initial commit

This commit is contained in:
Pijus Kamandulis
2020-01-19 04:11:53 +02:00
commit 69574dcb7e
10 changed files with 313 additions and 0 deletions

28
models/config.go Normal file
View File

@@ -0,0 +1,28 @@
package models
import (
"flag"
"fmt"
"os"
)
// Config - Runtime configuration
var Config struct {
UserName string
OutputPath string
}
// GetConfig - Returns Config object
func GetConfig() {
outputPath := flag.String("output", "./downloads", "Output path")
flag.Parse()
args := flag.Args()
if len(args) < 1 {
fmt.Println("Usage: tiktok-dl [OPTIONS] TIKTOK_USERNAME")
os.Exit(2)
}
Config.UserName = flag.Args()[len(args)-1]
Config.OutputPath = *outputPath
}

25
models/upload.go Normal file
View File

@@ -0,0 +1,25 @@
package models
import (
"encoding/json"
"strings"
)
// Upload - Upload object
type Upload struct {
ShareLink string `json:"shareLink"`
URL string `json:"url"`
}
// ParseUploads - Parses json uploads array
func ParseUploads(str string) []Upload {
var uploads []Upload
json.Unmarshal([]byte(str), &uploads)
return uploads
}
// GetUploadID - Returns upload id
func (u Upload) GetUploadID() string {
parts := strings.Split(u.ShareLink, "/")
return parts[len(parts)-1]
}

36
models/upload_test.go Normal file
View File

@@ -0,0 +1,36 @@
package models
import "testing"
// TestParseUploads - Test parsing
func TestParseUploads(t *testing.T) {
jsonStr := "[{\"shareLink\":\"some_share_link\", \"url\": \"some_url\"}]"
actual := ParseUploads(jsonStr)
expectedLen := 1
if len(actual) != expectedLen {
t.Errorf("Array len incorrect: Expected %d, but got %d", expectedLen, len(actual))
}
expectedShareLink := "some_share_link"
if actual[0].ShareLink != expectedShareLink {
t.Errorf("ShareLink is incorrect: Expected %s, but got %s", expectedShareLink, actual[0].ShareLink)
}
expectedURL := "some_url"
if actual[0].URL != expectedURL {
t.Errorf("URL is incorrect: Expected %s, but got %s", expectedURL, actual[0].URL)
}
}
func TestGetUploadID(t *testing.T) {
var upload Upload
upload.ShareLink = "http://pikami.org/some_thing/some_upload_id"
expected := "some_upload_id"
actual := upload.GetUploadID()
if actual != expected {
t.Errorf("UploadId is incorrect: Expected %s, but got %s", expected, actual)
}
}