Info Fetching completed
This commit is contained in:
@@ -4,9 +4,9 @@ import (
|
||||
"fmt"
|
||||
"net/http"
|
||||
"net/url"
|
||||
"strconv"
|
||||
"strings"
|
||||
|
||||
"tea.chunkbyte.com/kato/grabrr/lib/helper"
|
||||
strux "tea.chunkbyte.com/kato/grabrr/lib/strux"
|
||||
|
||||
"github.com/PuerkitoBio/goquery"
|
||||
@@ -39,9 +39,15 @@ func FetchAlbumInfo(rawURL string) (*strux.Album, error) {
|
||||
return nil, fmt.Errorf("failed to parse URL: %w", err)
|
||||
}
|
||||
|
||||
album.ID = helper.GetLastPathElement(album.URL)
|
||||
// Title
|
||||
album.Title = strings.TrimSpace(doc.Find("h1.truncate").First().Text())
|
||||
|
||||
// Views
|
||||
if albumViews, err := album.FetchAlbumViews(); err == nil {
|
||||
album.Views = albumViews
|
||||
}
|
||||
|
||||
// Files
|
||||
doc.Find(".grid-images .theItem").Each(func(i int, s *goquery.Selection) {
|
||||
linkTag := s.Find("a")
|
||||
@@ -58,7 +64,7 @@ func FetchAlbumInfo(rawURL string) (*strux.Album, error) {
|
||||
}
|
||||
|
||||
if theSizeEl := s.Find(".theSize"); theSizeEl != nil {
|
||||
fileSize, err = ParseSizeToKB(theSizeEl.Text())
|
||||
fileSize, err = helper.ParseSizeToKB(theSizeEl.Text())
|
||||
}
|
||||
|
||||
if href[0] == '/' {
|
||||
@@ -72,38 +78,18 @@ func FetchAlbumInfo(rawURL string) (*strux.Album, error) {
|
||||
URL: fileURL,
|
||||
Filename: fileTitle,
|
||||
SizeKB: fileSize,
|
||||
Format: fileURL.Path[strings.LastIndex(fileURL.Path, ".")+1:],
|
||||
Format: helper.GetMimeTypeFromFilename(fileTitle),
|
||||
}
|
||||
|
||||
uploadTimeStampRaw := strings.TrimSpace(s.Find(".theDate").First().Text())
|
||||
if newDate, err := helper.ParseCustomDateTime(uploadTimeStampRaw); err == nil {
|
||||
file.DateUploaded = newDate
|
||||
}
|
||||
|
||||
album.Files = append(album.Files, file)
|
||||
})
|
||||
|
||||
album.SizeKB = album.GetKbSize()
|
||||
|
||||
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