More stufferino

This commit is contained in:
Daniel Legt 2025-07-23 15:08:33 +03:00
parent 0777c108ee
commit b5f8fb0118
4 changed files with 134 additions and 12 deletions

View File

@ -22,14 +22,14 @@ to quickly create a Cobra application.`,
}
var (
url string
albumURLLink string
downloadLocation string
)
func init() {
rootCmd.AddCommand(getCmd)
getCmd.Flags().StringVarP(&url, "url", "u", "", "Bunkrr album url (required)")
getCmd.Flags().StringVarP(&albumURLLink, "url", "u", "", "Bunkrr album url (required)")
getCmd.Flags().StringVarP(&downloadLocation, "location", "l", "./", "Download destination, it will create a folder inside of it with the album id")
getCmd.MarkFlagRequired("url")

View File

@ -2,6 +2,9 @@ package cmd
import (
"fmt"
"log"
bunkr "tea.chunkbyte.com/kato/grabrr/lib/bunkr"
"github.com/spf13/cobra"
)
@ -12,7 +15,9 @@ var infoCmd = &cobra.Command{
Short: "A simple info fetch of a bunkr album",
Long: `The command is used to fetch information about a specific bunkrr album, it will reply with all the information it has about it.`,
Run: func(cmd *cobra.Command, args []string) {
fmt.Println("info called")
if err := runInfoCommand(); err != nil {
log.Fatalf("error: %v", err)
}
},
}
@ -25,5 +30,18 @@ func init() {
infoCmd.Flags().StringVarP(&albumURL, "url", "u", "", "Bunkrr album url (required)")
infoCmd.MarkFlagRequired("url")
}
func runInfoCommand() error {
album, err := bunkr.FetchAlbumInfo(albumURL)
// Print
fmt.Printf("Album Title: %s\n", album.Title)
fmt.Printf("Album URL: %s\n", album.URL.String())
fmt.Printf("Files (%d):\n", len(album.Files))
for _, f := range album.Files {
fmt.Printf(" - %s (%s)\n", f.Filename, f.URL.String())
}
return err
}

View File

@ -1,5 +1,109 @@
package lib
import (
"fmt"
"net/http"
"net/url"
"strconv"
"strings"
strux "tea.chunkbyte.com/kato/grabrr/lib/strux"
"github.com/PuerkitoBio/goquery"
)
func GetBunkrAlbum() {
}
func FetchAlbumInfo(rawURL string) (*strux.Album, error) {
resp, err := http.Get(rawURL)
if err != nil {
return nil, fmt.Errorf("failed to fetch page: %w", err)
}
defer resp.Body.Close()
if resp.StatusCode != 200 {
return nil, fmt.Errorf("non-200 response: %d", resp.StatusCode)
}
doc, err := goquery.NewDocumentFromReader(resp.Body)
if err != nil {
return nil, fmt.Errorf("failed to parse HTML: %w", err)
}
var album *strux.Album = &strux.Album{}
fmt.Println(rawURL)
album.URL, err = url.Parse(rawURL)
if err != nil {
return nil, fmt.Errorf("failed to parse URL: %w", err)
}
// Title
album.Title = strings.TrimSpace(doc.Find("h1.truncate").First().Text())
// Files
doc.Find(".grid-images .theItem").Each(func(i int, s *goquery.Selection) {
linkTag := s.Find("a")
href, exists := linkTag.Attr("href")
if !exists {
return
}
fileTitle := "No name"
var fileSize int64 = 0
if theTitleEl := s.Find(".theName"); theTitleEl != nil {
fileTitle = strings.TrimSpace(theTitleEl.Text())
}
if theSizeEl := s.Find(".theSize"); theSizeEl != nil {
fileSize, err = ParseSizeToKB(theSizeEl.Text())
}
if href[0] == '/' {
href = fmt.Sprintf("%s://%s%s", album.URL.Scheme, album.URL.Hostname(), href)
}
fileURL, _ := url.Parse(href)
file := strux.File{
Title: fileTitle,
URL: fileURL,
Filename: fileTitle,
SizeKB: fileSize,
Format: fileURL.Path[strings.LastIndex(fileURL.Path, ".")+1:],
}
album.Files = append(album.Files, file)
})
return album, nil
}
func ParseSizeToKB(sizeStr string) (int64, error) {
s := strings.TrimSpace(strings.ToLower(sizeStr))
var multiplier float64
switch {
case strings.HasSuffix(s, "gb"):
multiplier = 1024 * 1024
s = strings.TrimSuffix(s, "gb")
case strings.HasSuffix(s, "mb"):
multiplier = 1024
s = strings.TrimSuffix(s, "mb")
case strings.HasSuffix(s, "kb"):
multiplier = 1
s = strings.TrimSuffix(s, "kb")
default:
return 0, fmt.Errorf("unrecognized unit in %q", sizeStr)
}
s = strings.TrimSpace(s)
value, err := strconv.ParseFloat(s, 64)
if err != nil {
return 0, fmt.Errorf("invalid number %q: %w", s, err)
}
return int64(value * multiplier), nil
}

View File

@ -8,19 +8,19 @@ import (
// File represents a single file within a Bunkkr album.
type File struct {
Title string `json:"title"`
SizeKB float64 `json:"size_kb"`
SizeKB int64 `json:"size_kb"`
DateUploaded time.Time `json:"date_uploaded"`
URL url.URL `json:"url"`
URL *url.URL `json:"url"`
Filename string `json:"filename"`
Format string `json:"format"`
}
// 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 float64 `json:"size_kb"`
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"`
}