rimgo/api/tag.go

115 lines
2.5 KiB
Go
Raw Normal View History

2022-02-18 21:56:56 +00:00
package api
import (
"io/ioutil"
"net/http"
"strings"
"sync"
2022-03-16 02:56:34 +00:00
"time"
2022-02-18 21:56:56 +00:00
2022-04-22 15:55:53 +00:00
"codeberg.org/video-prize-ranch/rimgo/utils"
2022-03-16 02:56:34 +00:00
"github.com/patrickmn/go-cache"
2022-02-18 21:56:56 +00:00
"github.com/tidwall/gjson"
)
2022-07-22 15:55:22 +00:00
type Tag struct {
2022-07-22 16:20:51 +00:00
Tag string
Display string
Sort string
PostCount int64
Posts []Submission
Background string
BackgroundId string
2022-07-22 15:55:22 +00:00
}
2022-03-16 02:56:34 +00:00
var tagCache = cache.New(15*time.Minute, 15*time.Minute)
2022-07-22 15:55:22 +00:00
func FetchTag(tag string, sort string, page string) (Tag, error) {
2022-03-16 02:56:34 +00:00
cacheData, found := tagCache.Get(tag + sort + page)
if found {
2022-07-22 15:55:22 +00:00
return cacheData.(Tag), nil
2022-03-16 02:56:34 +00:00
}
2022-02-18 21:56:56 +00:00
req, err := http.NewRequest("GET", "https://api.imgur.com/post/v1/posts/t/"+tag, nil)
if err != nil {
2022-07-22 15:55:22 +00:00
return Tag{}, err
2022-02-18 21:56:56 +00:00
}
q := req.URL.Query()
2022-04-22 15:55:53 +00:00
q.Add("client_id", utils.Config["imgurId"].(string))
2022-02-18 21:56:56 +00:00
q.Add("include", "cover")
q.Add("page", page)
switch sort {
case "newest":
q.Add("filter[window]", "week")
q.Add("sort", "-time")
case "best":
q.Add("filter[window]", "all")
q.Add("sort", "-top")
case "popular":
default:
q.Add("filter[window]", "week")
q.Add("sort", "-viral")
sort = "popular"
}
req.URL.RawQuery = q.Encode()
res, err := http.DefaultClient.Do(req)
if err != nil {
2022-07-22 15:55:22 +00:00
return Tag{}, err
2022-02-18 21:56:56 +00:00
}
body, err := ioutil.ReadAll(res.Body)
if err != nil {
2022-07-22 15:55:22 +00:00
return Tag{}, err
2022-02-18 21:56:56 +00:00
}
data := gjson.Parse(string(body))
wg := sync.WaitGroup{}
2022-07-22 15:55:22 +00:00
posts := make([]Submission, 0)
2022-02-18 21:56:56 +00:00
data.Get("posts").ForEach(
func(key, value gjson.Result) bool {
wg.Add(1)
go func() {
defer wg.Done()
2022-07-22 15:55:22 +00:00
posts = append(posts, Submission{
2022-02-18 21:56:56 +00:00
Id: value.Get("id").String(),
Title: value.Get("title").String(),
Link: strings.ReplaceAll(value.Get("url").String(), "https://imgur.com", ""),
2022-07-22 15:55:22 +00:00
Cover: Media{
2022-07-22 16:20:51 +00:00
Id: value.Get("cover_id").String(),
2022-02-18 21:56:56 +00:00
Type: value.Get("cover.type").String(),
2022-07-22 16:20:51 +00:00
Url: strings.ReplaceAll(value.Get("cover.url").String(), "https://i.imgur.com", ""),
2022-02-18 21:56:56 +00:00
},
Points: value.Get("point_count").Int(),
Upvotes: value.Get("upvote_count").Int(),
Downvotes: value.Get("downvote_count").Int(),
Comments: value.Get("comment_count").Int(),
Views: value.Get("view_count").Int(),
IsAlbum: value.Get("is_album").Bool(),
})
}()
return true
},
)
wg.Wait()
2022-07-22 15:55:22 +00:00
tagData := Tag{
2022-07-22 16:20:51 +00:00
Tag: tag,
Display: data.Get("display").String(),
Sort: sort,
PostCount: data.Get("post_count").Int(),
Posts: posts,
Background: "/" + data.Get("background_id").String() + ".webp",
2022-03-16 02:56:34 +00:00
}
tagCache.Set(tag, tagData, cache.DefaultExpiration)
return tagData, nil
2022-02-18 21:56:56 +00:00
}