From b526efc7adb0469000db45d1fb40d785b3e03b41 Mon Sep 17 00:00:00 2001 From: video-prize-ranch Date: Thu, 10 Aug 2023 14:22:42 +0000 Subject: [PATCH] Switch to scraping card view for more info --- api/search.go | 19 ++++++++++++------- main.go | 2 +- views/partials/result.hbs | 36 +++++++++++++++++++++++------------- 3 files changed, 36 insertions(+), 21 deletions(-) diff --git a/api/search.go b/api/search.go index 2c03060..734d6ed 100644 --- a/api/search.go +++ b/api/search.go @@ -15,13 +15,15 @@ type SearchResult struct { Url string ImageUrl string Title string + User string Points string Views string + RelTime string } func (client *Client) Search(query string) ([]SearchResult, error) { query = url.QueryEscape(query) - req, err := http.NewRequest("GET", "https://imgur.com/search?q=" + query, nil) + req, err := http.NewRequest("GET", "https://imgur.com/search?qs=list&q=" + query, nil) if err != nil { return []SearchResult{}, err } @@ -42,23 +44,26 @@ func (client *Client) Search(query string) ([]SearchResult, error) { } results := []SearchResult{} - doc.Find(".post").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") - views := s.Find(".post-info").Text() - views = strings.Split(views, "·")[1] - views = strings.TrimSpace(views) + postInfo := strings.Split(s.Find(".post-info").Text(), "·") + points := strings.TrimSpace(postInfo[0]) + points = strings.TrimSuffix(points, " points") + views := strings.TrimSpace(postInfo[1]) views = strings.TrimSuffix(views, " views") result := SearchResult{ Id: strings.Split(url, "/")[2], Url: url, ImageUrl: strings.ReplaceAll(imageUrl, "//i.imgur.com", ""), - Title: s.Find(".hover p").Text(), + Title: s.Find(".search-item-title a").Text(), + User: s.Find(".account").Text(), Views: views, + Points: points, + RelTime: strings.TrimSpace(postInfo[2]), } - result.Points = s.Find(".points-" + result.Id).Text() results = append(results, result) }) diff --git a/main.go b/main.go index c2e8c4f..ce0572f 100644 --- a/main.go +++ b/main.go @@ -94,7 +94,7 @@ func main() { Expiration: 30 * time.Minute, MaxBytes: 25000000, KeyGenerator: func(c *fiber.Ctx) string { - return c.Path() + c.Query("q") + c.Query("no_webp") + return c.OriginalURL() }, CacheControl: true, StoreResponseHeaders: true, diff --git a/views/partials/result.hbs b/views/partials/result.hbs index 2c13569..a310cbb 100644 --- a/views/partials/result.hbs +++ b/views/partials/result.hbs @@ -1,16 +1,26 @@ - -
+
+ -

{{Title}}

-
-
- Points - {{Points}} -
-
- Views - {{Views}} -
+
+

+ {{Title}}
+ {{#if User}} + by {{User}} + {{else}} +
+ {{/if}} +

+
+
+ {{RelTime}} +
+
+ Points + {{Points}} +
+
+ Views + {{Views}}
- \ No newline at end of file +
\ No newline at end of file