mirror of
https://codeberg.org/video-prize-ranch/rimgo.git
synced 2026-01-27 17:11:13 +00:00
gofmt
This commit is contained in:
@@ -7,15 +7,15 @@ import (
|
||||
)
|
||||
|
||||
type Client struct {
|
||||
ClientID string
|
||||
Cache *cache.Cache
|
||||
ClientID string
|
||||
Cache *cache.Cache
|
||||
}
|
||||
|
||||
func NewClient(clientId string) (*Client) {
|
||||
client := Client{
|
||||
ClientID: clientId,
|
||||
Cache: cache.New(15*time.Minute, 15*time.Minute),
|
||||
}
|
||||
|
||||
return &client
|
||||
}
|
||||
func NewClient(clientId string) *Client {
|
||||
client := Client{
|
||||
ClientID: clientId,
|
||||
Cache: cache.New(15*time.Minute, 15*time.Minute),
|
||||
}
|
||||
|
||||
return &client
|
||||
}
|
||||
|
||||
@@ -11,19 +11,19 @@ import (
|
||||
)
|
||||
|
||||
type SearchResult struct {
|
||||
Id string
|
||||
Url string
|
||||
ImageUrl string
|
||||
Title string
|
||||
User string
|
||||
Points string
|
||||
Views string
|
||||
RelTime string
|
||||
Id string
|
||||
Url string
|
||||
ImageUrl string
|
||||
Title string
|
||||
User string
|
||||
Points string
|
||||
Views string
|
||||
RelTime string
|
||||
}
|
||||
|
||||
func (client *Client) Search(query string, page string) ([]SearchResult, error) {
|
||||
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 {
|
||||
return []SearchResult{}, err
|
||||
}
|
||||
@@ -35,16 +35,16 @@ func (client *Client) Search(query string, page string) ([]SearchResult, error)
|
||||
}
|
||||
defer res.Body.Close()
|
||||
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)
|
||||
if err != nil {
|
||||
return []SearchResult{}, err
|
||||
}
|
||||
if err != nil {
|
||||
return []SearchResult{}, err
|
||||
}
|
||||
|
||||
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")
|
||||
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")
|
||||
|
||||
result := SearchResult{
|
||||
Id: strings.Split(url, "/")[2],
|
||||
Url: url,
|
||||
Id: strings.Split(url, "/")[2],
|
||||
Url: url,
|
||||
ImageUrl: strings.ReplaceAll(imageUrl, "//i.imgur.com", ""),
|
||||
Title: s.Find(".search-item-title a").Text(),
|
||||
User: s.Find(".account").Text(),
|
||||
Views: views,
|
||||
Points: points,
|
||||
RelTime: strings.TrimSpace(postInfo[2]),
|
||||
Title: s.Find(".search-item-title a").Text(),
|
||||
User: s.Find(".account").Text(),
|
||||
Views: views,
|
||||
Points: points,
|
||||
RelTime: strings.TrimSpace(postInfo[2]),
|
||||
}
|
||||
|
||||
results = append(results, result)
|
||||
})
|
||||
|
||||
return results, nil
|
||||
}
|
||||
}
|
||||
|
||||
@@ -7,7 +7,6 @@ import (
|
||||
"github.com/gofiber/fiber/v2"
|
||||
)
|
||||
|
||||
|
||||
func HandleAbout(c *fiber.Ctx) error {
|
||||
utils.SetHeaders(c)
|
||||
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")
|
||||
|
||||
return c.Render("about", fiber.Map{
|
||||
"proto": c.Protocol(),
|
||||
"domain": c.Hostname(),
|
||||
"proto": c.Protocol(),
|
||||
"domain": c.Hostname(),
|
||||
"force_webp": os.Getenv("FORCE_WEBP"),
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
@@ -8,5 +8,5 @@ import (
|
||||
var ApiClient *api.Client
|
||||
|
||||
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") {
|
||||
return utils.RenderError(c, 404)
|
||||
}
|
||||
if err != nil {
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
@@ -45,4 +45,4 @@ func HandleGifv(c *fiber.Ctx) error {
|
||||
return c.Render("gifv", fiber.Map{
|
||||
"id": c.Params("postID"),
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
@@ -17,4 +17,4 @@ func HandleFrontpage(c *fiber.Ctx) error {
|
||||
"config": utils.Config,
|
||||
"version": VersionInfo,
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
@@ -35,11 +35,11 @@ func handleMedia(c *fiber.Ctx, url string) error {
|
||||
utils.SetHeaders(c)
|
||||
|
||||
if utils.Config.ForceWebp &&
|
||||
!strings.HasSuffix(c.Path(), ".webp") &&
|
||||
c.Get("Sec-Fetch-Dest") == "image" &&
|
||||
c.Query("no_webp") == "" &&
|
||||
c.Accepts("image/webp") == "image/webp" &&
|
||||
!strings.HasPrefix(c.Path(), "/stack") {
|
||||
!strings.HasSuffix(c.Path(), ".webp") &&
|
||||
c.Get("Sec-Fetch-Dest") == "image" &&
|
||||
c.Query("no_webp") == "" &&
|
||||
c.Accepts("image/webp") == "image/webp" &&
|
||||
!strings.HasPrefix(c.Path(), "/stack") {
|
||||
url = strings.ReplaceAll(url, ".png", ".webp")
|
||||
url = strings.ReplaceAll(url, ".jpg", ".webp")
|
||||
url = strings.ReplaceAll(url, ".jpeg", ".webp")
|
||||
|
||||
@@ -35,10 +35,10 @@ func HandleSearch(c *fiber.Ctx) error {
|
||||
}
|
||||
|
||||
return c.Render("search", fiber.Map{
|
||||
"query": query,
|
||||
"results": results,
|
||||
"page": pageNumber,
|
||||
"nextPage": pageNumber + 1,
|
||||
"prevPage": pageNumber - 1,
|
||||
"query": query,
|
||||
"results": results,
|
||||
"page": pageNumber,
|
||||
"nextPage": pageNumber + 1,
|
||||
"prevPage": pageNumber - 1,
|
||||
})
|
||||
}
|
||||
|
||||
@@ -42,11 +42,11 @@ func HandleTrending(c *fiber.Ctx) error {
|
||||
}
|
||||
|
||||
return c.Render("trending", fiber.Map{
|
||||
"results": results,
|
||||
"section": section,
|
||||
"sort": sort,
|
||||
"page": pageNumber,
|
||||
"nextPage": pageNumber + 1,
|
||||
"prevPage": pageNumber - 1,
|
||||
"results": results,
|
||||
"section": section,
|
||||
"sort": sort,
|
||||
"page": pageNumber,
|
||||
"nextPage": pageNumber + 1,
|
||||
"prevPage": pageNumber - 1,
|
||||
})
|
||||
}
|
||||
|
||||
@@ -7,4 +7,4 @@ var files embed.FS
|
||||
|
||||
func GetFiles() embed.FS {
|
||||
return files
|
||||
}
|
||||
}
|
||||
|
||||
@@ -18,8 +18,8 @@ func RenderError(c *fiber.Ctx, code int) error {
|
||||
c.Set("Content-Type", "image/png")
|
||||
return c.Status(code).Send(img)
|
||||
} 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(),
|
||||
})
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -9,4 +9,4 @@ func FormatDate(date string) (string, error) {
|
||||
}
|
||||
|
||||
return time.Format("Jan 2, 2006 3:04 PM"), nil
|
||||
}
|
||||
}
|
||||
|
||||
@@ -2,4 +2,4 @@ package utils
|
||||
|
||||
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
|
||||
}
|
||||
|
||||
switch (res.StatusCode) {
|
||||
switch res.StatusCode {
|
||||
case 200:
|
||||
return gjson.Parse(string(body)), nil
|
||||
case 429:
|
||||
@@ -43,4 +43,4 @@ func GetJSON(url string) (gjson.Result, error) {
|
||||
default:
|
||||
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-Site", "same-site")
|
||||
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 {
|
||||
return files
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user