tiktok-dl/client/getRedirectUrl.go

55 lines
1.2 KiB
Go
Raw Normal View History

2020-03-22 10:38:08 +00:00
package client
import (
"context"
"io/ioutil"
"os"
"time"
2020-04-09 16:53:45 +01:00
"github.com/chromedp/chromedp"
2020-04-12 01:22:00 +01:00
config "github.com/pikami/tiktok-dl/models/config"
log "github.com/pikami/tiktok-dl/utils/log"
2020-03-22 10:38:08 +00:00
)
2020-04-09 16:53:45 +01:00
// GetRedirectURL - Returns URL that the given URL redirects to
func GetRedirectURL(url string) (string, error) {
2020-03-22 10:38:08 +00:00
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
}