Initial users support

This commit is contained in:
video-prize-ranch
2022-01-27 20:41:10 -05:00
parent fdd66853f9
commit 8d21d1a576
15 changed files with 344 additions and 66 deletions

View File

@@ -75,7 +75,7 @@ func ParseComment(data gjson.Result) types.Comment {
return types.Comment{
Comments: comments,
User: types.User{
Id: data.Get("account.id").String(),
Id: data.Get("account.id").Int(),
Username: data.Get("account.username").String(),
Avatar: userAvatar,
},

View File

@@ -1,16 +1,8 @@
export const fetchUserInfo = async (userID: string): Promise<UserResult> => {
// https://api.imgur.com/account/v1/accounts/hughjaniss?client_id=${CLIENT_ID}
const response = await get(
`https://api.imgur.com/account/v1/accounts/${userID.toLowerCase()}?client_id=${CONFIG.imgur_client_id}&include=`,
);
return JSON.parse(response.body);
}
export const fetchUserPosts = async (userID: string, sort: Sorting = 'newest'): Promise<Post[]> => {
/* eslint-disable max-len */
// https://api.imgur.com/3/account/mombotnumber5/submissions/0/newest?album_previews=1&client_id=${CLIENT_ID}
const response = await get(
`https://api.imgur.com/3/account/${userID.toLowerCase()}/submissions/0/${sort}?album_previews=1&client_id=${CONFIG.imgur_client_id}`,
``,
);
return JSON.parse(response.body).data;
/* eslint-enable max-len */

91
api/user.go Normal file
View File

@@ -0,0 +1,91 @@
package api
import (
"encoding/json"
"io"
"net/http"
"strings"
"sync"
"time"
"codeberg.org/video-prize-ranch/rimgo/types"
"github.com/spf13/viper"
"github.com/tidwall/gjson"
)
func FetchUser(username string) (types.User, error) {
res, err := http.Get("https://api.imgur.com/account/v1/accounts/" + username + "?client_id=" + viper.GetString("RIMGU_IMGUR_CLIENT_ID"))
if err != nil {
return types.User{}, err
}
body, err := io.ReadAll(res.Body)
if err != nil {
return types.User{}, err
}
var user types.User
err = json.Unmarshal(body, &user)
if err != nil {
return types.User{}, err
}
user.Cover = strings.ReplaceAll(user.Cover, "https://imgur.com", "")
user.Avatar = strings.ReplaceAll(user.Avatar, "https://i.imgur.com", "")
createdTime, _ := time.Parse("2006-01-02T15:04:05Z", user.CreatedAt)
user.CreatedAt = createdTime.Format("January 2, 2006")
return user, nil
}
func FetchSubmissions(username string, sort string, page string) ([]types.Submission, error) {
res, err := http.Get("https://api.imgur.com/3/account/" + username + "/submissions/" + page + "/" + sort + "?album_previews=1&client_id=" + viper.GetString("RIMGU_IMGUR_CLIENT_ID"))
if err != nil {
return []types.Submission{}, err
}
body, err := io.ReadAll(res.Body)
if err != nil {
return []types.Submission{}, err
}
data := gjson.Parse(string(body))
submissions := []types.Submission{}
wg := sync.WaitGroup{}
data.Get("data").ForEach(
func(key, value gjson.Result) bool {
wg.Add(1)
go func() {
defer wg.Done()
cover := value.Get("images.#(id==\"" + value.Get("cover").String() + "\")")
submissions = append(submissions, types.Submission{
Id: value.Get("id").String(),
Link: strings.ReplaceAll(value.Get("link").String(), "https://imgur.com", ""),
Title: value.Get("title").String(),
Cover: types.Media{
Id: cover.Get("id").String(),
Description: cover.Get("description").String(),
Type: strings.Split(cover.Get("type").String(), "/")[0],
Url: strings.ReplaceAll(cover.Get("link").String(), "https://i.imgur.com", ""),
},
Points: cover.Get("points").Int(),
Upvotes: cover.Get("ups").Int(),
Downvotes: cover.Get("downs").Int(),
Comments: cover.Get("comment_count").Int(),
Views: cover.Get("views").Int(),
IsAlbum: cover.Get("is_album").Bool(),
})
}()
return true
},
)
wg.Wait()
return submissions, nil
}