rimgo/api/album.go

160 lines
4.1 KiB
Go
Raw Normal View History

2022-01-17 20:23:04 +00:00
package api
import (
"io"
"net/http"
"strings"
2022-03-16 02:56:34 +00:00
"time"
2022-01-17 20:23:04 +00:00
2022-01-17 22:34:05 +00:00
"codeberg.org/video-prize-ranch/rimgo/types"
2022-01-18 23:05:06 +00:00
"codeberg.org/video-prize-ranch/rimgo/utils"
2022-03-16 02:56:34 +00:00
"github.com/patrickmn/go-cache"
2022-01-17 20:23:04 +00:00
"github.com/tidwall/gjson"
)
2022-03-16 02:56:34 +00:00
var albumCache = cache.New(1*time.Hour, 15*time.Minute)
2022-01-17 20:23:04 +00:00
func FetchAlbum(albumID string) (types.Album, error) {
2022-03-16 02:56:34 +00:00
cacheData, found := albumCache.Get(albumID + "-album")
if found {
return cacheData.(types.Album), nil
}
2022-01-17 20:23:04 +00:00
2022-04-22 15:55:53 +00:00
res, err := http.Get("https://api.imgur.com/post/v1/albums/" + albumID + "?client_id=" + utils.Config["imgurId"].(string) + "&include=media%2Caccount")
2022-01-17 20:23:04 +00:00
if err != nil {
return types.Album{}, err
}
body, err := io.ReadAll(res.Body)
if err != nil {
return types.Album{}, err
}
data := gjson.Parse(string(body))
2022-01-20 21:09:57 +00:00
album := types.Album{}
if data.Get("shared_with_community").Bool() || res.StatusCode == 404 {
2022-01-20 21:09:57 +00:00
album, err = FetchPosts(albumID)
2022-01-26 21:28:03 +00:00
} else {
album, err = ParseAlbum(data)
2022-01-20 21:09:57 +00:00
}
2022-01-24 01:59:29 +00:00
2022-03-16 02:56:34 +00:00
albumCache.Set(albumID + "-album", album, cache.DefaultExpiration)
2022-01-20 21:09:57 +00:00
return album, err
}
func FetchPosts(albumID string) (types.Album, error) {
2022-03-16 02:56:34 +00:00
cacheData, found := albumCache.Get(albumID + "-posts")
if found {
return cacheData.(types.Album), nil
}
2022-04-22 15:55:53 +00:00
res, err := http.Get("https://api.imgur.com/post/v1/posts/" + albumID + "?client_id=" + utils.Config["imgurId"].(string) + "&include=media%2Caccount%2Ctags")
2022-01-20 21:09:57 +00:00
if err != nil {
return types.Album{}, err
}
body, err := io.ReadAll(res.Body)
if err != nil {
return types.Album{}, err
}
data := gjson.Parse(string(body))
2022-03-16 02:56:34 +00:00
album, err := ParseAlbum(data)
if err != nil {
return types.Album{}, err
}
2022-03-16 02:56:34 +00:00
albumCache.Set(albumID + "-posts", album, cache.DefaultExpiration)
return album, nil
}
func FetchMedia(mediaID string) (types.Album, error) {
2022-03-16 02:56:34 +00:00
cacheData, found := albumCache.Get(mediaID + "-media")
if found {
return cacheData.(types.Album), nil
}
2022-04-22 15:55:53 +00:00
res, err := http.Get("https://api.imgur.com/post/v1/media/" + mediaID + "?client_id=" + utils.Config["imgurId"].(string) + "&include=media%2Caccount")
if err != nil {
return types.Album{}, err
2022-01-20 21:09:57 +00:00
}
body, err := io.ReadAll(res.Body)
if err != nil {
return types.Album{}, err
}
data := gjson.Parse(string(body))
2022-03-16 02:56:34 +00:00
album, err := ParseAlbum(data)
if err != nil {
return types.Album{}, err
}
2022-01-24 01:59:29 +00:00
2022-03-16 02:56:34 +00:00
albumCache.Set(mediaID + "-media", album, cache.DefaultExpiration)
return album, nil
2022-01-20 21:09:57 +00:00
}
func ParseAlbum(data gjson.Result) (types.Album, error) {
2022-01-17 22:11:33 +00:00
media := make([]types.Media, 0)
2022-01-17 20:23:04 +00:00
data.Get("media").ForEach(
func(key gjson.Result, value gjson.Result) bool {
url := value.Get("url").String()
2022-01-17 22:47:33 +00:00
url = strings.ReplaceAll(url, "https://i.imgur.com", "")
2022-01-17 22:11:33 +00:00
media = append(media, types.Media{
2022-01-17 22:34:05 +00:00
Id: value.Get("id").String(),
Name: value.Get("name").String(),
2022-01-18 23:05:06 +00:00
MimeType: value.Get("mime_type").String(),
Type: value.Get("type").String(),
2022-01-17 22:34:05 +00:00
Title: value.Get("metadata.title").String(),
2022-01-17 22:11:33 +00:00
Description: value.Get("metadata.description").String(),
2022-01-17 22:34:05 +00:00
Url: url,
2022-01-17 22:11:33 +00:00
})
2022-01-17 20:23:04 +00:00
return true
},
)
2022-02-21 17:14:00 +00:00
tags := make([]types.Tag, 0)
data.Get("tags").ForEach(
func(key gjson.Result, value gjson.Result) bool {
tags = append(tags, types.Tag{
Tag: value.Get("tag").String(),
Display: value.Get("display").String(),
Background: "/" + value.Get("background_id").String() + ".webp",
})
return true
},
)
2022-01-18 23:05:06 +00:00
createdAt, err := utils.FormatDate(data.Get("created_at").String())
2022-01-17 20:23:04 +00:00
if err != nil {
return types.Album{}, err
}
2022-02-02 15:46:47 +00:00
album := types.Album{
2022-01-24 01:59:29 +00:00
Id: data.Get("id").String(),
Title: data.Get("title").String(),
SharedWithCommunity: data.Get("shared_with_community").Bool(),
Views: data.Get("view_count").Int(),
Upvotes: data.Get("upvote_count").Int(),
Downvotes: data.Get("downvote_count").Int(),
Comments: data.Get("comment_count").Int(),
CreatedAt: createdAt,
Media: media,
2022-02-21 17:14:00 +00:00
Tags: tags,
2022-02-02 15:46:47 +00:00
}
account := data.Get("account")
if account.Raw != "" {
album.User = types.User{
Id: account.Get("id").Int(),
Username: account.Get("username").String(),
Avatar: strings.ReplaceAll(account.Get("avatar_url").String(), "https://i.imgur.com", ""),
}
}
return album, nil
2022-01-24 01:59:29 +00:00
}