Switch to scraping card view for more info

This commit is contained in:
video-prize-ranch 2023-08-10 14:22:42 +00:00 committed by video-prize-ranch
parent 5dc95f4470
commit b526efc7ad
3 changed files with 36 additions and 21 deletions

View File

@ -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)
})

View File

@ -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,

View File

@ -1,16 +1,26 @@
<a href="{{Url}}">
<div class="bg-slate-600 rounded-lg">
<div class="bg-slate-600 rounded-lg">
<a href="{{Url}}">
<img src="{{ImageUrl}}?no_webp=1" loading="lazy" width="100%" height="100%">
<p class="m-2 text-ellipsis whitespace-nowrap overflow-hidden">{{Title}}</p>
<div class="flex gap-2 p-2">
<div class="flex gap-1">
<img class="invert icon" src="/static/icons/PhArrowFatUp.svg" alt="Points" width="18px" height="18px">
{{Points}}
</div>
<div class="flex gap-1">
<img class="invert icon" src="/static/icons/PhEye.svg" alt="Views" width="18px" height="18px">
{{Views}}
</div>
</a>
<p class="m-2 text-ellipsis whitespace-nowrap overflow-hidden">
<a href="{{Url}}">{{Title}}</a><br />
{{#if User}}
by <a href="/user/{{User}}">{{User}}</a>
{{else}}
<br />
{{/if}}
</p>
<div class="flex gap-2 p-2">
<div class="flex gap-1">
{{RelTime}}
</div>
<div class="flex gap-1">
<img class="invert icon" src="/static/icons/PhArrowFatUp.svg" alt="Points" width="18px" height="18px">
{{Points}}
</div>
<div class="flex gap-1">
<img class="invert icon" src="/static/icons/PhEye.svg" alt="Views" width="18px" height="18px">
{{Views}}
</div>
</div>
</a>
</div>