From 1263e66f452b39fd861df62d3f074c7c96b0507e Mon Sep 17 00:00:00 2001 From: video-prize-ranch Date: Sat, 10 Sep 2022 10:11:17 -0400 Subject: [PATCH] Add error page for ratelimit --- pages/embed.go | 6 ++++-- pages/post.go | 6 ++++-- pages/tag.go | 6 ++++-- pages/user.go | 35 ++++++++++++++--------------------- utils/request.go | 8 ++++++++ views/errors/429.hbs | 20 ++++++++++++++++++++ 6 files changed, 54 insertions(+), 27 deletions(-) create mode 100644 views/errors/429.hbs diff --git a/pages/embed.go b/pages/embed.go index 3adbef3..d289585 100644 --- a/pages/embed.go +++ b/pages/embed.go @@ -22,9 +22,11 @@ func HandleEmbed(c *fiber.Ctx) error { default: post, err = api.FetchMedia(c.Params("postID")) } + if err != nil && err.Error() == "ratelimited by imgur" { + return c.Status(429).Render("errors/429", nil) + } if post.Id == "" || (err != nil && strings.Contains(err.Error(), "404")) { - c.Status(404) - return c.Render("errors/404", nil) + return c.Status(404).Render("errors/404", nil) } if err != nil { return err diff --git a/pages/post.go b/pages/post.go index a96bc75..ac6cac6 100644 --- a/pages/post.go +++ b/pages/post.go @@ -23,9 +23,11 @@ func HandlePost(c *fiber.Ctx) error { default: post, err = api.FetchMedia(c.Params("postID")) } + if err != nil && err.Error() == "ratelimited by imgur" { + return c.Status(429).Render("errors/429", nil) + } if post.Id == "" || (err != nil && strings.Contains(err.Error(), "404")) { - c.Status(404) - return c.Render("errors/404", nil) + return c.Status(404).Render("errors/404", nil) } if err != nil { return err diff --git a/pages/tag.go b/pages/tag.go index a99c3e4..705d03a 100644 --- a/pages/tag.go +++ b/pages/tag.go @@ -30,12 +30,14 @@ func HandleTag(c *fiber.Ctx) error { } tag, err := api.FetchTag(c.Params("tag"), c.Query("sort"), page) + if err != nil && err.Error() == "ratelimited by imgur" { + return c.Status(429).Render("errors/429", nil) + } if err != nil { return err } if tag.Display == "" { - c.Status(404) - return c.Render("errors/404", nil) + return c.Status(404).Render("errors/404", nil) } return c.Render("tag", fiber.Map{ diff --git a/pages/user.go b/pages/user.go index cb8f301..904dc34 100644 --- a/pages/user.go +++ b/pages/user.go @@ -2,7 +2,6 @@ package pages import ( "strconv" - "sync" "codeberg.org/video-prize-ranch/rimgo/api" "codeberg.org/video-prize-ranch/rimgo/utils" @@ -25,30 +24,24 @@ func HandleUser(c *fiber.Ctx) error { pageNumber = 0 } - wg := sync.WaitGroup{} - wg.Add(2) - user, err := api.User{}, error(nil) - go func() { - defer wg.Done() - user, err = api.FetchUser(c.Params("userID")) - }() + user, err := api.FetchUser(c.Params("userID")) + if err != nil && err.Error() == "ratelimited by imgur" { + return c.Status(429).Render("errors/429", nil) + } if err != nil { return err } - - submissions, err := []api.Submission{}, error(nil) - go func() { - defer wg.Done() - submissions, err = api.FetchSubmissions(c.Params("userID"), "newest", page) - }() - if err != nil { - return err - } - - wg.Wait() if user.Username == "" { - c.Status(404) - return c.Render("errors/404", nil) + return c.Status(404).Render("errors/404", nil) + } + + submissions, err := api.FetchSubmissions(c.Params("userID"), "newest", page) + if err != nil && err.Error() == "ratelimited by imgur" { + c.Status(429) + return c.Render("errors/429", nil) + } + if err != nil { + return err } return c.Render("user", fiber.Map{ diff --git a/utils/request.go b/utils/request.go index cb8e20e..0e6ebcf 100644 --- a/utils/request.go +++ b/utils/request.go @@ -4,6 +4,7 @@ import ( "fmt" "io" "net/http" + "strconv" "github.com/tidwall/gjson" ) @@ -30,6 +31,13 @@ func GetJSON(url string) (gjson.Result, error) { if err != nil { return gjson.Result{}, err } + rateLimitRemaining := res.Header.Get("X-RateLimit-UserRemaining") + if rateLimitRemaining != "" { + ratelimit, _ := strconv.Atoi(rateLimitRemaining) + if ratelimit <= 0 { + return gjson.Result{}, fmt.Errorf("ratelimited by imgur") + } + } body, err := io.ReadAll(res.Body) if err != nil { diff --git a/views/errors/429.hbs b/views/errors/429.hbs new file mode 100644 index 0000000..2170c37 --- /dev/null +++ b/views/errors/429.hbs @@ -0,0 +1,20 @@ + + + + + {{> 'partials/head' }} + + 404 - rimgo + + + + {{> 'partials/header' }} + +
+

Rate limited by Imgur

+

This instance has been temporarily blocked by Imgur. Try using another instance.

+
+ + + + \ No newline at end of file