Browse Source

Add metadata collection + comment rendering

master
PrivateGER 3 years ago
parent
commit
dc9b4adc81
Signed by: PrivateGER GPG Key ID: CAE625C962F94C67
  1. 25
      DirectoryIndexers/DirectoryIndexer.go
  2. 52
      DirectoryIndexers/MetadataParser.go
  3. 2
      PageHandlers/Index.go
  4. 2
      PageHandlers/View.go
  5. 4
      PageHandlers/templates/base.html
  6. 8
      PageHandlers/templates/index.html
  7. 47
      PageHandlers/templates/view.html
  8. 2
      go.sum

25
DirectoryIndexers/DirectoryIndexer.go

@ -5,6 +5,7 @@ import (
"github.com/dlclark/regexp2" "github.com/dlclark/regexp2"
"io/ioutil" "io/ioutil"
"log" "log"
"os"
"path/filepath" "path/filepath"
"strings" "strings"
"sync" "sync"
@ -20,6 +21,7 @@ type VideoFile struct {
Extension string Extension string
Title string Title string
Id string Id string
Metadata Metadata
} }
func Index(path string, results chan FileList) { func Index(path string, results chan FileList) {
@ -56,11 +58,34 @@ func Index(path string, results chan FileList) {
id := filenameToID(video.Name()) 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{ FL.Files[id] = VideoFile{
Filename: video.Name(), Filename: video.Name(),
Extension: extension, Extension: extension,
Title: filenameToTitle(video.Name(), extension), Title: filenameToTitle(video.Name(), extension),
Id: id, Id: id,
Metadata: metadata,
} }
} }

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

2
PageHandlers/Index.go

@ -22,8 +22,6 @@ func Index(writer http.ResponseWriter, request *http.Request, FL *DirectoryIndex
Files: FL.Files, Files: FL.Files,
} }
fmt.Println(tmpl["index.html"].Name())
err := tmpl["index.html"].ExecuteTemplate(writer, "base", data) err := tmpl["index.html"].ExecuteTemplate(writer, "base", data)
if err != nil { if err != nil {
fmt.Println(err) fmt.Println(err)

2
PageHandlers/View.go

@ -13,6 +13,7 @@ type ViewPageData struct {
Filename string Filename string
Id string Id string
Extension string Extension string
Metadata DirectoryIndexers.Metadata
} }
func View(writer http.ResponseWriter, request *http.Request, FL *DirectoryIndexers.FileList) { 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, Filename: video.Filename,
Id: video.Id, Id: video.Id,
Extension: video.Extension, Extension: video.Extension,
Metadata: video.Metadata,
} }
err := tmpl["view.html"].ExecuteTemplate(writer, "base", data) err := tmpl["view.html"].ExecuteTemplate(writer, "base", data)

4
PageHandlers/templates/base.html

@ -14,8 +14,8 @@
{{template "head" .}} {{template "head" .}}
</head> </head>
<body style="background-color: #212121"> <body style="background-color: #212121">
<div style="background-color: #424242; padding: 8px; border-radius: 15px; margin-bottom: 50px"> <div style="background-color: #424242; padding: 8px; border-radius: 15px;">
<h1 style="float: left">yt-dlp Archive Viewer</h1> <a href="/" style="text-decoration: none"><h1 style="float: left">yt-dlp Archive Viewer</h1></a>
<form action="/search" method="get" style="float: right; margin: auto"> <form action="/search" method="get" style="float: right; margin: auto">
<label> <label>
Search term: Search term:

8
PageHandlers/templates/index.html

@ -1,9 +1,9 @@
{{define "head"}} {{define "head"}}
<title>ytdlp-viewer | {{.FileCount}}</title> <title>ytdlp-viewer | {{.FileCount}} videos</title>
{{end}} {{end}}
{{define "body"}} {{define "body"}}
<div style="text-align: center;"> <div>
<h1>yt-dlp Viewer</h1> <h1>yt-dlp Viewer</h1>
<h2>{{.FileCount}} files in this archive</h2> <h2>{{.FileCount}} files in this archive</h2>
@ -15,7 +15,9 @@
</form> </form>
{{range .Files}} {{range .Files}}
<a href="/view?id={{.Id}}">{{.Id}} - {{.Title}}</a><br /> <p><a href="/view?id={{.Id}}">{{.Id}} - {{.Title}}</a>
{{ if .Metadata.ChannelID }} [M] {{ end }}
{{ if .Metadata.ChannelID }} - {{.Metadata.Channel }} {{ end }}<br /></p>
{{end}} {{end}}
</div> </div>
{{end}} {{end}}

47
PageHandlers/templates/view.html

@ -8,10 +8,49 @@
{{end}} {{end}}
{{define "body"}} {{define "body"}}
<video class="video-js vjs-big-play-centered centeredvideo" id="player" height="720" width="1280" controls autoplay data-setup="{}"> <div style="max-width: 1280px">
<source src="/videos/{{.Filename}}"> <video class="video-js vjs-big-play-centered centeredvideo" id="player" height="720" width="1280" controls autoplay data-setup="{}">
</video> <source src="/videos/{{.Filename}}">
<h1>{{.Title}}</h1> </video>
<h1>{{.Title}}</h1>
{{ if .Metadata.ChannelID }}
<div>
<p style="float: right">{{ .Metadata.LikeCount }} likes | {{ .Metadata.DislikeCount }} dislikes</p>
<p>{{ .Metadata.ViewCount }} views</p>
<p>Uploaded by: <a href="{{ .Metadata.UploaderURL }}">{{ .Metadata.Channel }}</a></p>
<hr />
<div style="clear: both"></div>
</div>
<br />
<div style="white-space: pre-line;">
{{ .Metadata.Description }}
</div>
<p>Comments: ({{ .Metadata.CommentCount }}</p>
<hr />
<div>
{{range .Metadata.Comments}}
<div>
<img src="{{ .AuthorThumbnail }}" style="border-radius: 50%; max-height: 75px; max-width: 75px; float: left" />
<p>{{ .Author }}</p>
<div style="clear: both"></div>
<p>{{ .LikeCount }} likes</p>
</div>
<div style="white-space: pre-line;">
{{ .Text }}
</div>
<hr />
{{end}}
</div>
{{ end }}
</div>
<br />
<p>Filetype: {{.Extension}}</p> <p>Filetype: {{.Extension}}</p>
<p>Youtube ID: {{.Id}}</p> <p>Youtube ID: {{.Id}}</p>
<p>Filename: {{.Filename}}</p> <p>Filename: {{.Filename}}</p>

2
go.sum

@ -0,0 +1,2 @@
github.com/dlclark/regexp2 v1.4.0 h1:F1rxgk7p4uKjwIQxBs9oAXe5CqrXlCduYEJvrF4u93E=
github.com/dlclark/regexp2 v1.4.0/go.mod h1:2pZnwuY/m+8K6iRw6wQdMtk+rH5tNGR1i55kozfMjCc=
Loading…
Cancel
Save