add cache, fix query

This commit is contained in:
orangix 2023-08-14 22:49:04 +02:00
parent d17d06853f
commit be88c02d99
No known key found for this signature in database
GPG Key ID: C31D4A86601C8416
2 changed files with 28 additions and 18 deletions

View File

@ -1,39 +1,35 @@
package api package api
import ( import (
"io/ioutil" "fmt"
"io"
"net/http" "net/http"
"os"
"strings" "strings"
"sync" "sync"
"codeberg.org/rimgo/rimgo/utils"
"github.com/patrickmn/go-cache"
"github.com/tidwall/gjson" "github.com/tidwall/gjson"
) )
func (client *Client) FetchTrending(section, sort, page string) ([]Submission, error) { func (client *Client) FetchTrending(section, sort, page string) ([]Submission, error) {
req, err := http.NewRequest("GET", "https://api.imgur.com/post/v1/posts?client_id="+client.ClientID+"&filter[section]=eq:"+section+"&include=cover,viral&page=1", nil) cacheData, found := client.Cache.Get(fmt.Sprintf("trending-%s-%s-%s", section, sort, page))
if found {
return cacheData.([]Submission), nil
}
req, err := http.NewRequest("GET", "https://api.imgur.com/post/v1/posts", nil)
if err != nil { if err != nil {
return []Submission{}, err return []Submission{}, err
} }
utils.SetReqHeaders(req)
q := req.URL.Query() q := req.URL.Query()
q.Add("client_id", client.ClientID) q.Add("client_id", client.ClientID)
q.Add("include", "cover") q.Add("include", "cover")
q.Add("page", page) q.Add("page", page)
switch section {
case "hot":
q.Add("filter[section]", "eq:hot")
q.Add("sort", "-viral")
case "new":
q.Add("filter[section]", "eq:new")
q.Add("sort", "-viral")
case "top":
q.Add("filter[section]", "eq:top")
q.Add("sort", "-viral")
default:
q.Add("filter[section]", "eq:hot")
section = "hot"
}
switch sort { switch sort {
case "newest": case "newest":
q.Add("filter[window]", "week") q.Add("filter[window]", "week")
@ -48,6 +44,18 @@ func (client *Client) FetchTrending(section, sort, page string) ([]Submission, e
q.Add("sort", "-viral") q.Add("sort", "-viral")
sort = "popular" sort = "popular"
} }
switch section {
case "hot":
q.Add("filter[section]", "eq:hot")
case "new":
q.Add("filter[section]", "eq:new")
case "top":
q.Add("filter[section]", "eq:top")
q.Add("filter[window]", "day")
default:
q.Add("filter[section]", "eq:hot")
section = "hot"
}
req.URL.RawQuery = q.Encode() req.URL.RawQuery = q.Encode()
@ -56,11 +64,12 @@ func (client *Client) FetchTrending(section, sort, page string) ([]Submission, e
return []Submission{}, err return []Submission{}, err
} }
body, err := ioutil.ReadAll(res.Body) body, err := io.ReadAll(res.Body)
if err != nil { if err != nil {
return []Submission{}, err return []Submission{}, err
} }
os.Stdout.Write(body)
data := gjson.Parse(string(body)) data := gjson.Parse(string(body))
wg := sync.WaitGroup{} wg := sync.WaitGroup{}
@ -95,6 +104,7 @@ func (client *Client) FetchTrending(section, sort, page string) ([]Submission, e
wg.Wait() wg.Wait()
client.Cache.Set(fmt.Sprintf("trending-%s-%s-%s", section, sort, page), posts, cache.DefaultExpiration)
// TODO: Cache trending // TODO: Cache trending
// client.Cache.Set(tag+sort+page+"-tag", tagData, cache.DefaultExpiration) // client.Cache.Set(tag+sort+page+"-tag", tagData, cache.DefaultExpiration)
return posts, nil return posts, nil

View File

@ -2,7 +2,7 @@
<html lang="en"> <html lang="en">
<head> <head>
<title>{{query}} - rimgo</title> <title>Trending - rimgo</title>
{{> partials/head }} {{> partials/head }}
</head> </head>