2020-01-30 16:59:34 +00:00
|
|
|
package client
|
|
|
|
|
|
|
|
import (
|
2020-02-24 23:01:10 +00:00
|
|
|
"context"
|
2020-02-25 18:12:01 +00:00
|
|
|
"errors"
|
2020-02-24 23:01:10 +00:00
|
|
|
"io/ioutil"
|
|
|
|
"os"
|
2020-02-25 18:12:01 +00:00
|
|
|
"strings"
|
2020-02-24 23:01:10 +00:00
|
|
|
"time"
|
|
|
|
|
2020-03-22 00:10:24 +00:00
|
|
|
"github.com/chromedp/chromedp"
|
|
|
|
|
2020-04-12 01:22:00 +01:00
|
|
|
config "github.com/pikami/tiktok-dl/models/config"
|
2020-04-12 14:56:50 +01:00
|
|
|
res "github.com/pikami/tiktok-dl/resources"
|
2020-04-12 01:22:00 +01:00
|
|
|
utils "github.com/pikami/tiktok-dl/utils"
|
|
|
|
log "github.com/pikami/tiktok-dl/utils/log"
|
2020-01-30 16:59:34 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// GetMusicUploads - Get all uploads by given music
|
2020-02-25 18:12:01 +00:00
|
|
|
func executeClientAction(url string, jsAction string) (string, error) {
|
2020-02-24 23:01:10 +00:00
|
|
|
dir, err := ioutil.TempDir("", "chromedp-example")
|
2020-02-25 18:12:01 +00:00
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
2020-02-24 23:01:10 +00:00
|
|
|
defer os.RemoveAll(dir)
|
2020-01-30 16:59:34 +00:00
|
|
|
|
2020-02-24 23:01:10 +00:00
|
|
|
opts := append(chromedp.DefaultExecAllocatorOptions[:],
|
|
|
|
chromedp.DisableGPU,
|
|
|
|
chromedp.UserDataDir(dir),
|
|
|
|
chromedp.Flag("headless", !config.Config.Debug),
|
|
|
|
)
|
2020-01-30 16:59:34 +00:00
|
|
|
|
2020-02-24 23:01:10 +00:00
|
|
|
allocCtx, cancel := chromedp.NewExecAllocator(context.Background(), opts...)
|
|
|
|
defer cancel()
|
2020-01-30 16:59:34 +00:00
|
|
|
|
2020-02-24 23:01:10 +00:00
|
|
|
ctx, cancel := chromedp.NewContext(
|
|
|
|
allocCtx,
|
2020-03-22 00:10:24 +00:00
|
|
|
chromedp.WithLogf(log.Logf),
|
2020-02-24 23:01:10 +00:00
|
|
|
)
|
|
|
|
defer cancel()
|
2020-01-30 16:59:34 +00:00
|
|
|
|
2020-02-24 23:01:10 +00:00
|
|
|
ctx, cancel = context.WithTimeout(ctx, time.Duration(config.Config.Deadline)*time.Second)
|
|
|
|
defer cancel()
|
2020-01-30 16:59:34 +00:00
|
|
|
|
2020-02-25 18:12:01 +00:00
|
|
|
jsOutput, err := runScrapeWithInfo(ctx, jsAction, url)
|
|
|
|
if strings.HasPrefix(jsOutput, "\"ERR:") {
|
|
|
|
err = errors.New(jsOutput)
|
|
|
|
}
|
|
|
|
return jsOutput, err
|
2020-02-07 23:51:17 +00:00
|
|
|
}
|
|
|
|
|
2020-02-25 18:12:01 +00:00
|
|
|
func runScrapeQuiet(ctx context.Context, jsAction string, url string) (string, error) {
|
2020-02-24 23:01:10 +00:00
|
|
|
var jsOutput string
|
2020-02-25 18:12:01 +00:00
|
|
|
if err := chromedp.Run(ctx,
|
2020-02-24 23:01:10 +00:00
|
|
|
// Navigate to user's page
|
|
|
|
chromedp.Navigate(url),
|
|
|
|
// Execute url grabber script
|
2020-04-12 14:56:50 +01:00
|
|
|
chromedp.EvaluateAsDevTools(utils.GetScraper(), &jsOutput),
|
2020-02-24 23:01:10 +00:00
|
|
|
chromedp.EvaluateAsDevTools(jsAction, &jsOutput),
|
|
|
|
// Wait until custom js finishes
|
|
|
|
chromedp.WaitVisible(`video_urls`),
|
|
|
|
// Grab url links from our element
|
|
|
|
chromedp.InnerHTML(`video_urls`, &jsOutput),
|
2020-02-25 18:12:01 +00:00
|
|
|
); err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
|
|
|
return jsOutput, nil
|
2020-02-07 23:51:17 +00:00
|
|
|
}
|
|
|
|
|
2020-02-25 18:12:01 +00:00
|
|
|
func runScrapeWithInfo(ctx context.Context, jsAction string, url string) (string, error) {
|
2020-02-24 23:01:10 +00:00
|
|
|
var jsOutput string
|
2020-02-25 18:12:01 +00:00
|
|
|
if err := chromedp.Run(ctx,
|
2020-02-24 23:01:10 +00:00
|
|
|
// Navigate to user's page
|
|
|
|
chromedp.Navigate(url),
|
|
|
|
// Execute url grabber script
|
2020-04-12 14:56:50 +01:00
|
|
|
chromedp.EvaluateAsDevTools(utils.GetScraper(), &jsOutput),
|
2020-02-24 23:01:10 +00:00
|
|
|
chromedp.EvaluateAsDevTools(jsAction, &jsOutput),
|
2020-02-25 18:12:01 +00:00
|
|
|
); err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
2020-02-24 23:01:10 +00:00
|
|
|
|
|
|
|
for {
|
2020-02-25 18:12:01 +00:00
|
|
|
if err := chromedp.Run(ctx, chromedp.EvaluateAsDevTools("currentState.preloadCount.toString()", &jsOutput)); err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
2020-02-24 23:01:10 +00:00
|
|
|
if jsOutput != "0" {
|
2020-04-12 14:56:50 +01:00
|
|
|
log.Logf(res.PreloadingItemsFound, jsOutput)
|
2020-02-24 23:01:10 +00:00
|
|
|
} else {
|
2020-04-12 14:56:50 +01:00
|
|
|
log.Logf(res.Preloading)
|
2020-02-24 23:01:10 +00:00
|
|
|
}
|
|
|
|
|
2020-02-25 18:12:01 +00:00
|
|
|
if err := chromedp.Run(ctx, chromedp.EvaluateAsDevTools("currentState.finished.toString()", &jsOutput)); err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
2020-02-24 23:01:10 +00:00
|
|
|
if jsOutput == "true" {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
|
|
|
|
time.Sleep(50 * time.Millisecond)
|
|
|
|
}
|
|
|
|
|
2020-04-12 14:56:50 +01:00
|
|
|
log.Log(res.Retrieving)
|
2020-02-25 18:12:01 +00:00
|
|
|
if err := chromedp.Run(ctx,
|
2020-02-24 23:01:10 +00:00
|
|
|
// Wait until custom js finishes
|
|
|
|
chromedp.WaitVisible(`video_urls`),
|
|
|
|
// Grab url links from our element
|
|
|
|
chromedp.InnerHTML(`video_urls`, &jsOutput),
|
2020-02-25 18:12:01 +00:00
|
|
|
); err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
2020-02-24 23:01:10 +00:00
|
|
|
|
2020-02-25 18:12:01 +00:00
|
|
|
return jsOutput, nil
|
2020-01-30 16:59:34 +00:00
|
|
|
}
|