mirror of
https://codeberg.org/video-prize-ranch/rimgo.git
synced 2026-01-28 09:31:13 +00:00
gofmt
This commit is contained in:
@@ -7,15 +7,15 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
type Client struct {
|
type Client struct {
|
||||||
ClientID string
|
ClientID string
|
||||||
Cache *cache.Cache
|
Cache *cache.Cache
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewClient(clientId string) (*Client) {
|
func NewClient(clientId string) *Client {
|
||||||
client := Client{
|
client := Client{
|
||||||
ClientID: clientId,
|
ClientID: clientId,
|
||||||
Cache: cache.New(15*time.Minute, 15*time.Minute),
|
Cache: cache.New(15*time.Minute, 15*time.Minute),
|
||||||
}
|
}
|
||||||
|
|
||||||
return &client
|
return &client
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -11,19 +11,19 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
type SearchResult struct {
|
type SearchResult struct {
|
||||||
Id string
|
Id string
|
||||||
Url string
|
Url string
|
||||||
ImageUrl string
|
ImageUrl string
|
||||||
Title string
|
Title string
|
||||||
User string
|
User string
|
||||||
Points string
|
Points string
|
||||||
Views string
|
Views string
|
||||||
RelTime string
|
RelTime string
|
||||||
}
|
}
|
||||||
|
|
||||||
func (client *Client) Search(query string, page string) ([]SearchResult, error) {
|
func (client *Client) Search(query string, page string) ([]SearchResult, error) {
|
||||||
query = url.QueryEscape(query)
|
query = url.QueryEscape(query)
|
||||||
req, err := http.NewRequest("GET", "https://imgur.com/search/all/page/" + page + "?scrolled&q_size_is_mpx=off&qs=list&q=" + query, nil)
|
req, err := http.NewRequest("GET", "https://imgur.com/search/all/page/"+page+"?scrolled&q_size_is_mpx=off&qs=list&q="+query, nil)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return []SearchResult{}, err
|
return []SearchResult{}, err
|
||||||
}
|
}
|
||||||
@@ -35,16 +35,16 @@ func (client *Client) Search(query string, page string) ([]SearchResult, error)
|
|||||||
}
|
}
|
||||||
defer res.Body.Close()
|
defer res.Body.Close()
|
||||||
if res.StatusCode != 200 {
|
if res.StatusCode != 200 {
|
||||||
return []SearchResult{}, fmt.Errorf("invalid status code, got %d", res.StatusCode)
|
return []SearchResult{}, fmt.Errorf("invalid status code, got %d", res.StatusCode)
|
||||||
}
|
}
|
||||||
|
|
||||||
doc, err := goquery.NewDocumentFromReader(res.Body)
|
doc, err := goquery.NewDocumentFromReader(res.Body)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return []SearchResult{}, err
|
return []SearchResult{}, err
|
||||||
}
|
}
|
||||||
|
|
||||||
results := []SearchResult{}
|
results := []SearchResult{}
|
||||||
doc.Find(".post-list").Each(func(i int, s *goquery.Selection) {
|
doc.Find(".post-list").Each(func(i int, s *goquery.Selection) {
|
||||||
url, _ := s.Find("a").Attr("href")
|
url, _ := s.Find("a").Attr("href")
|
||||||
imageUrl, _ := s.Find("img").Attr("src")
|
imageUrl, _ := s.Find("img").Attr("src")
|
||||||
|
|
||||||
@@ -55,18 +55,18 @@ func (client *Client) Search(query string, page string) ([]SearchResult, error)
|
|||||||
views = strings.TrimSuffix(views, " views")
|
views = strings.TrimSuffix(views, " views")
|
||||||
|
|
||||||
result := SearchResult{
|
result := SearchResult{
|
||||||
Id: strings.Split(url, "/")[2],
|
Id: strings.Split(url, "/")[2],
|
||||||
Url: url,
|
Url: url,
|
||||||
ImageUrl: strings.ReplaceAll(imageUrl, "//i.imgur.com", ""),
|
ImageUrl: strings.ReplaceAll(imageUrl, "//i.imgur.com", ""),
|
||||||
Title: s.Find(".search-item-title a").Text(),
|
Title: s.Find(".search-item-title a").Text(),
|
||||||
User: s.Find(".account").Text(),
|
User: s.Find(".account").Text(),
|
||||||
Views: views,
|
Views: views,
|
||||||
Points: points,
|
Points: points,
|
||||||
RelTime: strings.TrimSpace(postInfo[2]),
|
RelTime: strings.TrimSpace(postInfo[2]),
|
||||||
}
|
}
|
||||||
|
|
||||||
results = append(results, result)
|
results = append(results, result)
|
||||||
})
|
})
|
||||||
|
|
||||||
return results, nil
|
return results, nil
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -7,7 +7,6 @@ import (
|
|||||||
"github.com/gofiber/fiber/v2"
|
"github.com/gofiber/fiber/v2"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
func HandleAbout(c *fiber.Ctx) error {
|
func HandleAbout(c *fiber.Ctx) error {
|
||||||
utils.SetHeaders(c)
|
utils.SetHeaders(c)
|
||||||
c.Set("X-Frame-Options", "DENY")
|
c.Set("X-Frame-Options", "DENY")
|
||||||
@@ -15,8 +14,8 @@ func HandleAbout(c *fiber.Ctx) error {
|
|||||||
c.Set("Content-Security-Policy", "default-src 'none'; frame-ancestors 'none'; base-uri 'none'; form-action 'self'; style-src 'self'; img-src 'self'; manifest-src 'self'; block-all-mixed-content")
|
c.Set("Content-Security-Policy", "default-src 'none'; frame-ancestors 'none'; base-uri 'none'; form-action 'self'; style-src 'self'; img-src 'self'; manifest-src 'self'; block-all-mixed-content")
|
||||||
|
|
||||||
return c.Render("about", fiber.Map{
|
return c.Render("about", fiber.Map{
|
||||||
"proto": c.Protocol(),
|
"proto": c.Protocol(),
|
||||||
"domain": c.Hostname(),
|
"domain": c.Hostname(),
|
||||||
"force_webp": os.Getenv("FORCE_WEBP"),
|
"force_webp": os.Getenv("FORCE_WEBP"),
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -8,5 +8,5 @@ import (
|
|||||||
var ApiClient *api.Client
|
var ApiClient *api.Client
|
||||||
|
|
||||||
func InitializeApiClient() {
|
func InitializeApiClient() {
|
||||||
ApiClient = api.NewClient(utils.Config.ImgurId)
|
ApiClient = api.NewClient(utils.Config.ImgurId)
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -28,7 +28,7 @@ func HandleEmbed(c *fiber.Ctx) error {
|
|||||||
if err != nil && post.Id == "" && strings.Contains(err.Error(), "404") {
|
if err != nil && post.Id == "" && strings.Contains(err.Error(), "404") {
|
||||||
return utils.RenderError(c, 404)
|
return utils.RenderError(c, 404)
|
||||||
}
|
}
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -45,4 +45,4 @@ func HandleGifv(c *fiber.Ctx) error {
|
|||||||
return c.Render("gifv", fiber.Map{
|
return c.Render("gifv", fiber.Map{
|
||||||
"id": c.Params("postID"),
|
"id": c.Params("postID"),
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -17,4 +17,4 @@ func HandleFrontpage(c *fiber.Ctx) error {
|
|||||||
"config": utils.Config,
|
"config": utils.Config,
|
||||||
"version": VersionInfo,
|
"version": VersionInfo,
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -35,11 +35,11 @@ func handleMedia(c *fiber.Ctx, url string) error {
|
|||||||
utils.SetHeaders(c)
|
utils.SetHeaders(c)
|
||||||
|
|
||||||
if utils.Config.ForceWebp &&
|
if utils.Config.ForceWebp &&
|
||||||
!strings.HasSuffix(c.Path(), ".webp") &&
|
!strings.HasSuffix(c.Path(), ".webp") &&
|
||||||
c.Get("Sec-Fetch-Dest") == "image" &&
|
c.Get("Sec-Fetch-Dest") == "image" &&
|
||||||
c.Query("no_webp") == "" &&
|
c.Query("no_webp") == "" &&
|
||||||
c.Accepts("image/webp") == "image/webp" &&
|
c.Accepts("image/webp") == "image/webp" &&
|
||||||
!strings.HasPrefix(c.Path(), "/stack") {
|
!strings.HasPrefix(c.Path(), "/stack") {
|
||||||
url = strings.ReplaceAll(url, ".png", ".webp")
|
url = strings.ReplaceAll(url, ".png", ".webp")
|
||||||
url = strings.ReplaceAll(url, ".jpg", ".webp")
|
url = strings.ReplaceAll(url, ".jpg", ".webp")
|
||||||
url = strings.ReplaceAll(url, ".jpeg", ".webp")
|
url = strings.ReplaceAll(url, ".jpeg", ".webp")
|
||||||
|
|||||||
@@ -35,10 +35,10 @@ func HandleSearch(c *fiber.Ctx) error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
return c.Render("search", fiber.Map{
|
return c.Render("search", fiber.Map{
|
||||||
"query": query,
|
"query": query,
|
||||||
"results": results,
|
"results": results,
|
||||||
"page": pageNumber,
|
"page": pageNumber,
|
||||||
"nextPage": pageNumber + 1,
|
"nextPage": pageNumber + 1,
|
||||||
"prevPage": pageNumber - 1,
|
"prevPage": pageNumber - 1,
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -42,11 +42,11 @@ func HandleTrending(c *fiber.Ctx) error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
return c.Render("trending", fiber.Map{
|
return c.Render("trending", fiber.Map{
|
||||||
"results": results,
|
"results": results,
|
||||||
"section": section,
|
"section": section,
|
||||||
"sort": sort,
|
"sort": sort,
|
||||||
"page": pageNumber,
|
"page": pageNumber,
|
||||||
"nextPage": pageNumber + 1,
|
"nextPage": pageNumber + 1,
|
||||||
"prevPage": pageNumber - 1,
|
"prevPage": pageNumber - 1,
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -7,4 +7,4 @@ var files embed.FS
|
|||||||
|
|
||||||
func GetFiles() embed.FS {
|
func GetFiles() embed.FS {
|
||||||
return files
|
return files
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -18,8 +18,8 @@ func RenderError(c *fiber.Ctx, code int) error {
|
|||||||
c.Set("Content-Type", "image/png")
|
c.Set("Content-Type", "image/png")
|
||||||
return c.Status(code).Send(img)
|
return c.Status(code).Send(img)
|
||||||
} else {
|
} else {
|
||||||
return c.Status(code).Render("errors/" + strconv.Itoa(code), fiber.Map{
|
return c.Status(code).Render("errors/"+strconv.Itoa(code), fiber.Map{
|
||||||
"path": c.Path(),
|
"path": c.Path(),
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -9,4 +9,4 @@ func FormatDate(date string) (string, error) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
return time.Format("Jan 2, 2006 3:04 PM"), nil
|
return time.Format("Jan 2, 2006 3:04 PM"), nil
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -2,4 +2,4 @@ package utils
|
|||||||
|
|
||||||
import "regexp"
|
import "regexp"
|
||||||
|
|
||||||
var ImgurRe = regexp.MustCompile(`https?://(i\.)?imgur\.com`)
|
var ImgurRe = regexp.MustCompile(`https?://(i\.)?imgur\.com`)
|
||||||
|
|||||||
@@ -35,7 +35,7 @@ func GetJSON(url string) (gjson.Result, error) {
|
|||||||
return gjson.Result{}, err
|
return gjson.Result{}, err
|
||||||
}
|
}
|
||||||
|
|
||||||
switch (res.StatusCode) {
|
switch res.StatusCode {
|
||||||
case 200:
|
case 200:
|
||||||
return gjson.Parse(string(body)), nil
|
return gjson.Parse(string(body)), nil
|
||||||
case 429:
|
case 429:
|
||||||
@@ -43,4 +43,4 @@ func GetJSON(url string) (gjson.Result, error) {
|
|||||||
default:
|
default:
|
||||||
return gjson.Result{}, fmt.Errorf("received status %s, expected 200 OK.\n%s", res.Status, string(body))
|
return gjson.Result{}, fmt.Errorf("received status %s, expected 200 OK.\n%s", res.Status, string(body))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -25,4 +25,4 @@ func SetReqHeaders(req *http.Request) {
|
|||||||
req.Header.Set("Sec-Fetch-Mode", "cors")
|
req.Header.Set("Sec-Fetch-Mode", "cors")
|
||||||
req.Header.Set("Sec-Fetch-Site", "same-site")
|
req.Header.Set("Sec-Fetch-Site", "same-site")
|
||||||
req.Header.Set("User-Agent", "Mozilla/5.0 (Windows NT 10.0; rv:102.0) Gecko/20100101 Firefox/102.0")
|
req.Header.Set("User-Agent", "Mozilla/5.0 (Windows NT 10.0; rv:102.0) Gecko/20100101 Firefox/102.0")
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -7,4 +7,4 @@ var files embed.FS
|
|||||||
|
|
||||||
func GetFiles() embed.FS {
|
func GetFiles() embed.FS {
|
||||||
return files
|
return files
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user