mirror of
https://codeberg.org/video-prize-ranch/rimgo.git
synced 2025-12-13 19:55:21 +00:00
Initial tags support (#11)
This commit is contained in:
19
api/f.ts
19
api/f.ts
@@ -1,19 +0,0 @@
|
||||
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(
|
||||
``,
|
||||
);
|
||||
return JSON.parse(response.body).data;
|
||||
/* eslint-enable max-len */
|
||||
}
|
||||
|
||||
export const fetchTagPosts = async (tagID: string, sort: Sorting = 'viral'): Promise<TagResult> => {
|
||||
/* 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/gallery/t/${tagID.toLowerCase()}/${sort}/week/0?client_id=${CONFIG.imgur_client_id}`,
|
||||
);
|
||||
return JSON.parse(response.body).data;
|
||||
/* eslint-enable max-len */
|
||||
}
|
||||
93
api/tag.go
Normal file
93
api/tag.go
Normal file
@@ -0,0 +1,93 @@
|
||||
package api
|
||||
|
||||
import (
|
||||
"io/ioutil"
|
||||
"net/http"
|
||||
"strings"
|
||||
"sync"
|
||||
|
||||
"codeberg.org/video-prize-ranch/rimgo/types"
|
||||
"github.com/spf13/viper"
|
||||
"github.com/tidwall/gjson"
|
||||
)
|
||||
|
||||
func FetchTag(tag string, sort string, page string) (types.Tag, error) {
|
||||
req, err := http.NewRequest("GET", "https://api.imgur.com/post/v1/posts/t/"+tag, nil)
|
||||
if err != nil {
|
||||
return types.Tag{}, err
|
||||
}
|
||||
|
||||
q := req.URL.Query()
|
||||
q.Add("client_id", viper.GetString("RIMGU_IMGUR_CLIENT_ID"))
|
||||
q.Add("include", "cover")
|
||||
q.Add("page", page)
|
||||
|
||||
switch sort {
|
||||
case "newest":
|
||||
q.Add("filter[window]", "week")
|
||||
q.Add("sort", "-time")
|
||||
case "best":
|
||||
q.Add("filter[window]", "all")
|
||||
q.Add("sort", "-top")
|
||||
case "popular":
|
||||
default:
|
||||
q.Add("filter[window]", "week")
|
||||
q.Add("sort", "-viral")
|
||||
sort = "popular"
|
||||
}
|
||||
|
||||
req.URL.RawQuery = q.Encode()
|
||||
|
||||
res, err := http.DefaultClient.Do(req)
|
||||
if err != nil {
|
||||
return types.Tag{}, err
|
||||
}
|
||||
|
||||
body, err := ioutil.ReadAll(res.Body)
|
||||
if err != nil {
|
||||
return types.Tag{}, err
|
||||
}
|
||||
|
||||
data := gjson.Parse(string(body))
|
||||
|
||||
wg := sync.WaitGroup{}
|
||||
posts := make([]types.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{
|
||||
Id: value.Get("id").String(),
|
||||
Title: value.Get("title").String(),
|
||||
Link: strings.ReplaceAll(value.Get("url").String(), "https://imgur.com", ""),
|
||||
Cover: types.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", ""),
|
||||
},
|
||||
Points: value.Get("point_count").Int(),
|
||||
Upvotes: value.Get("upvote_count").Int(),
|
||||
Downvotes: value.Get("downvote_count").Int(),
|
||||
Comments: value.Get("comment_count").Int(),
|
||||
Views: value.Get("view_count").Int(),
|
||||
IsAlbum: value.Get("is_album").Bool(),
|
||||
})
|
||||
}()
|
||||
|
||||
return true
|
||||
},
|
||||
)
|
||||
|
||||
wg.Wait()
|
||||
|
||||
return types.Tag{
|
||||
Tag: tag,
|
||||
Display: data.Get("display").String(),
|
||||
Sort: sort,
|
||||
PostCount: data.Get("post_count").Int(),
|
||||
Posts: posts,
|
||||
Background: "/" + data.Get("background_id").String() + ".webp",
|
||||
}, nil
|
||||
}
|
||||
Reference in New Issue
Block a user