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

3
.gitignore vendored Normal file
View File

@ -0,0 +1,3 @@
downloads
*.exe
tiktok-dl

57
client/getUserUploads.go Normal file
View File

@ -0,0 +1,57 @@
package client
import (
"context"
"github.com/chromedp/chromedp"
"io/ioutil"
"log"
"os"
"time"
models "../models"
utils "../utils"
)
// GetUserUploads - Get all uploads by user
func GetUserUploads(username string) []models.Upload {
dir, err := ioutil.TempDir("", "chromedp-example")
if err != nil {
panic(err)
}
defer os.RemoveAll(dir)
opts := append(chromedp.DefaultExecAllocatorOptions[:],
chromedp.DisableGPU,
chromedp.UserDataDir(dir),
chromedp.Flag("headless", false),
)
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(`https://www.tiktok.com/@`+username),
// Execute url grabber script
chromedp.EvaluateAsDevTools(utils.ReadFileAsString("getVidLinks.js"), &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)
}

67
getVidLinks.js Normal file
View File

@ -0,0 +1,67 @@
createVidUrlElement = function(videoSet) {
var videoArray = Object.entries(videoSet).map(x => {
return {
shareLink: x[1].shareLink,
url: x[0],
};
});
var urlSetElement = document.createElement("video_urls");
urlSetElement.innerText = JSON.stringify(videoArray);
document.getElementsByTagName("body")[0].appendChild(urlSetElement);
}
buldVidUrlSet = function(finishCallback) {
var feedItem = document.getElementsByClassName("video-feed-item-wrapper")[0];
feedItem.click();
var videoSet = {};
var intervalID = window.setInterval(x => {
var players = document.getElementsByClassName("video-player");
for (var i = 0; i < players.length; i++) {
var vidUrl = players[i].getAttribute("src");
if(!videoSet[vidUrl]) {
var shareLink = document.querySelector(".copy-link-container > input").value;
videoSet[vidUrl] = {
shareLink: shareLink
};
}
}
var arrowRight = document.querySelectorAll("div.video-card-modal > div > img.arrow-right")[0];
if (arrowRight.classList.contains("disabled")) {
window.clearInterval(intervalID);
document.querySelector(".video-card-modal > div > div.close").click();
finishCallback(videoSet);
} else {
arrowRight.click();
}
}, 500);
};
scrollWhileNew = function(finishCallback) {
var state = { count: 0 };
var intervalID = window.setInterval(x => {
var oldCount = state.count;
state.count = document.getElementsByClassName("video-feed-item").length;
if (oldCount !== state.count) {
window.scrollTo(0, document.body.scrollHeight);
} else {
window.clearInterval(intervalID);
finishCallback();
}
}, 1000);
};
init = () => {
const newProto = navigator.__proto__;
delete newProto.webdriver;
navigator.__proto__ = newProto;
window.setTimeout(x => {
window.scrollTo(0, document.body.scrollHeight);
window.setTimeout(x => buldVidUrlSet(createVidUrlElement), 2000);
}, 1000)
};
init();
'script initialized'

30
main.go Normal file
View File

@ -0,0 +1,30 @@
package main
import (
client "./client"
models "./models"
utils "./utils"
"fmt"
)
func main() {
models.GetConfig()
username := models.Config.UserName
downloadDir := fmt.Sprintf("%s/%s", models.Config.OutputPath, username)
uploads := client.GetUserUploads(username)
utils.InitOutputDirectory(downloadDir)
for _, upload := range uploads {
uploadID := upload.GetUploadID()
downloadPath := fmt.Sprintf("%s/%s.mp4", downloadDir, uploadID)
if utils.CheckIfExists(downloadPath) {
fmt.Println("Upload '" + uploadID + "' already downloaded, skipping")
continue
}
utils.DownloadFile(downloadPath, upload.URL)
}
}

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)
}
}

31
utils/downloadFile.go Normal file
View File

@ -0,0 +1,31 @@
package utils
import (
"io"
"net/http"
"os"
)
// DownloadFile - Downloads content from `url` and stores it in `outputPath`
func DownloadFile(outputPath string, url string) {
// Get the data
resp, err := http.Get(url)
if err != nil {
panic(err)
}
defer resp.Body.Close()
// Create the file
out, err := os.Create(outputPath)
if err != nil {
panic(err)
}
defer out.Close()
// Write the body to file
_, err = io.Copy(out, resp.Body)
if err != nil {
panic(err)
}
}

21
utils/fileio.go Normal file
View File

@ -0,0 +1,21 @@
package utils
import (
"os"
)
// CheckIfExists - Checks if file or directory exists
func CheckIfExists(path string) bool {
if _, err := os.Stat(path); os.IsNotExist(err) {
return false
}
return true
}
// InitOutputDirectory - Creates output directory
func InitOutputDirectory(path string) {
if _, err := os.Stat(path); os.IsNotExist(err) {
os.MkdirAll(path, os.ModePerm)
}
}

15
utils/readFileAsString.go Normal file
View File

@ -0,0 +1,15 @@
package utils
import (
"io/ioutil"
"log"
)
// ReadFileAsString - Returns contents of given file
func ReadFileAsString(fileName string) string {
content, err := ioutil.ReadFile(fileName)
if err != nil {
log.Fatal(err)
}
return string(content)
}