Move types to api
This commit is contained in:
parent
da42f7cf90
commit
e0efe6caca
66
api/album.go
66
api/album.go
|
@ -4,82 +4,106 @@ import (
|
|||
"strings"
|
||||
"time"
|
||||
|
||||
"codeberg.org/video-prize-ranch/rimgo/types"
|
||||
"codeberg.org/video-prize-ranch/rimgo/utils"
|
||||
"github.com/patrickmn/go-cache"
|
||||
"github.com/tidwall/gjson"
|
||||
)
|
||||
|
||||
type Album struct {
|
||||
Id string
|
||||
Title string
|
||||
Views int64
|
||||
Upvotes int64
|
||||
Downvotes int64
|
||||
SharedWithCommunity bool
|
||||
CreatedAt string
|
||||
UpdatedAt string
|
||||
Comments int64
|
||||
User User
|
||||
Media []Media
|
||||
Tags []Tag
|
||||
}
|
||||
|
||||
type Media struct {
|
||||
Id string
|
||||
Name string
|
||||
Title string
|
||||
Description string
|
||||
Url string
|
||||
Type string
|
||||
MimeType string
|
||||
}
|
||||
|
||||
var albumCache = cache.New(1*time.Hour, 15*time.Minute)
|
||||
|
||||
func FetchAlbum(albumID string) (types.Album, error) {
|
||||
func FetchAlbum(albumID string) (Album, error) {
|
||||
cacheData, found := albumCache.Get(albumID + "-album")
|
||||
if found {
|
||||
return cacheData.(types.Album), nil
|
||||
return cacheData.(Album), nil
|
||||
}
|
||||
|
||||
data, err := utils.GetJSON("https://api.imgur.com/post/v1/albums/" + albumID + "?client_id=" + utils.Config["imgurId"].(string) + "&include=media%2Caccount")
|
||||
if err != nil {
|
||||
return types.Album{}, err
|
||||
return Album{}, err
|
||||
}
|
||||
|
||||
album, err := ParseAlbum(data)
|
||||
if err != nil {
|
||||
return types.Album{}, err
|
||||
return Album{}, err
|
||||
}
|
||||
|
||||
albumCache.Set(albumID + "-album", album, cache.DefaultExpiration)
|
||||
return album, err
|
||||
}
|
||||
|
||||
func FetchPosts(albumID string) (types.Album, error) {
|
||||
func FetchPosts(albumID string) (Album, error) {
|
||||
cacheData, found := albumCache.Get(albumID + "-posts")
|
||||
if found {
|
||||
return cacheData.(types.Album), nil
|
||||
return cacheData.(Album), nil
|
||||
}
|
||||
|
||||
data, err := utils.GetJSON("https://api.imgur.com/post/v1/posts/" + albumID + "?client_id=" + utils.Config["imgurId"].(string) + "&include=media%2Caccount%2Ctags")
|
||||
if err != nil {
|
||||
return types.Album{}, err
|
||||
return Album{}, err
|
||||
}
|
||||
|
||||
album, err := ParseAlbum(data)
|
||||
if err != nil {
|
||||
return types.Album{}, err
|
||||
return Album{}, err
|
||||
}
|
||||
|
||||
albumCache.Set(albumID + "-posts", album, cache.DefaultExpiration)
|
||||
return album, nil
|
||||
}
|
||||
|
||||
func FetchMedia(mediaID string) (types.Album, error) {
|
||||
func FetchMedia(mediaID string) (Album, error) {
|
||||
cacheData, found := albumCache.Get(mediaID + "-media")
|
||||
if found {
|
||||
return cacheData.(types.Album), nil
|
||||
return cacheData.(Album), nil
|
||||
}
|
||||
|
||||
data, err := utils.GetJSON("https://api.imgur.com/post/v1/media/" + mediaID + "?client_id=" + utils.Config["imgurId"].(string) + "&include=media%2Caccount")
|
||||
if err != nil {
|
||||
return types.Album{}, err
|
||||
return Album{}, err
|
||||
}
|
||||
|
||||
album, err := ParseAlbum(data)
|
||||
if err != nil {
|
||||
return types.Album{}, err
|
||||
return Album{}, err
|
||||
}
|
||||
|
||||
albumCache.Set(mediaID + "-media", album, cache.DefaultExpiration)
|
||||
return album, nil
|
||||
}
|
||||
|
||||
func ParseAlbum(data gjson.Result) (types.Album, error) {
|
||||
media := make([]types.Media, 0)
|
||||
func ParseAlbum(data gjson.Result) (Album, error) {
|
||||
media := make([]Media, 0)
|
||||
data.Get("media").ForEach(
|
||||
func(key gjson.Result, value gjson.Result) bool {
|
||||
url := value.Get("url").String()
|
||||
url = strings.ReplaceAll(url, "https://i.imgur.com", "")
|
||||
|
||||
media = append(media, types.Media{
|
||||
media = append(media, Media{
|
||||
Id: value.Get("id").String(),
|
||||
Name: value.Get("name").String(),
|
||||
MimeType: value.Get("mime_type").String(),
|
||||
|
@ -93,10 +117,10 @@ func ParseAlbum(data gjson.Result) (types.Album, error) {
|
|||
},
|
||||
)
|
||||
|
||||
tags := make([]types.Tag, 0)
|
||||
tags := make([]Tag, 0)
|
||||
data.Get("tags").ForEach(
|
||||
func(key gjson.Result, value gjson.Result) bool {
|
||||
tags = append(tags, types.Tag{
|
||||
tags = append(tags, Tag{
|
||||
Tag: value.Get("tag").String(),
|
||||
Display: value.Get("display").String(),
|
||||
Background: "/" + value.Get("background_id").String() + ".webp",
|
||||
|
@ -107,10 +131,10 @@ func ParseAlbum(data gjson.Result) (types.Album, error) {
|
|||
|
||||
createdAt, err := utils.FormatDate(data.Get("created_at").String())
|
||||
if err != nil {
|
||||
return types.Album{}, err
|
||||
return Album{}, err
|
||||
}
|
||||
|
||||
album := types.Album{
|
||||
album := Album{
|
||||
Id: data.Get("id").String(),
|
||||
Title: data.Get("title").String(),
|
||||
SharedWithCommunity: data.Get("shared_with_community").Bool(),
|
||||
|
@ -125,7 +149,7 @@ func ParseAlbum(data gjson.Result) (types.Album, error) {
|
|||
|
||||
account := data.Get("account")
|
||||
if account.Raw != "" {
|
||||
album.User = types.User{
|
||||
album.User = User{
|
||||
Id: account.Get("id").Int(),
|
||||
Username: account.Get("username").String(),
|
||||
Avatar: strings.ReplaceAll(account.Get("avatar_url").String(), "https://i.imgur.com", ""),
|
||||
|
|
|
@ -5,28 +5,41 @@ import (
|
|||
"sync"
|
||||
"time"
|
||||
|
||||
"codeberg.org/video-prize-ranch/rimgo/types"
|
||||
"codeberg.org/video-prize-ranch/rimgo/utils"
|
||||
"github.com/dustin/go-humanize"
|
||||
"github.com/patrickmn/go-cache"
|
||||
"github.com/tidwall/gjson"
|
||||
)
|
||||
|
||||
type Comment struct {
|
||||
Comments []Comment
|
||||
User User
|
||||
Id string
|
||||
Comment string
|
||||
Upvotes int64
|
||||
Downvotes int64
|
||||
Platform string
|
||||
CreatedAt string
|
||||
RelTime string
|
||||
UpdatedAt string
|
||||
DeletedAt string
|
||||
}
|
||||
|
||||
var commentCache = cache.New(15*time.Minute, 15*time.Minute)
|
||||
|
||||
func FetchComments(galleryID string) ([]types.Comment, error) {
|
||||
func FetchComments(galleryID string) ([]Comment, error) {
|
||||
cacheData, found := commentCache.Get(galleryID)
|
||||
if found {
|
||||
return cacheData.([]types.Comment), nil
|
||||
return cacheData.([]Comment), nil
|
||||
}
|
||||
|
||||
data, err := utils.GetJSON("https://api.imgur.com/comment/v1/comments?client_id=" + utils.Config["imgurId"].(string) + "&filter[post]=eq:" + galleryID + "&include=account,adconfig&per_page=30&sort=best")
|
||||
if err != nil {
|
||||
return []types.Comment{}, nil
|
||||
return []Comment{}, nil
|
||||
}
|
||||
|
||||
wg := sync.WaitGroup{}
|
||||
comments := make([]types.Comment, 0)
|
||||
comments := make([]Comment, 0)
|
||||
data.Get("data").ForEach(
|
||||
func(key, value gjson.Result) bool {
|
||||
wg.Add(1)
|
||||
|
@ -45,7 +58,7 @@ func FetchComments(galleryID string) ([]types.Comment, error) {
|
|||
return comments, nil
|
||||
}
|
||||
|
||||
func ParseComment(data gjson.Result) types.Comment {
|
||||
func ParseComment(data gjson.Result) Comment {
|
||||
createdTime, _ := time.Parse("2006-01-02T15:04:05Z", data.Get("created_at").String())
|
||||
createdAt := createdTime.Format("January 2, 2006 3:04 PM")
|
||||
updatedAt, _ := utils.FormatDate(data.Get("updated_at").String())
|
||||
|
@ -54,7 +67,7 @@ func ParseComment(data gjson.Result) types.Comment {
|
|||
userAvatar := strings.ReplaceAll(data.Get("account.avatar").String(), "https://i.imgur.com", "")
|
||||
|
||||
wg := sync.WaitGroup{}
|
||||
comments := make([]types.Comment, 0)
|
||||
comments := make([]Comment, 0)
|
||||
data.Get("comments").ForEach(
|
||||
func(key, value gjson.Result) bool {
|
||||
wg.Add(1)
|
||||
|
@ -69,9 +82,9 @@ func ParseComment(data gjson.Result) types.Comment {
|
|||
)
|
||||
wg.Wait()
|
||||
|
||||
return types.Comment{
|
||||
return Comment{
|
||||
Comments: comments,
|
||||
User: types.User{
|
||||
User: User{
|
||||
Id: data.Get("account.id").Int(),
|
||||
Username: data.Get("account.username").String(),
|
||||
Avatar: userAvatar,
|
||||
|
|
28
api/tag.go
28
api/tag.go
|
@ -7,23 +7,31 @@ import (
|
|||
"sync"
|
||||
"time"
|
||||
|
||||
"codeberg.org/video-prize-ranch/rimgo/types"
|
||||
"codeberg.org/video-prize-ranch/rimgo/utils"
|
||||
"github.com/patrickmn/go-cache"
|
||||
"github.com/tidwall/gjson"
|
||||
)
|
||||
|
||||
type Tag struct {
|
||||
Tag string
|
||||
Display string
|
||||
Sort string
|
||||
PostCount int64
|
||||
Posts []Submission
|
||||
Background string
|
||||
}
|
||||
|
||||
var tagCache = cache.New(15*time.Minute, 15*time.Minute)
|
||||
|
||||
func FetchTag(tag string, sort string, page string) (types.Tag, error) {
|
||||
func FetchTag(tag string, sort string, page string) (Tag, error) {
|
||||
cacheData, found := tagCache.Get(tag + sort + page)
|
||||
if found {
|
||||
return cacheData.(types.Tag), nil
|
||||
return cacheData.(Tag), nil
|
||||
}
|
||||
|
||||
req, err := http.NewRequest("GET", "https://api.imgur.com/post/v1/posts/t/"+tag, nil)
|
||||
if err != nil {
|
||||
return types.Tag{}, err
|
||||
return Tag{}, err
|
||||
}
|
||||
|
||||
q := req.URL.Query()
|
||||
|
@ -49,29 +57,29 @@ func FetchTag(tag string, sort string, page string) (types.Tag, error) {
|
|||
|
||||
res, err := http.DefaultClient.Do(req)
|
||||
if err != nil {
|
||||
return types.Tag{}, err
|
||||
return Tag{}, err
|
||||
}
|
||||
|
||||
body, err := ioutil.ReadAll(res.Body)
|
||||
if err != nil {
|
||||
return types.Tag{}, err
|
||||
return Tag{}, err
|
||||
}
|
||||
|
||||
data := gjson.Parse(string(body))
|
||||
|
||||
wg := sync.WaitGroup{}
|
||||
posts := make([]types.Submission, 0)
|
||||
posts := make([]Submission, 0)
|
||||
data.Get("posts").ForEach(
|
||||
func(key, value gjson.Result) bool {
|
||||
wg.Add(1)
|
||||
|
||||
go func() {
|
||||
defer wg.Done()
|
||||
posts = append(posts, types.Submission{
|
||||
posts = append(posts, Submission{
|
||||
Id: value.Get("id").String(),
|
||||
Title: value.Get("title").String(),
|
||||
Link: strings.ReplaceAll(value.Get("url").String(), "https://imgur.com", ""),
|
||||
Cover: types.Media{
|
||||
Cover: Media{
|
||||
Id: value.Get("cover_id").String(),
|
||||
Type: value.Get("cover.type").String(),
|
||||
Url: strings.ReplaceAll(value.Get("cover.url").String(), "https://i.imgur.com", ""),
|
||||
|
@ -91,7 +99,7 @@ func FetchTag(tag string, sort string, page string) (types.Tag, error) {
|
|||
|
||||
wg.Wait()
|
||||
|
||||
tagData := types.Tag{
|
||||
tagData := Tag{
|
||||
Tag: tag,
|
||||
Display: data.Get("display").String(),
|
||||
Sort: sort,
|
||||
|
|
50
api/user.go
50
api/user.go
|
@ -7,35 +7,57 @@ import (
|
|||
"sync"
|
||||
"time"
|
||||
|
||||
"codeberg.org/video-prize-ranch/rimgo/types"
|
||||
"codeberg.org/video-prize-ranch/rimgo/utils"
|
||||
"github.com/patrickmn/go-cache"
|
||||
"github.com/tidwall/gjson"
|
||||
)
|
||||
|
||||
type User struct {
|
||||
Id int64
|
||||
Bio string
|
||||
Username string
|
||||
Points int64
|
||||
Cover string
|
||||
Avatar string
|
||||
CreatedAt string
|
||||
}
|
||||
|
||||
type Submission struct {
|
||||
Id string
|
||||
Title string
|
||||
Link string
|
||||
Cover Media
|
||||
Points int64
|
||||
Upvotes int64
|
||||
Downvotes int64
|
||||
Comments int64
|
||||
Views int64
|
||||
IsAlbum bool
|
||||
}
|
||||
|
||||
var userCache = cache.New(30*time.Minute, 15*time.Minute)
|
||||
|
||||
func FetchUser(username string) (types.User, error) {
|
||||
func FetchUser(username string) (User, error) {
|
||||
cacheData, found := userCache.Get(username)
|
||||
if found {
|
||||
return cacheData.(types.User), nil
|
||||
return cacheData.(User), nil
|
||||
}
|
||||
|
||||
res, err := http.Get("https://api.imgur.com/account/v1/accounts/" + username + "?client_id=" + utils.Config["imgurId"].(string))
|
||||
if err != nil {
|
||||
return types.User{}, err
|
||||
return User{}, err
|
||||
}
|
||||
|
||||
body, err := io.ReadAll(res.Body)
|
||||
if err != nil {
|
||||
return types.User{}, err
|
||||
return User{}, err
|
||||
}
|
||||
|
||||
data := gjson.Parse(string(body))
|
||||
|
||||
createdTime, _ := time.Parse(time.RFC3339, data.Get("created_at").String())
|
||||
|
||||
user := types.User{
|
||||
user := User{
|
||||
Id: data.Get("id").Int(),
|
||||
Bio: data.Get("bio").String(),
|
||||
Username: data.Get("username").String(),
|
||||
|
@ -49,18 +71,18 @@ func FetchUser(username string) (types.User, error) {
|
|||
return user, nil
|
||||
}
|
||||
|
||||
func FetchSubmissions(username string, sort string, page string) ([]types.Submission, error) {
|
||||
func FetchSubmissions(username string, sort string, page string) ([]Submission, error) {
|
||||
cacheData, found := userCache.Get(username + "-submissions")
|
||||
if found {
|
||||
return cacheData.([]types.Submission), nil
|
||||
return cacheData.([]Submission), nil
|
||||
}
|
||||
|
||||
data, err := utils.GetJSON("https://api.imgur.com/3/account/" + username + "/submissions/" + page + "/" + sort + "?album_previews=1&client_id=" + utils.Config["imgurId"].(string))
|
||||
if err != nil {
|
||||
return []types.Submission{}, err
|
||||
return []Submission{}, err
|
||||
}
|
||||
|
||||
submissions := []types.Submission{}
|
||||
submissions := []Submission{}
|
||||
|
||||
wg := sync.WaitGroup{}
|
||||
data.Get("data").ForEach(
|
||||
|
@ -71,16 +93,16 @@ func FetchSubmissions(username string, sort string, page string) ([]types.Submis
|
|||
defer wg.Done()
|
||||
|
||||
coverData := value.Get("images.#(id==\"" + value.Get("cover").String() + "\")")
|
||||
cover := types.Media{}
|
||||
cover := Media{}
|
||||
if coverData.Exists() {
|
||||
cover = types.Media{
|
||||
cover = Media{
|
||||
Id: coverData.Get("id").String(),
|
||||
Description: coverData.Get("description").String(),
|
||||
Type: strings.Split(coverData.Get("type").String(), "/")[0],
|
||||
Url: strings.ReplaceAll(coverData.Get("link").String(), "https://i.imgur.com", ""),
|
||||
}
|
||||
} else {
|
||||
cover = types.Media{
|
||||
cover = Media{
|
||||
Id: value.Get("id").String(),
|
||||
Description: value.Get("description").String(),
|
||||
Type: strings.Split(value.Get("type").String(), "/")[0],
|
||||
|
@ -90,7 +112,7 @@ func FetchSubmissions(username string, sort string, page string) ([]types.Submis
|
|||
|
||||
id := value.Get("id").String()
|
||||
|
||||
submissions = append(submissions, types.Submission{
|
||||
submissions = append(submissions, Submission{
|
||||
Id: id,
|
||||
Link: "/a/" + id,
|
||||
Title: value.Get("title").String(),
|
||||
|
|
|
@ -4,7 +4,6 @@ import (
|
|||
"strings"
|
||||
|
||||
"codeberg.org/video-prize-ranch/rimgo/api"
|
||||
"codeberg.org/video-prize-ranch/rimgo/types"
|
||||
"codeberg.org/video-prize-ranch/rimgo/utils"
|
||||
"github.com/gofiber/fiber/v2"
|
||||
)
|
||||
|
@ -14,7 +13,7 @@ func HandleEmbed(c *fiber.Ctx) error {
|
|||
c.Set("Cache-Control", "public,max-age=31557600")
|
||||
c.Set("Content-Security-Policy", "default-src 'none'; media-src 'self'; style-src 'self'; img-src 'self'; font-src 'self'; block-all-mixed-content")
|
||||
|
||||
post, err := types.Album{}, error(nil)
|
||||
post, err := api.Album{}, error(nil)
|
||||
switch {
|
||||
case strings.HasPrefix(c.Path(), "/a"):
|
||||
post, err = api.FetchAlbum(c.Params("postID"))
|
||||
|
|
|
@ -4,7 +4,6 @@ import (
|
|||
"strings"
|
||||
|
||||
"codeberg.org/video-prize-ranch/rimgo/api"
|
||||
"codeberg.org/video-prize-ranch/rimgo/types"
|
||||
"codeberg.org/video-prize-ranch/rimgo/utils"
|
||||
"github.com/gofiber/fiber/v2"
|
||||
)
|
||||
|
@ -14,7 +13,7 @@ func HandlePost(c *fiber.Ctx) error {
|
|||
c.Set("X-Frame-Options", "DENY")
|
||||
c.Set("Content-Security-Policy", "default-src 'none'; media-src 'self'; style-src 'self'; img-src 'self'; font-src 'self'; manifest-src 'self'; block-all-mixed-content")
|
||||
|
||||
post, err := types.Album{}, error(nil)
|
||||
post, err := api.Album{}, error(nil)
|
||||
switch {
|
||||
case strings.HasPrefix(c.Path(), "/a"):
|
||||
post, err = api.FetchAlbum(c.Params("postID"))
|
||||
|
@ -31,7 +30,7 @@ func HandlePost(c *fiber.Ctx) error {
|
|||
return err
|
||||
}
|
||||
|
||||
comments := []types.Comment{}
|
||||
comments := []api.Comment{}
|
||||
if post.SharedWithCommunity {
|
||||
c.Set("Cache-Control", "public,max-age=604800")
|
||||
comments, err = api.FetchComments(c.Params("postID"))
|
||||
|
|
|
@ -5,7 +5,6 @@ import (
|
|||
"sync"
|
||||
|
||||
"codeberg.org/video-prize-ranch/rimgo/api"
|
||||
"codeberg.org/video-prize-ranch/rimgo/types"
|
||||
"codeberg.org/video-prize-ranch/rimgo/utils"
|
||||
"github.com/gofiber/fiber/v2"
|
||||
)
|
||||
|
@ -28,7 +27,7 @@ func HandleUser(c *fiber.Ctx) error {
|
|||
|
||||
wg := sync.WaitGroup{}
|
||||
wg.Add(2)
|
||||
user, err := types.User{}, error(nil)
|
||||
user, err := api.User{}, error(nil)
|
||||
go func() {
|
||||
defer wg.Done()
|
||||
user, err = api.FetchUser(c.Params("userID"))
|
||||
|
@ -37,7 +36,7 @@ func HandleUser(c *fiber.Ctx) error {
|
|||
return err
|
||||
}
|
||||
|
||||
submissions, err := []types.Submission{}, error(nil)
|
||||
submissions, err := []api.Submission{}, error(nil)
|
||||
go func() {
|
||||
defer wg.Done()
|
||||
submissions, err = api.FetchSubmissions(c.Params("userID"), "newest", page)
|
||||
|
|
|
@ -1,16 +0,0 @@
|
|||
package types
|
||||
|
||||
type Album struct {
|
||||
Id string
|
||||
Title string
|
||||
Views int64
|
||||
Upvotes int64
|
||||
Downvotes int64
|
||||
SharedWithCommunity bool
|
||||
CreatedAt string
|
||||
UpdatedAt string
|
||||
Comments int64
|
||||
User User
|
||||
Media []Media
|
||||
Tags []Tag
|
||||
}
|
|
@ -1,15 +0,0 @@
|
|||
package types
|
||||
|
||||
type Comment struct {
|
||||
Comments []Comment
|
||||
User User
|
||||
Id string
|
||||
Comment string
|
||||
Upvotes int64
|
||||
Downvotes int64
|
||||
Platform string
|
||||
CreatedAt string
|
||||
RelTime string
|
||||
UpdatedAt string
|
||||
DeletedAt string
|
||||
}
|
|
@ -1,11 +0,0 @@
|
|||
package types
|
||||
|
||||
type Media struct {
|
||||
Id string
|
||||
Name string
|
||||
Title string
|
||||
Description string
|
||||
Url string
|
||||
Type string
|
||||
MimeType string
|
||||
}
|
|
@ -1,14 +0,0 @@
|
|||
package types
|
||||
|
||||
type Submission struct {
|
||||
Id string
|
||||
Title string
|
||||
Link string
|
||||
Cover Media
|
||||
Points int64
|
||||
Upvotes int64
|
||||
Downvotes int64
|
||||
Comments int64
|
||||
Views int64
|
||||
IsAlbum bool
|
||||
}
|
10
types/Tag.go
10
types/Tag.go
|
@ -1,10 +0,0 @@
|
|||
package types
|
||||
|
||||
type Tag struct {
|
||||
Tag string
|
||||
Display string
|
||||
Sort string
|
||||
PostCount int64
|
||||
Posts []Submission
|
||||
Background string
|
||||
}
|
|
@ -1,11 +0,0 @@
|
|||
package types
|
||||
|
||||
type User struct {
|
||||
Id int64
|
||||
Bio string
|
||||
Username string
|
||||
Points int64
|
||||
Cover string
|
||||
Avatar string
|
||||
CreatedAt string
|
||||
}
|
Loading…
Reference in New Issue