More stufferino
This commit is contained in:
@@ -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
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user