mirror of
https://github.com/pikami/tiktok-dl.git
synced 2025-12-21 09:49:51 +00:00
Compare commits
15 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
feee0a9154 | ||
|
|
af7972685e | ||
|
|
f9d35e3bf2 | ||
|
|
9a65746fd4 | ||
|
|
70c605a696 | ||
|
|
208bffb846 | ||
|
|
7b9b7688a1 | ||
|
|
e77c904f89 | ||
|
|
68612282ee | ||
|
|
7a691ad32d | ||
|
|
b6bb470064 | ||
|
|
f724f0f2a2 | ||
|
|
1b3f985f42 | ||
|
|
673bbe1340 | ||
|
|
2af96e899e |
2
.github/workflows/go.yml
vendored
2
.github/workflows/go.yml
vendored
@@ -1,5 +1,5 @@
|
|||||||
name: tiktok-dl_CI
|
name: tiktok-dl_CI
|
||||||
on: [push]
|
on: [push, pull_request]
|
||||||
jobs:
|
jobs:
|
||||||
build:
|
build:
|
||||||
strategy:
|
strategy:
|
||||||
|
|||||||
1
.gitignore
vendored
1
.gitignore
vendored
@@ -5,3 +5,4 @@ downloads
|
|||||||
*.exe
|
*.exe
|
||||||
tiktok-dl
|
tiktok-dl
|
||||||
batch_file.txt
|
batch_file.txt
|
||||||
|
debug.log
|
||||||
|
|||||||
11
README.md
11
README.md
@@ -16,10 +16,15 @@ You can download items listed in a text file by running `./tiktok-dl [OPTIONS] -
|
|||||||
Clone this repository and run `go build` to build the executable.
|
Clone this repository and run `go build` to build the executable.
|
||||||
|
|
||||||
## Available options
|
## Available options
|
||||||
* `-debug` - enables debug mode
|
* `-archive` - Download only videos not listed in the archive file. Record the IDs of all downloaded videos in it.
|
||||||
* `-output some_directory` - Output path (default "./downloads")
|
|
||||||
* `-metadata` - Write video metadata to a .json file
|
|
||||||
* `-batch-file` - File containing URLs/Usernames to download, one value per line. Lines starting with '#', are considered as comments and ignored.
|
* `-batch-file` - File containing URLs/Usernames to download, one value per line. Lines starting with '#', are considered as comments and ignored.
|
||||||
|
* `-deadline` - Sets the timout for scraper logic in seconds (used as a workaround for context deadline exceeded error) (default 1500)
|
||||||
|
* `-debug` - enables debug mode
|
||||||
|
* `-json` - Returns whole data, that was scraped from TikTok, in json
|
||||||
|
* `-limit` - Sets the max count of video that will be downloaded (default infinity)
|
||||||
|
* `-metadata` - Write video metadata to a .json file
|
||||||
|
* `-output some_directory` - Output path (default "./downloads")
|
||||||
|
* `-quiet` - Supress output
|
||||||
|
|
||||||
## Acknowledgments
|
## Acknowledgments
|
||||||
This software uses the **chromedp** for web scraping, it can be found here: https://github.com/chromedp/chromedp \
|
This software uses the **chromedp** for web scraping, it can be found here: https://github.com/chromedp/chromedp \
|
||||||
|
|||||||
115
client/executeClientAction.go
Normal file
115
client/executeClientAction.go
Normal file
@@ -0,0 +1,115 @@
|
|||||||
|
package client
|
||||||
|
|
||||||
|
import (
|
||||||
|
"context"
|
||||||
|
"errors"
|
||||||
|
"io/ioutil"
|
||||||
|
"os"
|
||||||
|
"strings"
|
||||||
|
"time"
|
||||||
|
|
||||||
|
"github.com/chromedp/chromedp"
|
||||||
|
|
||||||
|
config "../models/config"
|
||||||
|
utils "../utils"
|
||||||
|
log "../utils/log"
|
||||||
|
)
|
||||||
|
|
||||||
|
// GetMusicUploads - Get all uploads by given music
|
||||||
|
func executeClientAction(url string, jsAction string) (string, error) {
|
||||||
|
dir, err := ioutil.TempDir("", "chromedp-example")
|
||||||
|
if err != nil {
|
||||||
|
return "", err
|
||||||
|
}
|
||||||
|
defer os.RemoveAll(dir)
|
||||||
|
|
||||||
|
opts := append(chromedp.DefaultExecAllocatorOptions[:],
|
||||||
|
chromedp.DisableGPU,
|
||||||
|
chromedp.UserDataDir(dir),
|
||||||
|
chromedp.Flag("headless", !config.Config.Debug),
|
||||||
|
)
|
||||||
|
|
||||||
|
allocCtx, cancel := chromedp.NewExecAllocator(context.Background(), opts...)
|
||||||
|
defer cancel()
|
||||||
|
|
||||||
|
ctx, cancel := chromedp.NewContext(
|
||||||
|
allocCtx,
|
||||||
|
chromedp.WithLogf(log.Logf),
|
||||||
|
)
|
||||||
|
defer cancel()
|
||||||
|
|
||||||
|
ctx, cancel = context.WithTimeout(ctx, time.Duration(config.Config.Deadline)*time.Second)
|
||||||
|
defer cancel()
|
||||||
|
|
||||||
|
jsOutput, err := runScrapeWithInfo(ctx, jsAction, url)
|
||||||
|
if strings.HasPrefix(jsOutput, "\"ERR:") {
|
||||||
|
err = errors.New(jsOutput)
|
||||||
|
}
|
||||||
|
return jsOutput, err
|
||||||
|
}
|
||||||
|
|
||||||
|
func runScrapeQuiet(ctx context.Context, jsAction string, url string) (string, error) {
|
||||||
|
var jsOutput string
|
||||||
|
if err := chromedp.Run(ctx,
|
||||||
|
// Navigate to user's page
|
||||||
|
chromedp.Navigate(url),
|
||||||
|
// Execute url grabber script
|
||||||
|
chromedp.EvaluateAsDevTools(utils.ReadFileAsString("scraper.js"), &jsOutput),
|
||||||
|
chromedp.EvaluateAsDevTools(jsAction, &jsOutput),
|
||||||
|
// Wait until custom js finishes
|
||||||
|
chromedp.WaitVisible(`video_urls`),
|
||||||
|
// Grab url links from our element
|
||||||
|
chromedp.InnerHTML(`video_urls`, &jsOutput),
|
||||||
|
); err != nil {
|
||||||
|
return "", err
|
||||||
|
}
|
||||||
|
|
||||||
|
return jsOutput, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func runScrapeWithInfo(ctx context.Context, jsAction string, url string) (string, error) {
|
||||||
|
var jsOutput string
|
||||||
|
if err := chromedp.Run(ctx,
|
||||||
|
// Navigate to user's page
|
||||||
|
chromedp.Navigate(url),
|
||||||
|
// Execute url grabber script
|
||||||
|
chromedp.EvaluateAsDevTools(utils.ReadFileAsString("scraper.js"), &jsOutput),
|
||||||
|
chromedp.EvaluateAsDevTools(jsAction, &jsOutput),
|
||||||
|
); err != nil {
|
||||||
|
return "", err
|
||||||
|
}
|
||||||
|
|
||||||
|
for {
|
||||||
|
if err := chromedp.Run(ctx, chromedp.EvaluateAsDevTools("currentState.preloadCount.toString()", &jsOutput)); err != nil {
|
||||||
|
return "", err
|
||||||
|
}
|
||||||
|
|
||||||
|
if jsOutput != "0" {
|
||||||
|
log.Logf("\rPreloading... %s items have been found.", jsOutput)
|
||||||
|
} else {
|
||||||
|
log.Logf("\rPreloading...")
|
||||||
|
}
|
||||||
|
|
||||||
|
if err := chromedp.Run(ctx, chromedp.EvaluateAsDevTools("currentState.finished.toString()", &jsOutput)); err != nil {
|
||||||
|
return "", err
|
||||||
|
}
|
||||||
|
|
||||||
|
if jsOutput == "true" {
|
||||||
|
break
|
||||||
|
}
|
||||||
|
|
||||||
|
time.Sleep(50 * time.Millisecond)
|
||||||
|
}
|
||||||
|
|
||||||
|
log.Log("\nRetrieving items...")
|
||||||
|
if err := chromedp.Run(ctx,
|
||||||
|
// Wait until custom js finishes
|
||||||
|
chromedp.WaitVisible(`video_urls`),
|
||||||
|
// Grab url links from our element
|
||||||
|
chromedp.InnerHTML(`video_urls`, &jsOutput),
|
||||||
|
); err != nil {
|
||||||
|
return "", err
|
||||||
|
}
|
||||||
|
|
||||||
|
return jsOutput, nil
|
||||||
|
}
|
||||||
28
client/getHashtagUploads.go
Normal file
28
client/getHashtagUploads.go
Normal file
@@ -0,0 +1,28 @@
|
|||||||
|
package client
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
|
||||||
|
models "../models"
|
||||||
|
config "../models/config"
|
||||||
|
)
|
||||||
|
|
||||||
|
// GetHashtagUploads - Get all uploads marked with given hashtag
|
||||||
|
func GetHashtagUploads(hashtagURL string) ([]models.Upload, error) {
|
||||||
|
actionOutput, err := GetHashtagUploadsJSON(hashtagURL)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
return models.ParseUploads(actionOutput), nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// GetHashtagUploadsJSON - Get hashtag uploads scrape
|
||||||
|
func GetHashtagUploadsJSON(hashtagURL string) (string, error) {
|
||||||
|
jsMethod := fmt.Sprintf("bootstrapIteratingVideos(%d)", config.Config.Limit)
|
||||||
|
actionOutput, err := executeClientAction(hashtagURL, jsMethod)
|
||||||
|
if err != nil {
|
||||||
|
return "", err
|
||||||
|
}
|
||||||
|
return actionOutput, nil
|
||||||
|
}
|
||||||
@@ -1,58 +1,27 @@
|
|||||||
package client
|
package client
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
"fmt"
|
||||||
"github.com/chromedp/chromedp"
|
|
||||||
"io/ioutil"
|
|
||||||
"log"
|
|
||||||
"os"
|
|
||||||
"time"
|
|
||||||
|
|
||||||
models "../models"
|
models "../models"
|
||||||
utils "../utils"
|
config "../models/config"
|
||||||
)
|
)
|
||||||
|
|
||||||
// GetMusicUploads - Get all uploads by given music
|
// GetMusicUploads - Get all uploads by given music
|
||||||
func GetMusicUploads(url string) []models.Upload {
|
func GetMusicUploads(url string) ([]models.Upload, error) {
|
||||||
dir, err := ioutil.TempDir("", "chromedp-example")
|
actionOutput, err := GetMusicUploadsJSON(url)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
panic(err)
|
return nil, err
|
||||||
}
|
}
|
||||||
defer os.RemoveAll(dir)
|
return models.ParseUploads(actionOutput), nil
|
||||||
|
}
|
||||||
opts := append(chromedp.DefaultExecAllocatorOptions[:],
|
|
||||||
chromedp.DisableGPU,
|
// GetMusicUploadsJSON - Get music uploads scrape
|
||||||
chromedp.UserDataDir(dir),
|
func GetMusicUploadsJSON(url string) (string, error) {
|
||||||
chromedp.Flag("headless", !models.Config.Debug),
|
jsMethod := fmt.Sprintf("bootstrapIteratingVideos(%d)", config.Config.Limit)
|
||||||
)
|
actionOutput, err := executeClientAction(url, jsMethod)
|
||||||
|
if err != nil {
|
||||||
allocCtx, cancel := chromedp.NewExecAllocator(context.Background(), opts...)
|
return "", err
|
||||||
defer cancel()
|
}
|
||||||
|
return actionOutput, nil
|
||||||
ctx, cancel := chromedp.NewContext(
|
|
||||||
allocCtx,
|
|
||||||
chromedp.WithLogf(log.Printf),
|
|
||||||
)
|
|
||||||
defer cancel()
|
|
||||||
|
|
||||||
ctx, cancel = context.WithTimeout(ctx, 1500*time.Second)
|
|
||||||
defer cancel()
|
|
||||||
|
|
||||||
var jsOutput string
|
|
||||||
err = chromedp.Run(ctx,
|
|
||||||
// Navigate to user's page
|
|
||||||
chromedp.Navigate(url),
|
|
||||||
// Execute url grabber script
|
|
||||||
chromedp.EvaluateAsDevTools(utils.ReadFileAsString("scraper.js"), &jsOutput),
|
|
||||||
chromedp.EvaluateAsDevTools("bootstrapIteratingVideos()", &jsOutput),
|
|
||||||
// Wait until custom js finishes
|
|
||||||
chromedp.WaitVisible(`video_urls`),
|
|
||||||
// Grab url links from our element
|
|
||||||
chromedp.InnerHTML(`video_urls`, &jsOutput),
|
|
||||||
)
|
|
||||||
if err != nil {
|
|
||||||
log.Fatal(err)
|
|
||||||
}
|
|
||||||
|
|
||||||
return models.ParseUploads(jsOutput)
|
|
||||||
}
|
}
|
||||||
|
|||||||
52
client/getRedirectUrl.go
Normal file
52
client/getRedirectUrl.go
Normal file
@@ -0,0 +1,52 @@
|
|||||||
|
package client
|
||||||
|
|
||||||
|
import (
|
||||||
|
"context"
|
||||||
|
"github.com/chromedp/chromedp"
|
||||||
|
"io/ioutil"
|
||||||
|
"os"
|
||||||
|
"time"
|
||||||
|
|
||||||
|
config "../models/config"
|
||||||
|
log "../utils/log"
|
||||||
|
)
|
||||||
|
|
||||||
|
func GetRedirectUrl(url string) (string, error) {
|
||||||
|
dir, err := ioutil.TempDir("", "chromedp-example")
|
||||||
|
if err != nil {
|
||||||
|
return "", err
|
||||||
|
}
|
||||||
|
defer os.RemoveAll(dir)
|
||||||
|
|
||||||
|
opts := append(chromedp.DefaultExecAllocatorOptions[:],
|
||||||
|
chromedp.DisableGPU,
|
||||||
|
chromedp.UserDataDir(dir),
|
||||||
|
chromedp.Flag("headless", !config.Config.Debug),
|
||||||
|
)
|
||||||
|
|
||||||
|
allocCtx, cancel := chromedp.NewExecAllocator(context.Background(), opts...)
|
||||||
|
defer cancel()
|
||||||
|
|
||||||
|
ctx, cancel := chromedp.NewContext(
|
||||||
|
allocCtx,
|
||||||
|
chromedp.WithLogf(log.Logf),
|
||||||
|
)
|
||||||
|
defer cancel()
|
||||||
|
|
||||||
|
ctx, cancel = context.WithTimeout(ctx, time.Duration(config.Config.Deadline)*time.Second)
|
||||||
|
defer cancel()
|
||||||
|
|
||||||
|
var jsOutput string
|
||||||
|
if err := chromedp.Run(ctx,
|
||||||
|
// Navigate to user's page
|
||||||
|
chromedp.Navigate(url),
|
||||||
|
// Wait until page loads
|
||||||
|
chromedp.WaitReady(`div`),
|
||||||
|
// Grab url links from our element
|
||||||
|
chromedp.EvaluateAsDevTools(`window.location.href`, &jsOutput),
|
||||||
|
); err != nil {
|
||||||
|
return "", err
|
||||||
|
}
|
||||||
|
|
||||||
|
return jsOutput, err
|
||||||
|
}
|
||||||
@@ -1,58 +1,27 @@
|
|||||||
package client
|
package client
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
"fmt"
|
||||||
"github.com/chromedp/chromedp"
|
|
||||||
"io/ioutil"
|
|
||||||
"log"
|
|
||||||
"os"
|
|
||||||
"time"
|
|
||||||
|
|
||||||
models "../models"
|
models "../models"
|
||||||
utils "../utils"
|
config "../models/config"
|
||||||
)
|
)
|
||||||
|
|
||||||
// GetUserUploads - Get all uploads by user
|
// GetUserUploads - Get all uploads by user
|
||||||
func GetUserUploads(username string) []models.Upload {
|
func GetUserUploads(username string) ([]models.Upload, error) {
|
||||||
dir, err := ioutil.TempDir("", "chromedp-example")
|
actionOutput, err := GetUserUploadsJSON(username)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
panic(err)
|
return nil, err
|
||||||
}
|
}
|
||||||
defer os.RemoveAll(dir)
|
return models.ParseUploads(actionOutput), nil
|
||||||
|
}
|
||||||
opts := append(chromedp.DefaultExecAllocatorOptions[:],
|
|
||||||
chromedp.DisableGPU,
|
// GetUserUploadsJSON - Get user uploads scrape
|
||||||
chromedp.UserDataDir(dir),
|
func GetUserUploadsJSON(username string) (string, error) {
|
||||||
chromedp.Flag("headless", !models.Config.Debug),
|
jsMethod := fmt.Sprintf("bootstrapIteratingVideos(%d)", config.Config.Limit)
|
||||||
)
|
actionOutput, err := executeClientAction(`https://www.tiktok.com/@`+username, jsMethod)
|
||||||
|
if err != nil {
|
||||||
allocCtx, cancel := chromedp.NewExecAllocator(context.Background(), opts...)
|
return "", err
|
||||||
defer cancel()
|
}
|
||||||
|
return actionOutput, nil
|
||||||
ctx, cancel := chromedp.NewContext(
|
|
||||||
allocCtx,
|
|
||||||
chromedp.WithLogf(log.Printf),
|
|
||||||
)
|
|
||||||
defer cancel()
|
|
||||||
|
|
||||||
ctx, cancel = context.WithTimeout(ctx, 1500*time.Second)
|
|
||||||
defer cancel()
|
|
||||||
|
|
||||||
var jsOutput string
|
|
||||||
err = chromedp.Run(ctx,
|
|
||||||
// Navigate to user's page
|
|
||||||
chromedp.Navigate(`https://www.tiktok.com/@`+username),
|
|
||||||
// Execute url grabber script
|
|
||||||
chromedp.EvaluateAsDevTools(utils.ReadFileAsString("scraper.js"), &jsOutput),
|
|
||||||
chromedp.EvaluateAsDevTools("bootstrapIteratingVideos()", &jsOutput),
|
|
||||||
// Wait until custom js finishes
|
|
||||||
chromedp.WaitVisible(`video_urls`),
|
|
||||||
// Grab url links from our element
|
|
||||||
chromedp.InnerHTML(`video_urls`, &jsOutput),
|
|
||||||
)
|
|
||||||
if err != nil {
|
|
||||||
log.Fatal(err)
|
|
||||||
}
|
|
||||||
|
|
||||||
return models.ParseUploads(jsOutput)
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,58 +1,14 @@
|
|||||||
package client
|
package client
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
|
||||||
"github.com/chromedp/chromedp"
|
|
||||||
"io/ioutil"
|
|
||||||
"log"
|
|
||||||
"os"
|
|
||||||
"time"
|
|
||||||
|
|
||||||
models "../models"
|
models "../models"
|
||||||
utils "../utils"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
// GetVideoDetails - returns details of video
|
// GetVideoDetails - returns details of video
|
||||||
func GetVideoDetails(videoURL string) models.Upload {
|
func GetVideoDetails(videoURL string) (models.Upload, error) {
|
||||||
dir, err := ioutil.TempDir("", "chromedp-example")
|
actionOutput, err := executeClientAction(videoURL, "bootstrapGetCurrentVideo()")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
panic(err)
|
return models.Upload{}, err
|
||||||
}
|
}
|
||||||
defer os.RemoveAll(dir)
|
return models.ParseUpload(actionOutput), nil
|
||||||
|
|
||||||
opts := append(chromedp.DefaultExecAllocatorOptions[:],
|
|
||||||
chromedp.DisableGPU,
|
|
||||||
chromedp.UserDataDir(dir),
|
|
||||||
chromedp.Flag("headless", !models.Config.Debug),
|
|
||||||
)
|
|
||||||
|
|
||||||
allocCtx, cancel := chromedp.NewExecAllocator(context.Background(), opts...)
|
|
||||||
defer cancel()
|
|
||||||
|
|
||||||
ctx, cancel := chromedp.NewContext(
|
|
||||||
allocCtx,
|
|
||||||
chromedp.WithLogf(log.Printf),
|
|
||||||
)
|
|
||||||
defer cancel()
|
|
||||||
|
|
||||||
ctx, cancel = context.WithTimeout(ctx, 1500*time.Second)
|
|
||||||
defer cancel()
|
|
||||||
|
|
||||||
var jsOutput string
|
|
||||||
err = chromedp.Run(ctx,
|
|
||||||
// Navigate to user's page
|
|
||||||
chromedp.Navigate(videoURL),
|
|
||||||
// Execute url grabber script
|
|
||||||
chromedp.EvaluateAsDevTools(utils.ReadFileAsString("scraper.js"), &jsOutput),
|
|
||||||
chromedp.EvaluateAsDevTools("bootstrapGetCurrentVideo()", &jsOutput),
|
|
||||||
// Wait until custom js finishes
|
|
||||||
chromedp.WaitVisible(`video_urls`),
|
|
||||||
// Grab url links from our element
|
|
||||||
chromedp.InnerHTML(`video_urls`, &jsOutput),
|
|
||||||
)
|
|
||||||
if err != nil {
|
|
||||||
log.Fatal(err)
|
|
||||||
}
|
|
||||||
|
|
||||||
return models.ParseUpload(jsOutput)
|
|
||||||
}
|
}
|
||||||
|
|||||||
8
main.go
8
main.go
@@ -1,14 +1,14 @@
|
|||||||
package main
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
models "./models"
|
config "./models/config"
|
||||||
workflows "./workflows"
|
workflows "./workflows"
|
||||||
)
|
)
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
models.GetConfig()
|
config.GetConfig()
|
||||||
url := models.Config.URL
|
url := config.Config.URL
|
||||||
batchFilePath := models.Config.BatchFilePath
|
batchFilePath := config.Config.BatchFilePath
|
||||||
|
|
||||||
// Batch file
|
// Batch file
|
||||||
if workflows.CanUseDownloadBatchFile(batchFilePath) {
|
if workflows.CanUseDownloadBatchFile(batchFilePath) {
|
||||||
|
|||||||
@@ -1,28 +1,36 @@
|
|||||||
package models
|
package config
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"flag"
|
"flag"
|
||||||
"fmt"
|
"fmt"
|
||||||
"os"
|
"os"
|
||||||
"regexp"
|
|
||||||
"strings"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
// Config - Runtime configuration
|
// Config - Runtime configuration
|
||||||
var Config struct {
|
var Config struct {
|
||||||
URL string
|
URL string
|
||||||
OutputPath string
|
OutputPath string
|
||||||
BatchFilePath string
|
BatchFilePath string
|
||||||
Debug bool
|
ArchiveFilePath string
|
||||||
MetaData bool
|
Debug bool
|
||||||
|
MetaData bool
|
||||||
|
Quiet bool
|
||||||
|
JSONOnly bool
|
||||||
|
Deadline int
|
||||||
|
Limit int
|
||||||
}
|
}
|
||||||
|
|
||||||
// GetConfig - Returns Config object
|
// GetConfig - Returns Config object
|
||||||
func GetConfig() {
|
func GetConfig() {
|
||||||
outputPath := flag.String("output", "./downloads", "Output path")
|
outputPath := flag.String("output", "./downloads", "Output path")
|
||||||
batchFilePath := flag.String("batch-file", "", "File containing URLs/Usernames to download, one value per line. Lines starting with '#', are considered as comments and ignored.")
|
batchFilePath := flag.String("batch-file", "", "File containing URLs/Usernames to download, one value per line. Lines starting with '#', are considered as comments and ignored.")
|
||||||
|
archive := flag.String("archive", "", "Download only videos not listed in the archive file. Record the IDs of all downloaded videos in it.")
|
||||||
debug := flag.Bool("debug", false, "Enables debug mode")
|
debug := flag.Bool("debug", false, "Enables debug mode")
|
||||||
metadata := flag.Bool("metadata", false, "Write video metadata to a .json file")
|
metadata := flag.Bool("metadata", false, "Write video metadata to a .json file")
|
||||||
|
quiet := flag.Bool("quiet", false, "Supress output")
|
||||||
|
jsonOnly := flag.Bool("json", false, "Just get JSON data from scraper (without video downloading)")
|
||||||
|
deadline := flag.Int("deadline", 1500, "Sets the timout for scraper logic in seconds (used as a workaround for 'context deadline exceeded' error)")
|
||||||
|
limit := flag.Int("limit", 0, "Sets the videos count limit (useful when there too many videos from the user or by hashtag)")
|
||||||
flag.Parse()
|
flag.Parse()
|
||||||
|
|
||||||
args := flag.Args()
|
args := flag.Args()
|
||||||
@@ -39,25 +47,14 @@ func GetConfig() {
|
|||||||
}
|
}
|
||||||
Config.OutputPath = *outputPath
|
Config.OutputPath = *outputPath
|
||||||
Config.BatchFilePath = *batchFilePath
|
Config.BatchFilePath = *batchFilePath
|
||||||
|
Config.ArchiveFilePath = *archive
|
||||||
Config.Debug = *debug
|
Config.Debug = *debug
|
||||||
Config.MetaData = *metadata
|
Config.MetaData = *metadata
|
||||||
}
|
Config.Quiet = *quiet
|
||||||
|
if *jsonOnly {
|
||||||
// GetUsername - Get's username from passed URL param
|
Config.Quiet = true
|
||||||
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)
|
|
||||||
}
|
}
|
||||||
|
Config.JSONOnly = *jsonOnly
|
||||||
if match, _ := regexp.MatchString(".+tiktok\\.com/@.+", str); match { // URL
|
Config.Deadline = *deadline
|
||||||
stripedSuffix := strings.Split(str, "@")[1]
|
Config.Limit = *limit
|
||||||
return strings.Split(stripedSuffix, "/")[0]
|
|
||||||
}
|
|
||||||
|
|
||||||
panic("Could not recognise URL format")
|
|
||||||
}
|
}
|
||||||
@@ -2,9 +2,12 @@ package models
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"fmt"
|
|
||||||
"os"
|
"os"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
|
res "../resources"
|
||||||
|
checkErr "../utils/checkErr"
|
||||||
|
log "../utils/log"
|
||||||
)
|
)
|
||||||
|
|
||||||
// Upload - Upload object
|
// Upload - Upload object
|
||||||
@@ -46,21 +49,16 @@ func (u Upload) GetUploadID() string {
|
|||||||
func (u Upload) WriteToFile(outputPath string) {
|
func (u Upload) WriteToFile(outputPath string) {
|
||||||
bytes, err := json.Marshal(u)
|
bytes, err := json.Marshal(u)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
fmt.Printf("Could not serialize json for video: %s", u.GetUploadID())
|
log.Logf(res.ErrorCouldNotSerializeJSON, u.GetUploadID())
|
||||||
fmt.Println()
|
|
||||||
panic(err)
|
panic(err)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Create the file
|
// Create the file
|
||||||
out, err := os.Create(outputPath)
|
out, err := os.Create(outputPath)
|
||||||
if err != nil {
|
checkErr.CheckErr(err)
|
||||||
panic(err)
|
|
||||||
}
|
|
||||||
defer out.Close()
|
defer out.Close()
|
||||||
|
|
||||||
// Write to file
|
// Write to file
|
||||||
_, err = out.Write(bytes)
|
_, err = out.Write(bytes)
|
||||||
if err != nil {
|
checkErr.CheckErr(err)
|
||||||
panic(err)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,10 +1,11 @@
|
|||||||
package models
|
package models
|
||||||
|
|
||||||
import (
|
import (
|
||||||
testUtil "../unitTestUtil"
|
|
||||||
utils "../utils"
|
|
||||||
"os"
|
"os"
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
|
testUtil "../unitTestUtil"
|
||||||
|
fileio "../utils/fileio"
|
||||||
)
|
)
|
||||||
|
|
||||||
func TestParseUploads(t *testing.T) {
|
func TestParseUploads(t *testing.T) {
|
||||||
@@ -62,7 +63,7 @@ func TestWriteToFile(t *testing.T) {
|
|||||||
|
|
||||||
upload.WriteToFile(filePath)
|
upload.WriteToFile(filePath)
|
||||||
|
|
||||||
actual := utils.ReadFileToString(filePath)
|
actual := fileio.ReadFileToString(filePath)
|
||||||
tu.AssertString(actual, expected, "File content")
|
tu.AssertString(actual, expected, "File content")
|
||||||
|
|
||||||
os.Remove(filePath)
|
os.Remove(filePath)
|
||||||
|
|||||||
@@ -3,7 +3,7 @@
|
|||||||
"version": "0.0.1",
|
"version": "0.0.1",
|
||||||
"scripts": {
|
"scripts": {
|
||||||
"install-dependencies": "go get -v -t -d ./...",
|
"install-dependencies": "go get -v -t -d ./...",
|
||||||
"test": "go test -v ./models",
|
"test": "go test -v ./models && go test -v ./utils",
|
||||||
"clean": "rm -rf out",
|
"clean": "rm -rf out",
|
||||||
"build:scraper": "node node_modules/terser/bin/terser -c -m -- scraper.js > out/scraper.js",
|
"build:scraper": "node node_modules/terser/bin/terser -c -m -- scraper.js > out/scraper.js",
|
||||||
"build:app": "go build -o out/ -v .",
|
"build:app": "go build -o out/ -v .",
|
||||||
|
|||||||
13
resources/strings.go
Normal file
13
resources/strings.go
Normal file
@@ -0,0 +1,13 @@
|
|||||||
|
package resources
|
||||||
|
|
||||||
|
// ErrorCouldNotSerializeJSON -
|
||||||
|
var ErrorCouldNotSerializeJSON = "Could not serialize json for video: %s\n"
|
||||||
|
|
||||||
|
// ErrorCouldNotRecogniseURL -
|
||||||
|
var ErrorCouldNotRecogniseURL = "Could not recognise URL format of string %s"
|
||||||
|
|
||||||
|
// ErrorCouldNotGetUserUploads -
|
||||||
|
var ErrorCouldNotGetUserUploads = "Failed to get user uploads: %s\n"
|
||||||
|
|
||||||
|
// ErrorPathNotFound -
|
||||||
|
var ErrorPathNotFound = "File path %s not found."
|
||||||
61
scraper.js
61
scraper.js
@@ -1,7 +1,7 @@
|
|||||||
optStrings = {
|
optStrings = {
|
||||||
selectors: {
|
selectors: {
|
||||||
feedLoading: 'div.tiktok-loading.feed-loading',
|
feedLoading: 'div.tiktok-loading.feed-loading',
|
||||||
modalArrowLeft: 'div.video-card-modal > div > img.arrow-right',
|
modalArrowRight: 'div.video-card-modal > div > img.arrow-right',
|
||||||
modalClose: '.video-card-modal > div > div.close',
|
modalClose: '.video-card-modal > div > div.close',
|
||||||
modalPlayer: 'div > div > main > div.video-card-modal > div > div.video-card-big > div.video-card-container > div > div > video',
|
modalPlayer: 'div > div > main > div.video-card-modal > div > div.video-card-big > div.video-card-container > div > div > video',
|
||||||
modalShareInput: '.copy-link-container > input',
|
modalShareInput: '.copy-link-container > input',
|
||||||
@@ -17,6 +17,7 @@ optStrings = {
|
|||||||
classes: {
|
classes: {
|
||||||
feedVideoItem: 'video-feed-item-wrapper',
|
feedVideoItem: 'video-feed-item-wrapper',
|
||||||
modalCloseDisabled: 'disabled',
|
modalCloseDisabled: 'disabled',
|
||||||
|
titleMessage: 'title',
|
||||||
},
|
},
|
||||||
tags: {
|
tags: {
|
||||||
resultTag: 'video_urls',
|
resultTag: 'video_urls',
|
||||||
@@ -25,13 +26,38 @@ optStrings = {
|
|||||||
attributes: {
|
attributes: {
|
||||||
src: "src",
|
src: "src",
|
||||||
},
|
},
|
||||||
|
tiktokMessages: [
|
||||||
|
"Couldn't find this account",
|
||||||
|
"No videos yet",
|
||||||
|
"Video currently unavailable",
|
||||||
|
],
|
||||||
|
};
|
||||||
|
|
||||||
|
currentState = {
|
||||||
|
preloadCount: 0,
|
||||||
|
finished: false,
|
||||||
|
limit: 0
|
||||||
|
};
|
||||||
|
|
||||||
|
checkForErrors = function() {
|
||||||
|
var titles = document.getElementsByClassName(optStrings.classes.titleMessage);
|
||||||
|
debugger;
|
||||||
|
if (titles && titles.length) {
|
||||||
|
var error = Array.from(titles).find(x => optStrings.tiktokMessages.includes(x.textContent)).textContent;
|
||||||
|
if (error) {
|
||||||
|
createVidUrlElement("ERR: " + error);
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return false;
|
||||||
};
|
};
|
||||||
|
|
||||||
createVidUrlElement = function(outputObj) {
|
createVidUrlElement = function(outputObj) {
|
||||||
var urlSetElement = document.createElement(optStrings.tags.resultTag);
|
var urlSetElement = document.createElement(optStrings.tags.resultTag);
|
||||||
urlSetElement.innerText = JSON.stringify(outputObj);
|
urlSetElement.innerText = JSON.stringify(outputObj);
|
||||||
document.getElementsByTagName(optStrings.tags.resultParentTag)[0].appendChild(urlSetElement);
|
document.getElementsByTagName(optStrings.tags.resultParentTag)[0].appendChild(urlSetElement);
|
||||||
}
|
currentState.finished = true;
|
||||||
|
};
|
||||||
|
|
||||||
buldVidUrlArray = function(finishCallback) {
|
buldVidUrlArray = function(finishCallback) {
|
||||||
var feedItem = document.getElementsByClassName(optStrings.classes.feedVideoItem)[0];
|
var feedItem = document.getElementsByClassName(optStrings.classes.feedVideoItem)[0];
|
||||||
@@ -40,8 +66,14 @@ buldVidUrlArray = function(finishCallback) {
|
|||||||
var videoArray = [];
|
var videoArray = [];
|
||||||
var intervalID = window.setInterval(x => {
|
var intervalID = window.setInterval(x => {
|
||||||
videoArray.push(getCurrentModalVideo());
|
videoArray.push(getCurrentModalVideo());
|
||||||
|
if(currentState.limit > 0) {
|
||||||
var arrowRight = document.querySelectorAll(optStrings.selectors.modalArrowLeft)[0];
|
if (videoArray.length >= currentState.limit) {
|
||||||
|
window.clearInterval(intervalID);
|
||||||
|
document.querySelector(optStrings.selectors.modalClose).click();
|
||||||
|
finishCallback(videoArray);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
var arrowRight = document.querySelectorAll(optStrings.selectors.modalArrowRight)[0];
|
||||||
if (arrowRight.classList.contains(optStrings.classes.modalCloseDisabled)) {
|
if (arrowRight.classList.contains(optStrings.classes.modalCloseDisabled)) {
|
||||||
window.clearInterval(intervalID);
|
window.clearInterval(intervalID);
|
||||||
document.querySelector(optStrings.selectors.modalClose).click();
|
document.querySelector(optStrings.selectors.modalClose).click();
|
||||||
@@ -72,9 +104,10 @@ getCurrentModalVideo = function() {
|
|||||||
link: soundHref,
|
link: soundHref,
|
||||||
},
|
},
|
||||||
};
|
};
|
||||||
}
|
};
|
||||||
|
|
||||||
getCurrentVideo = function() {
|
getCurrentVideo = function() {
|
||||||
|
if(checkForErrors()) return;
|
||||||
var player = document.querySelector(optStrings.selectors.videoPlayer);
|
var player = document.querySelector(optStrings.selectors.videoPlayer);
|
||||||
var vidUrl = player.getAttribute(optStrings.attributes.src);
|
var vidUrl = player.getAttribute(optStrings.attributes.src);
|
||||||
var shareLink = document.querySelector(optStrings.selectors.videoShareInput).value;
|
var shareLink = document.querySelector(optStrings.selectors.videoShareInput).value;
|
||||||
@@ -94,14 +127,25 @@ getCurrentVideo = function() {
|
|||||||
link: soundHref,
|
link: soundHref,
|
||||||
},
|
},
|
||||||
};
|
};
|
||||||
}
|
};
|
||||||
|
|
||||||
scrollWhileNew = function(finishCallback) {
|
scrollWhileNew = function(finishCallback) {
|
||||||
var state = { count: 0 };
|
var state = { count: 0 };
|
||||||
var intervalID = window.setInterval(x => {
|
var intervalID = window.setInterval(x => {
|
||||||
var oldCount = state.count;
|
var oldCount = state.count;
|
||||||
state.count = document.getElementsByClassName(optStrings.classes.feedVideoItem).length;
|
state.count = document.getElementsByClassName(optStrings.classes.feedVideoItem).length;
|
||||||
|
if(currentState.limit > 0) {
|
||||||
|
if (currentState.preloadCount >= currentState.limit || state.count >= currentState.limit) {
|
||||||
|
finishCallback(createVidUrlElement);
|
||||||
|
window.clearInterval(intervalID);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if(checkForErrors()) {
|
||||||
|
window.clearInterval(intervalID);
|
||||||
|
return;
|
||||||
|
}
|
||||||
if (oldCount !== state.count) {
|
if (oldCount !== state.count) {
|
||||||
|
currentState.preloadCount = state.count;
|
||||||
window.scrollTo(0, document.body.scrollHeight);
|
window.scrollTo(0, document.body.scrollHeight);
|
||||||
} else {
|
} else {
|
||||||
if (document.querySelector(optStrings.selectors.feedLoading)) {
|
if (document.querySelector(optStrings.selectors.feedLoading)) {
|
||||||
@@ -114,7 +158,8 @@ scrollWhileNew = function(finishCallback) {
|
|||||||
}, 1000);
|
}, 1000);
|
||||||
};
|
};
|
||||||
|
|
||||||
bootstrapIteratingVideos = function() {
|
bootstrapIteratingVideos = function(limit) {
|
||||||
|
currentState.limit = limit;
|
||||||
scrollWhileNew(buldVidUrlArray);
|
scrollWhileNew(buldVidUrlArray);
|
||||||
return 'bootstrapIteratingVideos';
|
return 'bootstrapIteratingVideos';
|
||||||
};
|
};
|
||||||
@@ -123,7 +168,7 @@ bootstrapGetCurrentVideo = function() {
|
|||||||
var video = getCurrentVideo();
|
var video = getCurrentVideo();
|
||||||
createVidUrlElement(video);
|
createVidUrlElement(video);
|
||||||
return 'bootstrapGetCurrentVideo';
|
return 'bootstrapGetCurrentVideo';
|
||||||
}
|
};
|
||||||
|
|
||||||
init = () => {
|
init = () => {
|
||||||
const newProto = navigator.__proto__;
|
const newProto = navigator.__proto__;
|
||||||
|
|||||||
54
utils/archive.go
Normal file
54
utils/archive.go
Normal file
@@ -0,0 +1,54 @@
|
|||||||
|
package utils
|
||||||
|
|
||||||
|
import (
|
||||||
|
models "../models"
|
||||||
|
config "../models/config"
|
||||||
|
fileio "./fileio"
|
||||||
|
log "./log"
|
||||||
|
)
|
||||||
|
|
||||||
|
// IsItemInArchive - Checks if the item is already archived
|
||||||
|
func IsItemInArchive(upload models.Upload) bool {
|
||||||
|
if len(RemoveArchivedItems([]models.Upload{upload})) == 0 {
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
|
||||||
|
// RemoveArchivedItems - Returns items slice without archived items
|
||||||
|
func RemoveArchivedItems(uploads []models.Upload) []models.Upload {
|
||||||
|
archiveFilePath := config.Config.ArchiveFilePath
|
||||||
|
|
||||||
|
if archiveFilePath == "" || !fileio.CheckIfExists(archiveFilePath) {
|
||||||
|
return uploads
|
||||||
|
}
|
||||||
|
|
||||||
|
removeArchivedItemsDelegate := func(archivedItem string) {
|
||||||
|
for i, upload := range uploads {
|
||||||
|
if upload.GetUploadID() == archivedItem {
|
||||||
|
uploads = append(uploads[:i], uploads[i+1:]...)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
lenBeforeRemoval := len(uploads)
|
||||||
|
fileio.ReadFileLineByLine(archiveFilePath, removeArchivedItemsDelegate)
|
||||||
|
|
||||||
|
removedCount := lenBeforeRemoval - len(uploads)
|
||||||
|
if removedCount > 0 {
|
||||||
|
log.Logf("%d items, found in archive. Skipping...\n", removedCount)
|
||||||
|
}
|
||||||
|
|
||||||
|
return uploads
|
||||||
|
}
|
||||||
|
|
||||||
|
// AddItemToArchive - Adds item to archived list
|
||||||
|
func AddItemToArchive(uploadID string) {
|
||||||
|
archiveFilePath := config.Config.ArchiveFilePath
|
||||||
|
|
||||||
|
if archiveFilePath == "" {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
fileio.AppendToFile(uploadID, archiveFilePath)
|
||||||
|
}
|
||||||
12
utils/checkErr/checkErr.go
Normal file
12
utils/checkErr/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)
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -4,28 +4,23 @@ import (
|
|||||||
"io"
|
"io"
|
||||||
"net/http"
|
"net/http"
|
||||||
"os"
|
"os"
|
||||||
|
|
||||||
|
checkErr "./checkErr"
|
||||||
)
|
)
|
||||||
|
|
||||||
// DownloadFile - Downloads content from `url` and stores it in `outputPath`
|
// DownloadFile - Downloads content from `url` and stores it in `outputPath`
|
||||||
func DownloadFile(outputPath string, url string) {
|
func DownloadFile(outputPath string, url string) {
|
||||||
// Get the data
|
// Get the data
|
||||||
resp, err := http.Get(url)
|
resp, err := http.Get(url)
|
||||||
if err != nil {
|
checkErr.CheckErr(err)
|
||||||
panic(err)
|
|
||||||
}
|
|
||||||
defer resp.Body.Close()
|
defer resp.Body.Close()
|
||||||
|
|
||||||
// Create the file
|
// Create the file
|
||||||
out, err := os.Create(outputPath)
|
out, err := os.Create(outputPath)
|
||||||
if err != nil {
|
checkErr.CheckErr(err)
|
||||||
panic(err)
|
|
||||||
}
|
|
||||||
defer out.Close()
|
defer out.Close()
|
||||||
|
|
||||||
// Write the body to file
|
// Write the body to file
|
||||||
_, err = io.Copy(out, resp.Body)
|
_, err = io.Copy(out, resp.Body)
|
||||||
|
checkErr.CheckErr(err)
|
||||||
if err != nil {
|
|
||||||
panic(err)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -4,6 +4,8 @@ import (
|
|||||||
"bufio"
|
"bufio"
|
||||||
"io/ioutil"
|
"io/ioutil"
|
||||||
"os"
|
"os"
|
||||||
|
|
||||||
|
checkErr "../checkErr"
|
||||||
)
|
)
|
||||||
|
|
||||||
type delegateString func(string)
|
type delegateString func(string)
|
||||||
@@ -37,9 +39,7 @@ func ReadFileToString(path string) string {
|
|||||||
// ReadFileLineByLine - Reads file line by line and calls delegate
|
// ReadFileLineByLine - Reads file line by line and calls delegate
|
||||||
func ReadFileLineByLine(path string, delegate delegateString) {
|
func ReadFileLineByLine(path string, delegate delegateString) {
|
||||||
file, err := os.Open(path)
|
file, err := os.Open(path)
|
||||||
if err != nil {
|
checkErr.CheckErr(err)
|
||||||
panic(err)
|
|
||||||
}
|
|
||||||
defer file.Close()
|
defer file.Close()
|
||||||
|
|
||||||
scanner := bufio.NewScanner(file)
|
scanner := bufio.NewScanner(file)
|
||||||
@@ -51,3 +51,14 @@ func ReadFileLineByLine(path string, delegate delegateString) {
|
|||||||
panic(err)
|
panic(err)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// AppendToFile - Appends line to file
|
||||||
|
func AppendToFile(str string, filePath string) {
|
||||||
|
f, err := os.OpenFile(filePath, os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0644)
|
||||||
|
checkErr.CheckErr(err)
|
||||||
|
|
||||||
|
defer f.Close()
|
||||||
|
if _, err := f.WriteString(str + "\n"); err != nil {
|
||||||
|
checkErr.CheckErr(err)
|
||||||
|
}
|
||||||
|
}
|
||||||
16
utils/getHashtag.go
Normal file
16
utils/getHashtag.go
Normal file
@@ -0,0 +1,16 @@
|
|||||||
|
package utils
|
||||||
|
|
||||||
|
import (
|
||||||
|
res "../resources"
|
||||||
|
"fmt"
|
||||||
|
"strings"
|
||||||
|
)
|
||||||
|
|
||||||
|
// GetHashtagFromURL - Get's tag name from passed url
|
||||||
|
func GetHashtagFromURL(str string) string {
|
||||||
|
if match := strings.Contains(str, "/tag/"); match {
|
||||||
|
return strings.Split(str, "/tag/")[1]
|
||||||
|
}
|
||||||
|
|
||||||
|
panic(fmt.Sprintf(res.ErrorCouldNotRecogniseURL, str))
|
||||||
|
}
|
||||||
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))
|
||||||
|
}
|
||||||
@@ -1,6 +1,7 @@
|
|||||||
package models
|
package utils
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
config "../models/config"
|
||||||
testUtil "../unitTestUtil"
|
testUtil "../unitTestUtil"
|
||||||
"testing"
|
"testing"
|
||||||
)
|
)
|
||||||
@@ -8,7 +9,7 @@ import (
|
|||||||
func TestGetUsername(t *testing.T) {
|
func TestGetUsername(t *testing.T) {
|
||||||
testCaseDelegate := func(t *testing.T, url string, username string) {
|
testCaseDelegate := func(t *testing.T, url string, username string) {
|
||||||
tu := testUtil.TestUtil{T: t}
|
tu := testUtil.TestUtil{T: t}
|
||||||
Config.URL = url
|
config.Config.URL = url
|
||||||
actual := GetUsername()
|
actual := GetUsername()
|
||||||
tu.AssertString(actual, username, "Username")
|
tu.AssertString(actual, username, "Username")
|
||||||
}
|
}
|
||||||
32
utils/log/log.go
Normal file
32
utils/log/log.go
Normal file
@@ -0,0 +1,32 @@
|
|||||||
|
package utils
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"os"
|
||||||
|
|
||||||
|
config "../../models/config"
|
||||||
|
)
|
||||||
|
|
||||||
|
// 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...))
|
||||||
|
}
|
||||||
|
|
||||||
|
// LogErr - Write error
|
||||||
|
func LogErr(format string, a ...interface{}) {
|
||||||
|
fmt.Fprintf(os.Stderr, format, a...)
|
||||||
|
}
|
||||||
@@ -2,14 +2,13 @@ package utils
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"io/ioutil"
|
"io/ioutil"
|
||||||
"log"
|
|
||||||
|
checkErr "./checkErr"
|
||||||
)
|
)
|
||||||
|
|
||||||
// ReadFileAsString - Returns contents of given file
|
// ReadFileAsString - Returns contents of given file
|
||||||
func ReadFileAsString(fileName string) string {
|
func ReadFileAsString(fileName string) string {
|
||||||
content, err := ioutil.ReadFile(fileName)
|
content, err := ioutil.ReadFile(fileName)
|
||||||
if err != nil {
|
checkErr.CheckErr(err)
|
||||||
log.Fatal(err)
|
|
||||||
}
|
|
||||||
return string(content)
|
return string(content)
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,8 +1,9 @@
|
|||||||
package workflows
|
package workflows
|
||||||
|
|
||||||
import (
|
import (
|
||||||
utils "../utils"
|
res "../resources"
|
||||||
"fmt"
|
fileio "../utils/fileio"
|
||||||
|
log "../utils/log"
|
||||||
)
|
)
|
||||||
|
|
||||||
// CanUseDownloadBatchFile - Check's if DownloadBatchFile can be used
|
// CanUseDownloadBatchFile - Check's if DownloadBatchFile can be used
|
||||||
@@ -12,11 +13,11 @@ func CanUseDownloadBatchFile(batchFilePath string) bool {
|
|||||||
|
|
||||||
// DownloadBatchFile - Download items from batch file
|
// DownloadBatchFile - Download items from batch file
|
||||||
func DownloadBatchFile(batchFilePath string) {
|
func DownloadBatchFile(batchFilePath string) {
|
||||||
if !utils.CheckIfExists(batchFilePath) {
|
if !fileio.CheckIfExists(batchFilePath) {
|
||||||
panic(fmt.Sprintf("File path %s not found.", batchFilePath))
|
log.LogFatal(res.ErrorPathNotFound, batchFilePath)
|
||||||
}
|
}
|
||||||
|
|
||||||
utils.ReadFileLineByLine(batchFilePath, downloadItem)
|
fileio.ReadFileLineByLine(batchFilePath, downloadItem)
|
||||||
}
|
}
|
||||||
|
|
||||||
func downloadItem(batchItem string) {
|
func downloadItem(batchItem string) {
|
||||||
|
|||||||
52
workflows/downloadHashtag.go
Normal file
52
workflows/downloadHashtag.go
Normal file
@@ -0,0 +1,52 @@
|
|||||||
|
package workflows
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"strings"
|
||||||
|
|
||||||
|
client "../client"
|
||||||
|
config "../models/config"
|
||||||
|
res "../resources"
|
||||||
|
utils "../utils"
|
||||||
|
fileio "../utils/fileio"
|
||||||
|
log "../utils/log"
|
||||||
|
)
|
||||||
|
|
||||||
|
// CanUseDownloadHashtag - Test's if this workflow can be used for parameter
|
||||||
|
func CanUseDownloadHashtag(url string) bool {
|
||||||
|
match := strings.Contains(url, "/tag/")
|
||||||
|
return match
|
||||||
|
}
|
||||||
|
|
||||||
|
// DownloadHashtag - Download videos marked with given hashtag
|
||||||
|
func DownloadHashtag(url string) {
|
||||||
|
uploads, err := client.GetHashtagUploads(url)
|
||||||
|
if err != nil {
|
||||||
|
log.LogErr(res.ErrorCouldNotGetUserUploads, err.Error())
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
uploads = utils.RemoveArchivedItems(uploads)
|
||||||
|
uploadCount := len(uploads)
|
||||||
|
|
||||||
|
hashtag := utils.GetHashtagFromURL(url)
|
||||||
|
downloadDir := fmt.Sprintf("%s/%s", config.Config.OutputPath, hashtag)
|
||||||
|
|
||||||
|
fileio.InitOutputDirectory(downloadDir)
|
||||||
|
|
||||||
|
for index, upload := range uploads {
|
||||||
|
downloadVideo(upload, downloadDir)
|
||||||
|
log.Logf("\r[%d/%d] Downloaded", index+1, uploadCount)
|
||||||
|
}
|
||||||
|
log.Log()
|
||||||
|
}
|
||||||
|
|
||||||
|
// GetHashtagJSON - Prints scraped info from hashtag
|
||||||
|
func GetHashtagJSON(url string) {
|
||||||
|
uploads, err := client.GetHashtagUploadsJSON(url)
|
||||||
|
if err != nil {
|
||||||
|
log.LogErr(res.ErrorCouldNotGetUserUploads, err.Error())
|
||||||
|
return
|
||||||
|
}
|
||||||
|
fmt.Printf("%s", uploads)
|
||||||
|
}
|
||||||
@@ -1,11 +1,15 @@
|
|||||||
package workflows
|
package workflows
|
||||||
|
|
||||||
import (
|
import (
|
||||||
client "../client"
|
|
||||||
models "../models"
|
|
||||||
utils "../utils"
|
|
||||||
"fmt"
|
"fmt"
|
||||||
"regexp"
|
"regexp"
|
||||||
|
|
||||||
|
client "../client"
|
||||||
|
config "../models/config"
|
||||||
|
res "../resources"
|
||||||
|
utils "../utils"
|
||||||
|
fileio "../utils/fileio"
|
||||||
|
log "../utils/log"
|
||||||
)
|
)
|
||||||
|
|
||||||
// CanUseDownloadMusic - Check's if DownloadMusic can be used for parameter
|
// CanUseDownloadMusic - Check's if DownloadMusic can be used for parameter
|
||||||
@@ -16,13 +20,32 @@ func CanUseDownloadMusic(url string) bool {
|
|||||||
|
|
||||||
// DownloadMusic - Download all videos by given music
|
// DownloadMusic - Download all videos by given music
|
||||||
func DownloadMusic(url string) {
|
func DownloadMusic(url string) {
|
||||||
uploads := client.GetMusicUploads(url)
|
uploads, err := client.GetMusicUploads(url)
|
||||||
|
if err != nil {
|
||||||
for _, upload := range uploads {
|
log.LogErr(res.ErrorCouldNotGetUserUploads, err.Error())
|
||||||
username := models.GetUsernameFromString(upload.Uploader)
|
return
|
||||||
downloadDir := fmt.Sprintf("%s/%s", models.Config.OutputPath, username)
|
|
||||||
|
|
||||||
utils.InitOutputDirectory(downloadDir)
|
|
||||||
downloadVideo(upload, downloadDir)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
uploads = utils.RemoveArchivedItems(uploads)
|
||||||
|
uploadCount := len(uploads)
|
||||||
|
|
||||||
|
for index, upload := range uploads {
|
||||||
|
username := utils.GetUsernameFromString(upload.Uploader)
|
||||||
|
downloadDir := fmt.Sprintf("%s/%s", config.Config.OutputPath, username)
|
||||||
|
|
||||||
|
fileio.InitOutputDirectory(downloadDir)
|
||||||
|
downloadVideo(upload, downloadDir)
|
||||||
|
log.Logf("\r[%d/%d] Downloaded", index+1, uploadCount)
|
||||||
|
}
|
||||||
|
log.Log()
|
||||||
|
}
|
||||||
|
|
||||||
|
// GetMusicJSON - Prints scraped info from music
|
||||||
|
func GetMusicJSON(url string) {
|
||||||
|
uploads, err := client.GetMusicUploadsJSON(url)
|
||||||
|
if err != nil {
|
||||||
|
log.LogErr(res.ErrorCouldNotGetUserUploads, err.Error())
|
||||||
|
return
|
||||||
|
}
|
||||||
|
fmt.Printf("%s", uploads)
|
||||||
}
|
}
|
||||||
|
|||||||
27
workflows/downloadShareLink.go
Normal file
27
workflows/downloadShareLink.go
Normal file
@@ -0,0 +1,27 @@
|
|||||||
|
package workflows
|
||||||
|
|
||||||
|
import (
|
||||||
|
client "../client"
|
||||||
|
res "../resources"
|
||||||
|
log "../utils/log"
|
||||||
|
"regexp"
|
||||||
|
)
|
||||||
|
|
||||||
|
// CanUseDownloadShareLink - Check's if DownloadShareLink can be used
|
||||||
|
func CanUseDownloadShareLink(url string) bool {
|
||||||
|
match, _ := regexp.MatchString("vm.tiktok.com\\/.+", url)
|
||||||
|
return match
|
||||||
|
}
|
||||||
|
|
||||||
|
// DownloadShareLink - Download item by share link
|
||||||
|
func DownloadShareLink(url string) {
|
||||||
|
log.Logf("Resolving share link: %s\n", url)
|
||||||
|
|
||||||
|
finalURL, err := client.GetRedirectUrl(url)
|
||||||
|
if err != nil {
|
||||||
|
log.LogErr(res.ErrorCouldNotGetUserUploads, err.Error())
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
StartWorkflowByParameter(finalURL)
|
||||||
|
}
|
||||||
@@ -1,27 +1,53 @@
|
|||||||
package workflows
|
package workflows
|
||||||
|
|
||||||
import (
|
import (
|
||||||
client "../client"
|
|
||||||
models "../models"
|
|
||||||
utils "../utils"
|
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"regexp"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
|
client "../client"
|
||||||
|
config "../models/config"
|
||||||
|
res "../resources"
|
||||||
|
utils "../utils"
|
||||||
|
fileio "../utils/fileio"
|
||||||
|
log "../utils/log"
|
||||||
)
|
)
|
||||||
|
|
||||||
// CanUseDownloadUser - Test's if this workflow can be used for parameter
|
// CanUseDownloadUser - Test's if this workflow can be used for parameter
|
||||||
func CanUseDownloadUser(url string) bool {
|
func CanUseDownloadUser(url string) bool {
|
||||||
match := strings.Contains(url, "/")
|
isURL := strings.Contains(url, "/")
|
||||||
return !match
|
match, _ := regexp.MatchString(".+com\\/@[^\\/]+", url)
|
||||||
|
return !isURL || match
|
||||||
}
|
}
|
||||||
|
|
||||||
// DownloadUser - Download all user's videos
|
// DownloadUser - Download all user's videos
|
||||||
func DownloadUser(username string) {
|
func DownloadUser(username string) {
|
||||||
downloadDir := fmt.Sprintf("%s/%s", models.Config.OutputPath, username)
|
uploads, err := client.GetUserUploads(username)
|
||||||
uploads := client.GetUserUploads(username)
|
if err != nil {
|
||||||
|
log.LogErr(res.ErrorCouldNotGetUserUploads, err.Error())
|
||||||
utils.InitOutputDirectory(downloadDir)
|
return
|
||||||
|
|
||||||
for _, upload := range uploads {
|
|
||||||
downloadVideo(upload, downloadDir)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
uploads = utils.RemoveArchivedItems(uploads)
|
||||||
|
uploadCount := len(uploads)
|
||||||
|
|
||||||
|
downloadDir := fmt.Sprintf("%s/%s", config.Config.OutputPath, username)
|
||||||
|
|
||||||
|
fileio.InitOutputDirectory(downloadDir)
|
||||||
|
|
||||||
|
for index, upload := range uploads {
|
||||||
|
downloadVideo(upload, downloadDir)
|
||||||
|
log.Logf("\r[%d/%d] Downloaded", index+1, uploadCount)
|
||||||
|
}
|
||||||
|
log.Log()
|
||||||
|
}
|
||||||
|
|
||||||
|
// GetUserVideosJSON - Prints scraped info from user
|
||||||
|
func GetUserVideosJSON(username string) {
|
||||||
|
uploads, err := client.GetUserUploadsJSON(username)
|
||||||
|
if err != nil {
|
||||||
|
log.LogErr(res.ErrorCouldNotGetUserUploads, err.Error())
|
||||||
|
return
|
||||||
|
}
|
||||||
|
fmt.Printf("%s", uploads)
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,11 +1,16 @@
|
|||||||
package workflows
|
package workflows
|
||||||
|
|
||||||
import (
|
import (
|
||||||
client "../client"
|
|
||||||
models "../models"
|
|
||||||
utils "../utils"
|
|
||||||
"fmt"
|
"fmt"
|
||||||
"regexp"
|
"regexp"
|
||||||
|
|
||||||
|
client "../client"
|
||||||
|
models "../models"
|
||||||
|
config "../models/config"
|
||||||
|
res "../resources"
|
||||||
|
utils "../utils"
|
||||||
|
fileio "../utils/fileio"
|
||||||
|
log "../utils/log"
|
||||||
)
|
)
|
||||||
|
|
||||||
// CanUseDownloadSingleVideo - Check's if DownloadSingleVideo can be used for parameter
|
// CanUseDownloadSingleVideo - Check's if DownloadSingleVideo can be used for parameter
|
||||||
@@ -16,12 +21,21 @@ func CanUseDownloadSingleVideo(url string) bool {
|
|||||||
|
|
||||||
// DownloadSingleVideo - Downloads single video
|
// DownloadSingleVideo - Downloads single video
|
||||||
func DownloadSingleVideo(url string) {
|
func DownloadSingleVideo(url string) {
|
||||||
username := models.GetUsernameFromString(url)
|
username := utils.GetUsernameFromString(url)
|
||||||
upload := client.GetVideoDetails(url)
|
upload, err := client.GetVideoDetails(url)
|
||||||
downloadDir := fmt.Sprintf("%s/%s", models.Config.OutputPath, username)
|
if err != nil {
|
||||||
|
log.LogErr(res.ErrorCouldNotGetUserUploads, err.Error())
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
utils.InitOutputDirectory(downloadDir)
|
if utils.IsItemInArchive(upload) {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
downloadDir := fmt.Sprintf("%s/%s", config.Config.OutputPath, username)
|
||||||
|
|
||||||
|
fileio.InitOutputDirectory(downloadDir)
|
||||||
downloadVideo(upload, downloadDir)
|
downloadVideo(upload, downloadDir)
|
||||||
|
log.Log("[1/1] Downloaded\n")
|
||||||
}
|
}
|
||||||
|
|
||||||
// DownloadVideo - Downloads one video
|
// DownloadVideo - Downloads one video
|
||||||
@@ -29,16 +43,16 @@ func downloadVideo(upload models.Upload, downloadDir string) {
|
|||||||
uploadID := upload.GetUploadID()
|
uploadID := upload.GetUploadID()
|
||||||
downloadPath := fmt.Sprintf("%s/%s.mp4", downloadDir, uploadID)
|
downloadPath := fmt.Sprintf("%s/%s.mp4", downloadDir, uploadID)
|
||||||
|
|
||||||
if utils.CheckIfExists(downloadPath) {
|
if fileio.CheckIfExists(downloadPath) {
|
||||||
fmt.Println("Upload '" + uploadID + "' already downloaded, skipping")
|
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
fmt.Println("Downloading upload item '" + uploadID + "' to " + downloadPath)
|
|
||||||
utils.DownloadFile(downloadPath, upload.URL)
|
utils.DownloadFile(downloadPath, upload.URL)
|
||||||
|
|
||||||
if models.Config.MetaData {
|
if config.Config.MetaData {
|
||||||
metadataPath := fmt.Sprintf("%s/%s.json", downloadDir, uploadID)
|
metadataPath := fmt.Sprintf("%s/%s.json", downloadDir, uploadID)
|
||||||
upload.WriteToFile(metadataPath)
|
upload.WriteToFile(metadataPath)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
utils.AddItemToArchive(upload.GetUploadID())
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,8 +1,10 @@
|
|||||||
package workflows
|
package workflows
|
||||||
|
|
||||||
import (
|
import (
|
||||||
models "../models"
|
config "../models/config"
|
||||||
"fmt"
|
res "../resources"
|
||||||
|
utils "../utils"
|
||||||
|
log "../utils/log"
|
||||||
)
|
)
|
||||||
|
|
||||||
// StartWorkflowByParameter - Start needed workflow by given parameter
|
// StartWorkflowByParameter - Start needed workflow by given parameter
|
||||||
@@ -10,7 +12,11 @@ func StartWorkflowByParameter(url string) {
|
|||||||
|
|
||||||
// Music
|
// Music
|
||||||
if CanUseDownloadMusic(url) {
|
if CanUseDownloadMusic(url) {
|
||||||
DownloadMusic(url)
|
if config.Config.JSONOnly {
|
||||||
|
GetMusicJSON(url)
|
||||||
|
} else {
|
||||||
|
DownloadMusic(url)
|
||||||
|
}
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -22,9 +28,30 @@ func StartWorkflowByParameter(url string) {
|
|||||||
|
|
||||||
// Tiktok user
|
// Tiktok user
|
||||||
if CanUseDownloadUser(url) {
|
if CanUseDownloadUser(url) {
|
||||||
DownloadUser(models.GetUsername())
|
if config.Config.JSONOnly {
|
||||||
|
GetUserVideosJSON(utils.GetUsernameFromString(url))
|
||||||
|
} else {
|
||||||
|
DownloadUser(utils.GetUsernameFromString(url))
|
||||||
|
}
|
||||||
|
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
panic(fmt.Sprintf("Could not recognise URL format of string %s", url))
|
// Tiktok hashtag
|
||||||
|
if CanUseDownloadHashtag(url) {
|
||||||
|
if config.Config.JSONOnly {
|
||||||
|
GetHashtagJSON(url)
|
||||||
|
} else {
|
||||||
|
DownloadHashtag(url)
|
||||||
|
}
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
// Share URL
|
||||||
|
if CanUseDownloadShareLink(url) {
|
||||||
|
DownloadShareLink(url)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
log.LogFatal(res.ErrorCouldNotRecogniseURL, url)
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user