96 lines
2.1 KiB
Go
96 lines
2.1 KiB
Go
package lib
|
|
|
|
import (
|
|
"fmt"
|
|
"net/http"
|
|
"net/url"
|
|
"strings"
|
|
|
|
"tea.chunkbyte.com/kato/grabrr/lib/helper"
|
|
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)
|
|
}
|
|
|
|
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")
|
|
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 = helper.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: 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
|
|
}
|