mirror of https://github.com/pikami/tiktok-dl.git
Added option to download metadata
This commit is contained in:
parent
5609abb04c
commit
4e7093250f
5
main.go
5
main.go
|
@ -42,6 +42,11 @@ func downloadVideo(upload models.Upload, downloadDir string) {
|
||||||
|
|
||||||
fmt.Println("Downloading upload item '" + uploadID + "' to " + downloadPath)
|
fmt.Println("Downloading upload item '" + uploadID + "' to " + downloadPath)
|
||||||
utils.DownloadFile(downloadPath, upload.URL)
|
utils.DownloadFile(downloadPath, upload.URL)
|
||||||
|
|
||||||
|
if models.Config.MetaData {
|
||||||
|
metadataPath := fmt.Sprintf("%s/%s.json", downloadDir, uploadID)
|
||||||
|
upload.WriteToFile(metadataPath)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func downloadUser() {
|
func downloadUser() {
|
||||||
|
|
|
@ -11,12 +11,14 @@ var Config struct {
|
||||||
URL string
|
URL string
|
||||||
OutputPath string
|
OutputPath string
|
||||||
Debug bool
|
Debug bool
|
||||||
|
MetaData bool
|
||||||
}
|
}
|
||||||
|
|
||||||
// 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")
|
||||||
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")
|
||||||
flag.Parse()
|
flag.Parse()
|
||||||
|
|
||||||
args := flag.Args()
|
args := flag.Args()
|
||||||
|
@ -28,4 +30,5 @@ func GetConfig() {
|
||||||
Config.URL = flag.Args()[len(args)-1]
|
Config.URL = flag.Args()[len(args)-1]
|
||||||
Config.OutputPath = *outputPath
|
Config.OutputPath = *outputPath
|
||||||
Config.Debug = *debug
|
Config.Debug = *debug
|
||||||
|
Config.MetaData = *metadata
|
||||||
}
|
}
|
||||||
|
|
|
@ -2,13 +2,23 @@ package models
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
|
"fmt"
|
||||||
|
"os"
|
||||||
"strings"
|
"strings"
|
||||||
)
|
)
|
||||||
|
|
||||||
// Upload - Upload object
|
// Upload - Upload object
|
||||||
type Upload struct {
|
type Upload struct {
|
||||||
ShareLink string `json:"shareLink"`
|
|
||||||
URL string `json:"url"`
|
URL string `json:"url"`
|
||||||
|
ShareLink string `json:"shareLink"`
|
||||||
|
Caption string `json:"caption"`
|
||||||
|
Sound Sound `json:"sound"`
|
||||||
|
}
|
||||||
|
|
||||||
|
// Sound - Sound object
|
||||||
|
type Sound struct {
|
||||||
|
Title string `json:"title"`
|
||||||
|
Link string `json:"link"`
|
||||||
}
|
}
|
||||||
|
|
||||||
// ParseUploads - Parses json uploads array
|
// ParseUploads - Parses json uploads array
|
||||||
|
@ -30,3 +40,26 @@ func (u Upload) GetUploadID() string {
|
||||||
parts := strings.Split(u.ShareLink, "/")
|
parts := strings.Split(u.ShareLink, "/")
|
||||||
return parts[len(parts)-1]
|
return parts[len(parts)-1]
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// WriteToFile - Writes object to file
|
||||||
|
func (u Upload) WriteToFile(outputPath string) {
|
||||||
|
bytes, err := json.Marshal(u)
|
||||||
|
if err != nil {
|
||||||
|
fmt.Printf("Could not serialize json for video: %s", u.GetUploadID())
|
||||||
|
fmt.Println()
|
||||||
|
panic(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Create the file
|
||||||
|
out, err := os.Create(outputPath)
|
||||||
|
if err != nil {
|
||||||
|
panic(err)
|
||||||
|
}
|
||||||
|
defer out.Close()
|
||||||
|
|
||||||
|
// Write to file
|
||||||
|
_, err = out.Write(bytes)
|
||||||
|
if err != nil {
|
||||||
|
panic(err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
26
scraper.js
26
scraper.js
|
@ -5,8 +5,12 @@ optStrings = {
|
||||||
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',
|
||||||
|
modalCaption: 'div.video-card-big > div.content-container > div.video-meta-info > h1',
|
||||||
|
modalSoundLink: 'div.content-container > div.video-meta-info > h2.music-info > a',
|
||||||
videoPlayer: 'div.video-card-container > div > div > video',
|
videoPlayer: 'div.video-card-container > div > div > video',
|
||||||
videoShareInput: 'div.content-container.border > div.copy-link-container > input',
|
videoShareInput: 'div.content-container.border > div.copy-link-container > input',
|
||||||
|
videoCaption: 'div.content-container.border > div.video-meta-info > h1',
|
||||||
|
videoSoundLink: 'div.content-container.border > div.video-meta-info > h2.music-info > a',
|
||||||
},
|
},
|
||||||
classes: {
|
classes: {
|
||||||
feedVideoItem: 'video-feed-item-wrapper',
|
feedVideoItem: 'video-feed-item-wrapper',
|
||||||
|
@ -50,10 +54,19 @@ getCurrentModalVideo = function() {
|
||||||
var modalPlayer = document.querySelector(optStrings.selectors.modalPlayer);
|
var modalPlayer = document.querySelector(optStrings.selectors.modalPlayer);
|
||||||
var vidUrl = modalPlayer.getAttribute(optStrings.attributes.src);
|
var vidUrl = modalPlayer.getAttribute(optStrings.attributes.src);
|
||||||
var shareLink = document.querySelector(optStrings.selectors.modalShareInput).value;
|
var shareLink = document.querySelector(optStrings.selectors.modalShareInput).value;
|
||||||
|
var caption = document.querySelector(optStrings.selectors.modalCaption).textContent;
|
||||||
|
var soundLink = document.querySelector(optStrings.selectors.modalSoundLink);
|
||||||
|
var soundHref = soundLink.getAttribute("href");
|
||||||
|
var soundText = soundLink.text;
|
||||||
|
|
||||||
return {
|
return {
|
||||||
url: vidUrl,
|
url: vidUrl,
|
||||||
shareLink: shareLink
|
shareLink: shareLink,
|
||||||
|
caption: caption,
|
||||||
|
sound: {
|
||||||
|
title: soundText,
|
||||||
|
link: soundHref,
|
||||||
|
},
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -61,10 +74,19 @@ getCurrentVideo = function() {
|
||||||
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;
|
||||||
|
var caption = document.querySelector(optStrings.selectors.videoCaption).textContent;
|
||||||
|
var soundLink = document.querySelector(optStrings.selectors.videoSoundLink);
|
||||||
|
var soundHref = soundLink.getAttribute("href");
|
||||||
|
var soundText = soundLink.text;
|
||||||
|
|
||||||
return {
|
return {
|
||||||
url: vidUrl,
|
url: vidUrl,
|
||||||
shareLink: shareLink
|
shareLink: shareLink,
|
||||||
|
caption: caption,
|
||||||
|
sound: {
|
||||||
|
title: soundText,
|
||||||
|
link: soundHref,
|
||||||
|
},
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue