TTDL-5 Added better error handling

This commit is contained in:
Pijus Kamandulis
2020-02-25 20:12:01 +02:00
parent 1b3f985f42
commit 7a691ad32d
10 changed files with 113 additions and 41 deletions

View File

@@ -2,10 +2,12 @@ package client
import (
"context"
"errors"
"github.com/chromedp/chromedp"
"io/ioutil"
"log"
"os"
"strings"
"time"
config "../models/config"
@@ -13,9 +15,11 @@ import (
)
// GetMusicUploads - Get all uploads by given music
func executeClientAction(url string, jsAction string) string {
func executeClientAction(url string, jsAction string) (string, error) {
dir, err := ioutil.TempDir("", "chromedp-example")
utils.CheckErr(err)
if err != nil {
return "", err
}
defer os.RemoveAll(dir)
opts := append(chromedp.DefaultExecAllocatorOptions[:],
@@ -36,15 +40,16 @@ func executeClientAction(url string, jsAction string) string {
ctx, cancel = context.WithTimeout(ctx, time.Duration(config.Config.Deadline)*time.Second)
defer cancel()
var jsOutput string
jsOutput = runScrapeWithInfo(ctx, jsAction, url)
return jsOutput
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 {
func runScrapeQuiet(ctx context.Context, jsAction string, url string) (string, error) {
var jsOutput string
err := chromedp.Run(ctx,
if err := chromedp.Run(ctx,
// Navigate to user's page
chromedp.Navigate(url),
// Execute url grabber script
@@ -54,33 +59,40 @@ func runScrapeQuiet(ctx context.Context, jsAction string, url string) string {
chromedp.WaitVisible(`video_urls`),
// Grab url links from our element
chromedp.InnerHTML(`video_urls`, &jsOutput),
)
utils.CheckErr(err)
return jsOutput
); err != nil {
return "", err
}
return jsOutput, nil
}
func runScrapeWithInfo(ctx context.Context, jsAction string, url string) string {
func runScrapeWithInfo(ctx context.Context, jsAction string, url string) (string, error) {
var jsOutput string
err := chromedp.Run(ctx,
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),
)
utils.CheckErr(err)
); err != nil {
return "", err
}
for {
err = chromedp.Run(ctx, chromedp.EvaluateAsDevTools("currentState.preloadCount.toString()", &jsOutput))
utils.CheckErr(err)
if err := chromedp.Run(ctx, chromedp.EvaluateAsDevTools("currentState.preloadCount.toString()", &jsOutput)); err != nil {
return "", err
}
if jsOutput != "0" {
utils.Logf("\rPreloading... Currently loaded %s items.", jsOutput)
} else {
utils.Logf("\rPreloading...")
}
err = chromedp.Run(ctx, chromedp.EvaluateAsDevTools("currentState.finished.toString()", &jsOutput))
utils.CheckErr(err)
if err := chromedp.Run(ctx, chromedp.EvaluateAsDevTools("currentState.finished.toString()", &jsOutput)); err != nil {
return "", err
}
if jsOutput == "true" {
break
}
@@ -89,13 +101,14 @@ func runScrapeWithInfo(ctx context.Context, jsAction string, url string) string
}
utils.Log("\nRetrieving items...")
err = chromedp.Run(ctx,
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),
)
utils.CheckErr(err)
); err != nil {
return "", err
}
return jsOutput
return jsOutput, nil
}

View File

@@ -5,7 +5,10 @@ import (
)
// GetMusicUploads - Get all uploads by given music
func GetMusicUploads(url string) []models.Upload {
actionOutput := executeClientAction(url, "bootstrapIteratingVideos()")
return models.ParseUploads(actionOutput)
func GetMusicUploads(url string) ([]models.Upload, error) {
actionOutput, err := executeClientAction(url, "bootstrapIteratingVideos()")
if err != nil {
return nil, err
}
return models.ParseUploads(actionOutput), nil
}

View File

@@ -5,7 +5,10 @@ import (
)
// GetUserUploads - Get all uploads by user
func GetUserUploads(username string) []models.Upload {
actionOutput := executeClientAction(`https://www.tiktok.com/@`+username, "bootstrapIteratingVideos()")
return models.ParseUploads(actionOutput)
func GetUserUploads(username string) ([]models.Upload, error) {
actionOutput, err := executeClientAction(`https://www.tiktok.com/@`+username, "bootstrapIteratingVideos()")
if err != nil {
return nil, err
}
return models.ParseUploads(actionOutput), nil
}

View File

@@ -5,7 +5,10 @@ import (
)
// GetVideoDetails - returns details of video
func GetVideoDetails(videoURL string) models.Upload {
actionOutput := executeClientAction(videoURL, "bootstrapGetCurrentVideo()")
return models.ParseUpload(actionOutput)
func GetVideoDetails(videoURL string) (models.Upload, error) {
actionOutput, err := executeClientAction(videoURL, "bootstrapGetCurrentVideo()")
if err != nil {
return models.Upload{}, err
}
return models.ParseUpload(actionOutput), nil
}