From dc9b4adc81a3ca165b41e2c4dc9afee5e5d14008 Mon Sep 17 00:00:00 2001 From: PrivateGER Date: Sun, 17 Oct 2021 00:07:20 +0200 Subject: [PATCH] Add metadata collection + comment rendering --- DirectoryIndexers/DirectoryIndexer.go | 25 +++++++++++++ DirectoryIndexers/MetadataParser.go | 52 +++++++++++++++++++++++++++ PageHandlers/Index.go | 2 -- PageHandlers/View.go | 2 ++ PageHandlers/templates/base.html | 4 +-- PageHandlers/templates/index.html | 8 +++-- PageHandlers/templates/view.html | 47 +++++++++++++++++++++--- go.sum | 2 ++ 8 files changed, 131 insertions(+), 11 deletions(-) create mode 100644 DirectoryIndexers/MetadataParser.go create mode 100644 go.sum diff --git a/DirectoryIndexers/DirectoryIndexer.go b/DirectoryIndexers/DirectoryIndexer.go index 54d5557..243c755 100644 --- a/DirectoryIndexers/DirectoryIndexer.go +++ b/DirectoryIndexers/DirectoryIndexer.go @@ -5,6 +5,7 @@ import ( "github.com/dlclark/regexp2" "io/ioutil" "log" + "os" "path/filepath" "strings" "sync" @@ -20,6 +21,7 @@ type VideoFile struct { Extension string Title string Id string + Metadata Metadata } func Index(path string, results chan FileList) { @@ -56,11 +58,34 @@ func Index(path string, results chan FileList) { id := filenameToID(video.Name()) + videoObject := VideoFile{ + Filename: video.Name(), + Extension: extension, + Title: filenameToTitle(video.Name(), extension), + Id: id, + } + + metadataFilename := strings.TrimSuffix(video.Name(), filepath.Ext(video.Name())) + ".info.json" + jsonMetadataFile, err := os.Open(path + metadataFilename) + if err != nil { + FL.Files[id] = videoObject + continue + } + metadataBytes, _ := ioutil.ReadAll(jsonMetadataFile) + _ = jsonMetadataFile.Close() + + metadata, err := ParseMetadata(metadataBytes) + if err != nil { + FL.Files[id] = videoObject + continue + } + FL.Files[id] = VideoFile{ Filename: video.Name(), Extension: extension, Title: filenameToTitle(video.Name(), extension), Id: id, + Metadata: metadata, } } diff --git a/DirectoryIndexers/MetadataParser.go b/DirectoryIndexers/MetadataParser.go new file mode 100644 index 0000000..6f13398 --- /dev/null +++ b/DirectoryIndexers/MetadataParser.go @@ -0,0 +1,52 @@ +package DirectoryIndexers + +import ( + "encoding/json" + "fmt" +) + +type Metadata struct { + Description string `json:"description"` + UploadDate string `json:"upload_date"` + UploaderURL string `json:"uploader_url"` + ChannelID string `json:"channel_id"` + ChannelURL string `json:"channel_url"` + ViewCount int `json:"view_count"` + AverageRating float64 `json:"average_rating"` + AgeLimit int `json:"age_limit"` + WebpageURL string `json:"webpage_url"` + LikeCount int `json:"like_count"` + DislikeCount int `json:"dislike_count"` + Channel string `json:"channel"` + Thumbnail string `json:"thumbnail"` + DisplayID string `json:"display_id"` + Width int `json:"width"` + Height int `json:"height"` + Fps int `json:"fps"` + Vcodec string `json:"vcodec"` + Acodec string `json:"acodec"` + Abr float64 `json:"abr"` + Fulltitle string `json:"fulltitle"` + Comments []Comments `json:"comments"` + CommentCount int `json:"comment_count"` +} +type Comments struct { + ID string `json:"id"` + Text string `json:"text"` + Timestamp int `json:"timestamp"` + LikeCount int `json:"like_count"` + Author string `json:"author"` + AuthorID string `json:"author_id"` + AuthorThumbnail string `json:"author_thumbnail"` + AuthorIsUploader bool `json:"author_is_uploader"` +} + +func ParseMetadata(jsonBytes []byte) (Metadata, error) { + var meta Metadata + err := json.Unmarshal(jsonBytes, &meta) + if err != nil { + fmt.Println(err) + return Metadata{}, err + } + return meta, err +} diff --git a/PageHandlers/Index.go b/PageHandlers/Index.go index f8cb73a..56eb108 100644 --- a/PageHandlers/Index.go +++ b/PageHandlers/Index.go @@ -22,8 +22,6 @@ func Index(writer http.ResponseWriter, request *http.Request, FL *DirectoryIndex Files: FL.Files, } - fmt.Println(tmpl["index.html"].Name()) - err := tmpl["index.html"].ExecuteTemplate(writer, "base", data) if err != nil { fmt.Println(err) diff --git a/PageHandlers/View.go b/PageHandlers/View.go index 32c9110..5b6c623 100644 --- a/PageHandlers/View.go +++ b/PageHandlers/View.go @@ -13,6 +13,7 @@ type ViewPageData struct { Filename string Id string Extension string + Metadata DirectoryIndexers.Metadata } func View(writer http.ResponseWriter, request *http.Request, FL *DirectoryIndexers.FileList) { @@ -35,6 +36,7 @@ func View(writer http.ResponseWriter, request *http.Request, FL *DirectoryIndexe Filename: video.Filename, Id: video.Id, Extension: video.Extension, + Metadata: video.Metadata, } err := tmpl["view.html"].ExecuteTemplate(writer, "base", data) diff --git a/PageHandlers/templates/base.html b/PageHandlers/templates/base.html index 785dde3..613609b 100644 --- a/PageHandlers/templates/base.html +++ b/PageHandlers/templates/base.html @@ -14,8 +14,8 @@ {{template "head" .}} -
-

yt-dlp Archive Viewer

+
+

yt-dlp Archive Viewer