mirror of
https://codeberg.org/video-prize-ranch/rimgo.git
synced 2026-04-18 21:55:02 -04:00
14192e2943
* move sanitization code to views * split Tag into Tag and the subset TagMeta to match Imgur API * make the code to parse an array generic * change some of the views to match
117 lines
3.2 KiB
Go
117 lines
3.2 KiB
Go
package api
|
|
|
|
import (
|
|
"encoding/json"
|
|
"time"
|
|
|
|
"codeberg.org/rimgo/rimgo/utils"
|
|
)
|
|
|
|
type Album struct {
|
|
ID string `json:"id"`
|
|
Title string `json:"title"`
|
|
SharedWithCommunity bool `json:"shared_with_community"`
|
|
ViewCount int `json:"view_count"`
|
|
UpvoteCount int `json:"upvote_count"`
|
|
DownvoteCount int `json:"downvote_count"`
|
|
PointCount int `json:"point_count"`
|
|
CommentCount int `json:"comment_count"`
|
|
Media []Media `json:"media"`
|
|
Tags array[TagMeta] `json:"tags"`
|
|
Account _ApiUser `json:"account"`
|
|
}
|
|
|
|
type Media struct {
|
|
Id string `json:"id"`
|
|
Name string `json:"name"`
|
|
Title string `json:"-"`
|
|
Description string `json:"description"` // used outside metadata in user page cover
|
|
Url string `json:"url"`
|
|
Type string `json:"type"`
|
|
MimeType string `json:"mime_type"`
|
|
}
|
|
|
|
func (media *Media) UnmarshalJSON(data []byte) error {
|
|
type PlainMedia Media // type alias to prevent calling this function again
|
|
var m struct {
|
|
PlainMedia
|
|
// Media type is used for user page covers too, but without the metadata field in JSON
|
|
// so it makes more sense to have these fields at top level as they were before
|
|
Metadata struct {
|
|
Title string `json:"title"`
|
|
Description string `json:"description"`
|
|
} `json:"metadata"`
|
|
}
|
|
err := json.Unmarshal(data, &m)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
*media = Media(m.PlainMedia)
|
|
media.Title = m.Metadata.Title
|
|
media.Description = m.Metadata.Description
|
|
return nil
|
|
}
|
|
|
|
func (client *Client) FetchAlbum(albumID string) (Album, error) {
|
|
cacheData, found := client.Cache.Get(albumID + "-album")
|
|
if found {
|
|
return cacheData.(Album), nil
|
|
}
|
|
|
|
data, err := utils.GetJSONNew("https://api.imgur.com/post/v1/albums/" + albumID + "?client_id=" + client.ClientID + "&include=media%2Caccount")
|
|
if err != nil {
|
|
return Album{}, err
|
|
}
|
|
|
|
var album Album
|
|
err = json.Unmarshal(data, &album)
|
|
if err != nil {
|
|
return Album{}, err
|
|
}
|
|
|
|
client.Cache.Set(albumID+"-album", album, 1*time.Hour)
|
|
return album, err
|
|
}
|
|
|
|
func (client *Client) FetchPosts(albumID string) (Album, error) {
|
|
cacheData, found := client.Cache.Get(albumID + "-posts")
|
|
if found {
|
|
return cacheData.(Album), nil
|
|
}
|
|
|
|
data, err := utils.GetJSONNew("https://api.imgur.com/post/v1/posts/" + albumID + "?client_id=" + client.ClientID + "&include=media%2Caccount%2Ctags")
|
|
if err != nil {
|
|
return Album{}, err
|
|
}
|
|
|
|
var album Album
|
|
err = json.Unmarshal(data, &album)
|
|
if err != nil {
|
|
return Album{}, err
|
|
}
|
|
|
|
client.Cache.Set(albumID+"-posts", album, 1*time.Hour)
|
|
return album, nil
|
|
}
|
|
|
|
func (client *Client) FetchMedia(mediaID string) (Album, error) {
|
|
cacheData, found := client.Cache.Get(mediaID + "-media")
|
|
if found {
|
|
return cacheData.(Album), nil
|
|
}
|
|
|
|
data, err := utils.GetJSONNew("https://api.imgur.com/post/v1/media/" + mediaID + "?client_id=" + client.ClientID + "&include=media%2Caccount")
|
|
if err != nil {
|
|
return Album{}, err
|
|
}
|
|
|
|
var album Album
|
|
err = json.Unmarshal(data, &album)
|
|
if err != nil {
|
|
return Album{}, err
|
|
}
|
|
|
|
client.Cache.Set(mediaID+"-media", album, 1*time.Hour)
|
|
return album, nil
|
|
}
|