Added option to download metadata

This commit is contained in:
Pijus Kamandulis 2020-01-20 19:42:34 +02:00
parent 5609abb04c
commit 4e7093250f
4 changed files with 67 additions and 4 deletions

View File

@ -42,6 +42,11 @@ func downloadVideo(upload models.Upload, downloadDir string) {
fmt.Println("Downloading upload item '" + uploadID + "' to " + downloadPath)
utils.DownloadFile(downloadPath, upload.URL)
if models.Config.MetaData {
metadataPath := fmt.Sprintf("%s/%s.json", downloadDir, uploadID)
upload.WriteToFile(metadataPath)
}
}
func downloadUser() {

View File

@ -11,12 +11,14 @@ var Config struct {
URL string
OutputPath string
Debug bool
MetaData bool
}
// GetConfig - Returns Config object
func GetConfig() {
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()
args := flag.Args()
@ -28,4 +30,5 @@ func GetConfig() {
Config.URL = flag.Args()[len(args)-1]
Config.OutputPath = *outputPath
Config.Debug = *debug
Config.MetaData = *metadata
}

View File

@ -2,13 +2,23 @@ package models
import (
"encoding/json"
"fmt"
"os"
"strings"
)
// Upload - Upload object
type Upload struct {
ShareLink string `json:"shareLink"`
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
@ -30,3 +40,26 @@ func (u Upload) GetUploadID() string {
parts := strings.Split(u.ShareLink, "/")
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)
}
}

View File

@ -5,8 +5,12 @@ optStrings = {
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',
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',
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: {
feedVideoItem: 'video-feed-item-wrapper',
@ -50,10 +54,19 @@ getCurrentModalVideo = function() {
var modalPlayer = document.querySelector(optStrings.selectors.modalPlayer);
var vidUrl = modalPlayer.getAttribute(optStrings.attributes.src);
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 {
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 vidUrl = player.getAttribute(optStrings.attributes.src);
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 {
url: vidUrl,
shareLink: shareLink
shareLink: shareLink,
caption: caption,
sound: {
title: soundText,
link: soundHref,
},
};
}