rimgo/api/album.go

164 lines
4.2 KiB
Go
Raw Normal View History

2022-01-17 20:23:04 +00:00
package api
import (
"strings"
2022-03-16 02:56:34 +00:00
"time"
2022-01-17 20:23:04 +00:00
2022-01-18 23:05:06 +00:00
"codeberg.org/video-prize-ranch/rimgo/utils"
2022-10-13 21:29:05 +00:00
"github.com/microcosm-cc/bluemonday"
2022-01-17 20:23:04 +00:00
"github.com/tidwall/gjson"
)
2022-07-22 15:55:22 +00:00
type Album struct {
Id string
Title string
Views int64
Upvotes int64
Downvotes int64
SharedWithCommunity bool
CreatedAt string
UpdatedAt string
Comments int64
2022-07-22 16:20:51 +00:00
User User
2022-07-22 15:55:22 +00:00
Media []Media
2022-07-22 16:20:51 +00:00
Tags []Tag
2022-07-22 15:55:22 +00:00
}
type Media struct {
Id string
Name string
Title string
Description string
Url string
2022-07-22 16:20:51 +00:00
Type string
MimeType string
2022-07-22 15:55:22 +00:00
}
2023-01-01 20:12:03 +00:00
func (client *Client) FetchAlbum(albumID string) (Album, error) {
cacheData, found := client.Cache.Get(albumID + "-album")
2022-03-16 02:56:34 +00:00
if found {
2022-07-22 15:55:22 +00:00
return cacheData.(Album), nil
2022-03-16 02:56:34 +00:00
}
2022-01-17 20:23:04 +00:00
2023-01-01 20:12:03 +00:00
data, err := utils.GetJSON("https://api.imgur.com/post/v1/albums/" + albumID + "?client_id=" + client.ClientID + "&include=media%2Caccount")
2022-01-17 20:23:04 +00:00
if err != nil {
2022-07-22 15:55:22 +00:00
return Album{}, err
2022-01-17 20:23:04 +00:00
}
2023-01-01 20:12:03 +00:00
album, err := parseAlbum(data)
2022-01-17 20:23:04 +00:00
if err != nil {
2022-07-22 15:55:22 +00:00
return Album{}, err
2022-01-17 20:23:04 +00:00
}
2023-01-01 20:12:03 +00:00
client.Cache.Set(albumID+"-album", album, 1*time.Hour)
2022-01-20 21:09:57 +00:00
return album, err
}
2023-01-01 20:12:03 +00:00
func (client *Client) FetchPosts(albumID string) (Album, error) {
cacheData, found := client.Cache.Get(albumID + "-posts")
2022-03-16 02:56:34 +00:00
if found {
2022-07-22 15:55:22 +00:00
return cacheData.(Album), nil
2022-03-16 02:56:34 +00:00
}
2023-01-01 20:12:03 +00:00
data, err := utils.GetJSON("https://api.imgur.com/post/v1/posts/" + albumID + "?client_id=" + client.ClientID + "&include=media%2Caccount%2Ctags")
if err != nil {
2022-07-22 15:55:22 +00:00
return Album{}, err
}
2023-01-01 20:12:03 +00:00
album, err := parseAlbum(data)
2022-03-16 02:56:34 +00:00
if err != nil {
2022-07-22 15:55:22 +00:00
return Album{}, err
2022-03-16 02:56:34 +00:00
}
2023-01-01 20:12:03 +00:00
client.Cache.Set(albumID+"-posts", album, 1*time.Hour)
2022-03-16 02:56:34 +00:00
return album, nil
}
2023-01-01 20:12:03 +00:00
func (client *Client) FetchMedia(mediaID string) (Album, error) {
cacheData, found := client.Cache.Get(mediaID + "-media")
2022-03-16 02:56:34 +00:00
if found {
2022-07-22 15:55:22 +00:00
return cacheData.(Album), nil
2022-03-16 02:56:34 +00:00
}
2022-07-22 16:20:51 +00:00
2023-01-01 20:12:03 +00:00
data, err := utils.GetJSON("https://api.imgur.com/post/v1/media/" + mediaID + "?client_id=" + client.ClientID + "&include=media%2Caccount")
2022-01-20 21:09:57 +00:00
if err != nil {
2022-07-22 15:55:22 +00:00
return Album{}, err
2022-01-20 21:09:57 +00:00
}
2023-01-01 20:12:03 +00:00
album, err := parseAlbum(data)
2022-03-16 02:56:34 +00:00
if err != nil {
2022-07-22 15:55:22 +00:00
return Album{}, err
2022-03-16 02:56:34 +00:00
}
2022-01-24 01:59:29 +00:00
2023-01-01 20:12:03 +00:00
client.Cache.Set(mediaID+"-media", album, 1*time.Hour)
2022-03-16 02:56:34 +00:00
return album, nil
2022-01-20 21:09:57 +00:00
}
2023-01-01 20:12:03 +00:00
func parseAlbum(data gjson.Result) (Album, error) {
2022-07-22 15:55:22 +00:00
media := make([]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
2022-10-13 21:29:05 +00:00
description := value.Get("metadata.description").String()
description = strings.ReplaceAll(description, "\n", "<br>")
description = bluemonday.UGCPolicy().Sanitize(description)
2022-07-22 15:55:22 +00:00
media = append(media, 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-10-13 21:29:05 +00:00
Description: description,
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-07-22 15:55:22 +00:00
tags := make([]Tag, 0)
2022-02-21 17:14:00 +00:00
data.Get("tags").ForEach(
func(key gjson.Result, value gjson.Result) bool {
2022-07-22 15:55:22 +00:00
tags = append(tags, Tag{
2022-07-22 16:20:51 +00:00
Tag: value.Get("tag").String(),
Display: value.Get("display").String(),
Background: "/" + value.Get("background_id").String() + ".webp",
BackgroundId: value.Get("background_id").String(),
2022-02-21 17:14:00 +00:00
})
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 {
2022-07-22 15:55:22 +00:00
return Album{}, err
2022-01-17 20:23:04 +00:00
}
2022-07-22 15:55:22 +00:00
album := 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-07-22 16:20:51 +00:00
Tags: tags,
2022-02-02 15:46:47 +00:00
}
account := data.Get("account")
if account.Raw != "" {
2022-07-22 15:55:22 +00:00
album.User = User{
2022-07-22 16:20:51 +00:00
Id: account.Get("id").Int(),
2022-02-02 15:46:47 +00:00
Username: account.Get("username").String(),
2022-07-22 16:20:51 +00:00
Avatar: strings.ReplaceAll(account.Get("avatar_url").String(), "https://i.imgur.com", ""),
2022-02-02 15:46:47 +00:00
}
}
return album, nil
2022-01-24 01:59:29 +00:00
}