rimgo/api/user.go

142 lines
3.6 KiB
Go
Raw Normal View History

2022-01-28 01:41:10 +00:00
package api
import (
"io"
"net/http"
2023-06-10 16:04:29 +00:00
"regexp"
2022-01-28 01:41:10 +00:00
"strings"
"sync"
"time"
2022-04-22 15:55:53 +00:00
"codeberg.org/video-prize-ranch/rimgo/utils"
2022-01-28 01:41:10 +00:00
"github.com/tidwall/gjson"
)
2022-07-22 15:55:22 +00:00
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
}
2023-06-10 16:04:29 +00:00
var imgurRe = regexp.MustCompile(`https?://i?\.?imgur\.com`)
2023-01-01 20:12:03 +00:00
func (client *Client) FetchUser(username string) (User, error) {
cacheData, found := client.Cache.Get(username + "-user")
2022-03-16 02:56:34 +00:00
if found {
2022-07-22 15:55:22 +00:00
return cacheData.(User), nil
2022-03-16 02:56:34 +00:00
}
res, err := http.Get("https://api.imgur.com/account/v1/accounts/" + username + "?client_id=" + utils.Config.ImgurId)
2022-01-28 01:41:10 +00:00
if err != nil {
2022-07-22 15:55:22 +00:00
return User{}, err
2022-01-28 01:41:10 +00:00
}
body, err := io.ReadAll(res.Body)
if err != nil {
2022-07-22 15:55:22 +00:00
return User{}, err
2022-01-28 01:41:10 +00:00
}
2022-02-02 16:05:36 +00:00
data := gjson.Parse(string(body))
2022-01-28 01:41:10 +00:00
2022-02-02 16:05:36 +00:00
createdTime, _ := time.Parse(time.RFC3339, data.Get("created_at").String())
2022-07-22 15:55:22 +00:00
user := User{
2022-02-02 16:05:36 +00:00
Id: data.Get("id").Int(),
Bio: data.Get("bio").String(),
Username: data.Get("username").String(),
Points: data.Get("reputation_count").Int(),
Cover: strings.ReplaceAll(data.Get("cover_url").String(), "https://imgur.com", ""),
Avatar: strings.ReplaceAll(data.Get("avatar_url").String(), "https://i.imgur.com", ""),
CreatedAt: createdTime.Format("January 2, 2006"),
2022-03-16 02:56:34 +00:00
}
2023-01-01 20:12:03 +00:00
client.Cache.Set(username + "-user", user, 1*time.Hour)
2022-03-16 02:56:34 +00:00
return user, nil
2022-01-28 01:41:10 +00:00
}
2023-01-01 20:12:03 +00:00
func (client *Client) FetchSubmissions(username string, sort string, page string) ([]Submission, error) {
cacheData, found := client.Cache.Get(username + "-submissions")
2022-03-16 02:56:34 +00:00
if found {
2022-07-22 15:55:22 +00:00
return cacheData.([]Submission), nil
2022-03-16 02:56:34 +00:00
}
data, err := utils.GetJSON("https://api.imgur.com/3/account/" + username + "/submissions/" + page + "/" + sort + "?album_previews=1&client_id=" + utils.Config.ImgurId)
2022-01-28 01:41:10 +00:00
if err != nil {
2022-07-22 15:55:22 +00:00
return []Submission{}, err
2022-01-28 01:41:10 +00:00
}
2022-07-22 15:55:22 +00:00
submissions := []Submission{}
2022-01-28 01:41:10 +00:00
wg := sync.WaitGroup{}
data.Get("data").ForEach(
func(key, value gjson.Result) bool {
wg.Add(1)
go func() {
defer wg.Done()
2022-02-10 01:04:25 +00:00
coverData := value.Get("images.#(id==\"" + value.Get("cover").String() + "\")")
2022-07-22 15:55:22 +00:00
cover := Media{}
2022-02-10 01:04:25 +00:00
if coverData.Exists() {
2022-07-22 15:55:22 +00:00
cover = Media{
2022-02-10 01:04:25 +00:00
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 {
2022-07-22 15:55:22 +00:00
cover = Media{
2022-02-10 01:04:25 +00:00
Id: value.Get("id").String(),
Description: value.Get("description").String(),
Type: strings.Split(value.Get("type").String(), "/")[0],
Url: strings.ReplaceAll(value.Get("link").String(), "https://i.imgur.com", ""),
}
}
id := value.Get("id").String()
2023-06-10 16:04:29 +00:00
link := "/a/" + id
if value.Get("in_gallery").Bool() {
link = "/gallery/" + id
}
2022-01-28 01:41:10 +00:00
2022-07-22 15:55:22 +00:00
submissions = append(submissions, Submission{
2022-02-10 01:04:25 +00:00
Id: id,
2023-06-10 16:04:29 +00:00
Link: link,
2022-01-28 01:41:10 +00:00
Title: value.Get("title").String(),
2022-02-10 01:04:25 +00:00
Cover: cover,
Points: value.Get("points").Int(),
Upvotes: value.Get("ups").Int(),
Downvotes: value.Get("downs").Int(),
Comments: value.Get("comment_count").Int(),
Views: value.Get("views").Int(),
IsAlbum: value.Get("is_album").Bool(),
2022-01-28 01:41:10 +00:00
})
}()
return true
},
)
wg.Wait()
2022-03-16 02:56:34 +00:00
2023-01-01 20:12:03 +00:00
client.Cache.Set(username + "-submissions", submissions, 15*time.Minute)
2022-01-28 01:41:10 +00:00
return submissions, nil
}