From 4e7093250fd1aeb34cc74cb475f60c2e09402d6e Mon Sep 17 00:00:00 2001 From: Pijus Kamandulis Date: Mon, 20 Jan 2020 19:42:34 +0200 Subject: [PATCH] Added option to download metadata --- main.go | 5 +++++ models/config.go | 5 ++++- models/upload.go | 35 ++++++++++++++++++++++++++++++++++- scraper.js | 26 ++++++++++++++++++++++++-- 4 files changed, 67 insertions(+), 4 deletions(-) diff --git a/main.go b/main.go index ba48c5e..8bc0c3a 100644 --- a/main.go +++ b/main.go @@ -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() { diff --git a/models/config.go b/models/config.go index ad7b5e6..34ea98d 100644 --- a/models/config.go +++ b/models/config.go @@ -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 } diff --git a/models/upload.go b/models/upload.go index 3b90241..f0ef37d 100644 --- a/models/upload.go +++ b/models/upload.go @@ -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) + } +} diff --git a/scraper.js b/scraper.js index 1c36e35..2afad91 100644 --- a/scraper.js +++ b/scraper.js @@ -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, + }, }; }