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"
 | 
			
		||||
	"log"
 | 
			
		||||
 | 
			
		||||
	bunkr "tea.chunkbyte.com/kato/grabrr/lib/bunkr"
 | 
			
		||||
 | 
			
		||||
	"github.com/spf13/cobra"
 | 
			
		||||
	bunkr "tea.chunkbyte.com/kato/grabrr/lib/bunkr"
 | 
			
		||||
)
 | 
			
		||||
 | 
			
		||||
// infoCmd represents the info command
 | 
			
		||||
| 
						 | 
				
			
			@ -25,29 +24,42 @@ var infoCmd = &cobra.Command{
 | 
			
		|||
var (
 | 
			
		||||
	albumURL    string
 | 
			
		||||
	prettyPrint bool
 | 
			
		||||
	prettyJson  bool
 | 
			
		||||
)
 | 
			
		||||
 | 
			
		||||
func init() {
 | 
			
		||||
	rootCmd.AddCommand(infoCmd)
 | 
			
		||||
 | 
			
		||||
	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(&prettyJson, "json", "j", true, "Whether or not to pretty print the JSON output")
 | 
			
		||||
	infoCmd.MarkFlagRequired("url")
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func runInfoCommand() error {
 | 
			
		||||
	album, err := bunkr.FetchAlbumInfo(albumURL)
 | 
			
		||||
	if err != nil {
 | 
			
		||||
		return err
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	if prettyPrint {
 | 
			
		||||
		fmt.Println(album.ToString())
 | 
			
		||||
	} else {
 | 
			
		||||
		jsonObject, err := json.Marshal(album)
 | 
			
		||||
		if err != nil {
 | 
			
		||||
			panic(err)
 | 
			
		||||
 | 
			
		||||
		if prettyJson {
 | 
			
		||||
			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 err
 | 
			
		||||
	return nil
 | 
			
		||||
}
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -34,12 +34,14 @@ func FetchAlbumInfo(rawURL string) (*strux.Album, error) {
 | 
			
		|||
 | 
			
		||||
	var album *strux.Album = &strux.Album{}
 | 
			
		||||
	fmt.Println(rawURL)
 | 
			
		||||
	album.URL, err = url.Parse(rawURL)
 | 
			
		||||
	newUrl, err := url.Parse(rawURL)
 | 
			
		||||
	if err != nil {
 | 
			
		||||
		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
 | 
			
		||||
	album.Title = strings.TrimSpace(doc.Find("h1.truncate").First().Text())
 | 
			
		||||
 | 
			
		||||
| 
						 | 
				
			
			@ -75,7 +77,7 @@ func FetchAlbumInfo(rawURL string) (*strux.Album, error) {
 | 
			
		|||
 | 
			
		||||
		file := strux.File{
 | 
			
		||||
			Title:    fileTitle,
 | 
			
		||||
			URL:      fileURL,
 | 
			
		||||
			URL:      &strux.URLString{URL: fileURL},
 | 
			
		||||
			Filename: fileTitle,
 | 
			
		||||
			SizeKB:   fileSize,
 | 
			
		||||
			Format:   helper.GetMimeTypeFromFilename(fileTitle),
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -21,22 +21,22 @@ type AlbumStatsResponse struct {
 | 
			
		|||
 | 
			
		||||
// Album represents a Bunkkr album.
 | 
			
		||||
type Album struct {
 | 
			
		||||
	Title  string   `json:"title"`
 | 
			
		||||
	Views  int      `json:"views"`
 | 
			
		||||
	Files  []File   `json:"files"`
 | 
			
		||||
	ID     string   `json:"id"`
 | 
			
		||||
	URL    *url.URL `json:"url"`
 | 
			
		||||
	SizeKB int64    `json:"size_kb"`
 | 
			
		||||
	Title  string     `json:"title"`
 | 
			
		||||
	Views  int        `json:"views"`
 | 
			
		||||
	Files  []File     `json:"files"`
 | 
			
		||||
	ID     string     `json:"id"`
 | 
			
		||||
	URL    *URLString `json:"url"`
 | 
			
		||||
	SizeKB int64      `json:"size_kb"`
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// File represents a single file within a Bunkkr album.
 | 
			
		||||
type File struct {
 | 
			
		||||
	Title        string    `json:"title"`
 | 
			
		||||
	SizeKB       int64     `json:"size_kb"`
 | 
			
		||||
	DateUploaded time.Time `json:"date_uploaded"`
 | 
			
		||||
	URL          *url.URL  `json:"url"`
 | 
			
		||||
	Filename     string    `json:"filename"`
 | 
			
		||||
	Format       string    `json:"format"`
 | 
			
		||||
	Title        string     `json:"title"`
 | 
			
		||||
	SizeKB       int64      `json:"size_kb"`
 | 
			
		||||
	DateUploaded time.Time  `json:"date_uploaded"`
 | 
			
		||||
	URL          *URLString `json:"url"`
 | 
			
		||||
	Filename     string     `json:"filename"`
 | 
			
		||||
	Format       string     `json:"format"`
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func (a *Album) GetSize() string {
 | 
			
		||||
| 
						 | 
				
			
			@ -161,3 +161,30 @@ func (f *File) ToString() 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