Info command update
This commit is contained in:
parent
06db684c43
commit
b8d6934025
30
cmd/info.go
30
cmd/info.go
|
@ -5,9 +5,8 @@ import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"log"
|
"log"
|
||||||
|
|
||||||
bunkr "tea.chunkbyte.com/kato/grabrr/lib/bunkr"
|
|
||||||
|
|
||||||
"github.com/spf13/cobra"
|
"github.com/spf13/cobra"
|
||||||
|
bunkr "tea.chunkbyte.com/kato/grabrr/lib/bunkr"
|
||||||
)
|
)
|
||||||
|
|
||||||
// infoCmd represents the info command
|
// infoCmd represents the info command
|
||||||
|
@ -25,29 +24,42 @@ var infoCmd = &cobra.Command{
|
||||||
var (
|
var (
|
||||||
albumURL string
|
albumURL string
|
||||||
prettyPrint bool
|
prettyPrint bool
|
||||||
|
prettyJson bool
|
||||||
)
|
)
|
||||||
|
|
||||||
func init() {
|
func init() {
|
||||||
rootCmd.AddCommand(infoCmd)
|
rootCmd.AddCommand(infoCmd)
|
||||||
|
|
||||||
infoCmd.Flags().StringVarP(&albumURL, "url", "u", "", "Bunkrr album url (required)")
|
infoCmd.Flags().StringVarP(&albumURL, "url", "u", "", "Bunkrr album url (required)")
|
||||||
infoCmd.Flags().BoolVarP(&prettyPrint, "pretty", "p", false, "Whether or not to Pretty Print, otherwise export json")
|
infoCmd.Flags().BoolVarP(&prettyPrint, "pretty", "p", false, "Whether or not to Pretty Print, otherwise export json")
|
||||||
|
infoCmd.Flags().BoolVarP(&prettyJson, "json", "j", true, "Whether or not to pretty print the JSON output")
|
||||||
infoCmd.MarkFlagRequired("url")
|
infoCmd.MarkFlagRequired("url")
|
||||||
}
|
}
|
||||||
|
|
||||||
func runInfoCommand() error {
|
func runInfoCommand() error {
|
||||||
album, err := bunkr.FetchAlbumInfo(albumURL)
|
album, err := bunkr.FetchAlbumInfo(albumURL)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
if prettyPrint {
|
if prettyPrint {
|
||||||
fmt.Println(album.ToString())
|
fmt.Println(album.ToString())
|
||||||
} else {
|
} else {
|
||||||
jsonObject, err := json.Marshal(album)
|
|
||||||
if err != nil {
|
if prettyJson {
|
||||||
panic(err)
|
prettyJSON, err := json.MarshalIndent(album, "", " ")
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
fmt.Printf("%s", prettyJSON)
|
||||||
|
} else {
|
||||||
|
jsonObject, err := json.Marshal(album)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
} else {
|
||||||
|
fmt.Printf("%s", jsonObject)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fmt.Printf("%s", jsonObject)
|
|
||||||
}
|
}
|
||||||
|
return nil
|
||||||
return err
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -34,12 +34,14 @@ func FetchAlbumInfo(rawURL string) (*strux.Album, error) {
|
||||||
|
|
||||||
var album *strux.Album = &strux.Album{}
|
var album *strux.Album = &strux.Album{}
|
||||||
fmt.Println(rawURL)
|
fmt.Println(rawURL)
|
||||||
album.URL, err = url.Parse(rawURL)
|
newUrl, err := url.Parse(rawURL)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, fmt.Errorf("failed to parse URL: %w", err)
|
return nil, fmt.Errorf("failed to parse URL: %w", err)
|
||||||
|
} else {
|
||||||
|
album.URL = &strux.URLString{URL: newUrl}
|
||||||
}
|
}
|
||||||
|
|
||||||
album.ID = helper.GetLastPathElement(album.URL)
|
album.ID = helper.GetLastPathElement(album.URL.URL)
|
||||||
// Title
|
// Title
|
||||||
album.Title = strings.TrimSpace(doc.Find("h1.truncate").First().Text())
|
album.Title = strings.TrimSpace(doc.Find("h1.truncate").First().Text())
|
||||||
|
|
||||||
|
@ -75,7 +77,7 @@ func FetchAlbumInfo(rawURL string) (*strux.Album, error) {
|
||||||
|
|
||||||
file := strux.File{
|
file := strux.File{
|
||||||
Title: fileTitle,
|
Title: fileTitle,
|
||||||
URL: fileURL,
|
URL: &strux.URLString{URL: fileURL},
|
||||||
Filename: fileTitle,
|
Filename: fileTitle,
|
||||||
SizeKB: fileSize,
|
SizeKB: fileSize,
|
||||||
Format: helper.GetMimeTypeFromFilename(fileTitle),
|
Format: helper.GetMimeTypeFromFilename(fileTitle),
|
||||||
|
|
|
@ -21,22 +21,22 @@ type AlbumStatsResponse struct {
|
||||||
|
|
||||||
// Album represents a Bunkkr album.
|
// Album represents a Bunkkr album.
|
||||||
type Album struct {
|
type Album struct {
|
||||||
Title string `json:"title"`
|
Title string `json:"title"`
|
||||||
Views int `json:"views"`
|
Views int `json:"views"`
|
||||||
Files []File `json:"files"`
|
Files []File `json:"files"`
|
||||||
ID string `json:"id"`
|
ID string `json:"id"`
|
||||||
URL *url.URL `json:"url"`
|
URL *URLString `json:"url"`
|
||||||
SizeKB int64 `json:"size_kb"`
|
SizeKB int64 `json:"size_kb"`
|
||||||
}
|
}
|
||||||
|
|
||||||
// File represents a single file within a Bunkkr album.
|
// File represents a single file within a Bunkkr album.
|
||||||
type File struct {
|
type File struct {
|
||||||
Title string `json:"title"`
|
Title string `json:"title"`
|
||||||
SizeKB int64 `json:"size_kb"`
|
SizeKB int64 `json:"size_kb"`
|
||||||
DateUploaded time.Time `json:"date_uploaded"`
|
DateUploaded time.Time `json:"date_uploaded"`
|
||||||
URL *url.URL `json:"url"`
|
URL *URLString `json:"url"`
|
||||||
Filename string `json:"filename"`
|
Filename string `json:"filename"`
|
||||||
Format string `json:"format"`
|
Format string `json:"format"`
|
||||||
}
|
}
|
||||||
|
|
||||||
func (a *Album) GetSize() string {
|
func (a *Album) GetSize() string {
|
||||||
|
@ -161,3 +161,30 @@ func (f *File) ToString() string {
|
||||||
|
|
||||||
return builder.String()
|
return builder.String()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// ### WRAPPERS ###
|
||||||
|
|
||||||
|
type URLString struct {
|
||||||
|
*url.URL
|
||||||
|
}
|
||||||
|
|
||||||
|
func (u URLString) MarshalJSON() ([]byte, error) {
|
||||||
|
if u.URL == nil {
|
||||||
|
return []byte(`""`), nil
|
||||||
|
}
|
||||||
|
return []byte(`"` + u.String() + `"`), nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (u *URLString) UnmarshalJSON(data []byte) error {
|
||||||
|
s := strings.Trim(string(data), `"`)
|
||||||
|
if s == "" {
|
||||||
|
u.URL = nil
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
parsed, err := url.Parse(s)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
u.URL = parsed
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
Loading…
Reference in New Issue