mirror of
https://codeberg.org/video-prize-ranch/rimgo.git
synced 2025-12-13 19:55:21 +00:00
Comments
This commit is contained in:
13
api/album.go
13
api/album.go
@@ -4,9 +4,9 @@ import (
|
||||
"io"
|
||||
"net/http"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"codeberg.org/video-prize-ranch/rimgo/types"
|
||||
"codeberg.org/video-prize-ranch/rimgo/utils"
|
||||
"github.com/spf13/viper"
|
||||
"github.com/tidwall/gjson"
|
||||
)
|
||||
@@ -35,8 +35,8 @@ func FetchAlbum(albumID string) (types.Album, error) {
|
||||
media = append(media, types.Media{
|
||||
Id: value.Get("id").String(),
|
||||
Name: value.Get("name").String(),
|
||||
MimeType: value.Get("mime_type").String(),
|
||||
Type: value.Get("type").String(),
|
||||
MimeType: value.Get("mime_type").String(),
|
||||
Type: value.Get("type").String(),
|
||||
Title: value.Get("metadata.title").String(),
|
||||
Description: value.Get("metadata.description").String(),
|
||||
Url: url,
|
||||
@@ -46,7 +46,7 @@ func FetchAlbum(albumID string) (types.Album, error) {
|
||||
},
|
||||
)
|
||||
|
||||
createdAt, err := time.Parse("2006-01-02T15:04:05Z", data.Get("created_at").String())
|
||||
createdAt, err := utils.FormatDate(data.Get("created_at").String())
|
||||
if err != nil {
|
||||
return types.Album{}, err
|
||||
}
|
||||
@@ -55,7 +55,10 @@ func FetchAlbum(albumID string) (types.Album, error) {
|
||||
Id: data.Get("id").String(),
|
||||
Title: data.Get("title").String(),
|
||||
Views: data.Get("view_count").Int(),
|
||||
CreatedAt: createdAt.Format("January 2, 2006 3:04 PM"),
|
||||
Upvotes: data.Get("upvote_count").Int(),
|
||||
Downvotes: data.Get("downvote_count").Int(),
|
||||
Comments: data.Get("comment_count").Int(),
|
||||
CreatedAt: createdAt,
|
||||
Media: media,
|
||||
}, nil
|
||||
}
|
||||
|
||||
92
api/comments.go
Normal file
92
api/comments.go
Normal file
@@ -0,0 +1,92 @@
|
||||
package api
|
||||
|
||||
import (
|
||||
"io"
|
||||
"net/http"
|
||||
"strings"
|
||||
"sync"
|
||||
"time"
|
||||
|
||||
"codeberg.org/video-prize-ranch/rimgo/types"
|
||||
"codeberg.org/video-prize-ranch/rimgo/utils"
|
||||
"github.com/dustin/go-humanize"
|
||||
"github.com/spf13/viper"
|
||||
"github.com/tidwall/gjson"
|
||||
)
|
||||
|
||||
func FetchComments(galleryID string) ([]types.Comment, error) {
|
||||
// https://api.imgur.com/comment/v1/comments?client_id=546c25a59c58ad7&filter[post]=eq:g1bk7CB&include=account&per_page=30&sort=best
|
||||
|
||||
res, err := http.Get("https://api.imgur.com/comment/v1/comments?client_id=" + viper.GetString("RIMGU_IMGUR_CLIENT_ID") + "&filter[post]=eq:" + galleryID + "&include=account,adconfig&per_page=30&sort=best")
|
||||
if err != nil {
|
||||
return []types.Comment{}, err
|
||||
}
|
||||
|
||||
body, err := io.ReadAll(res.Body)
|
||||
if err != nil {
|
||||
return []types.Comment{}, err
|
||||
}
|
||||
|
||||
data := gjson.Parse(string(body))
|
||||
|
||||
wg := sync.WaitGroup{}
|
||||
comments := make([]types.Comment, 0)
|
||||
data.Get("data").ForEach(
|
||||
func(key, value gjson.Result) bool {
|
||||
wg.Add(1)
|
||||
|
||||
go func() {
|
||||
defer wg.Done()
|
||||
comments = append(comments, ParseComment(value))
|
||||
}()
|
||||
|
||||
return true
|
||||
},
|
||||
)
|
||||
wg.Wait()
|
||||
|
||||
return comments, nil
|
||||
}
|
||||
|
||||
func ParseComment(data gjson.Result) types.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())
|
||||
deletedAt, _ := utils.FormatDate(data.Get("deleted_at").String())
|
||||
|
||||
userAvatar := strings.ReplaceAll(data.Get("account.avatar").String(), "https://i.imgur.com", "")
|
||||
|
||||
wg := sync.WaitGroup{}
|
||||
comments := make([]types.Comment, 0)
|
||||
data.Get("comments").ForEach(
|
||||
func(key, value gjson.Result) bool {
|
||||
wg.Add(1)
|
||||
|
||||
go func() {
|
||||
defer wg.Done()
|
||||
comments = append(comments, ParseComment(value))
|
||||
}()
|
||||
|
||||
return true
|
||||
},
|
||||
)
|
||||
wg.Wait()
|
||||
|
||||
return types.Comment{
|
||||
Comments: comments,
|
||||
User: types.User{
|
||||
Id: data.Get("account.id").String(),
|
||||
Username: data.Get("account.username").String(),
|
||||
Avatar: userAvatar,
|
||||
},
|
||||
Id: data.Get("id").String(),
|
||||
Comment: data.Get("comment").String(),
|
||||
Upvotes: data.Get("upvote_count").Int(),
|
||||
Downvotes: data.Get("downvote_count").Int(),
|
||||
Platform: data.Get("platform").String(),
|
||||
CreatedAt: createdAt,
|
||||
RelTime: humanize.Time(createdTime),
|
||||
UpdatedAt: updatedAt,
|
||||
DeletedAt: deletedAt,
|
||||
}
|
||||
}
|
||||
10
api/f.ts
10
api/f.ts
@@ -1,13 +1,3 @@
|
||||
export const fetchComments = async (galleryID: string): Promise<Comment[]> => {
|
||||
/* eslint-disable max-len */
|
||||
// https://api.imgur.com/comment/v1/comments?client_id=${CLIENT_ID}%5Bpost%5D=eq%3Ag1bk7CB&include=account%2Cadconfig&per_page=30&sort=best
|
||||
const response = await get(
|
||||
`https://api.imgur.com/comment/v1/comments?client_id=${CONFIG.imgur_client_id}&filter%5Bpost%5D=eq%3A${galleryID}&include=account%2Cadconfig&per_page=30&sort=best`,
|
||||
);
|
||||
return JSON.parse(response.body).data;
|
||||
/* eslint-enable max-len */
|
||||
}
|
||||
|
||||
export const fetchUserInfo = async (userID: string): Promise<UserResult> => {
|
||||
// https://api.imgur.com/account/v1/accounts/hughjaniss?client_id=${CLIENT_ID}
|
||||
const response = await get(
|
||||
|
||||
Reference in New Issue
Block a user