Improve code reuse

This commit is contained in:
video-prize-ranch 2022-05-23 11:10:50 -04:00
parent 5569feb2c8
commit 6eee86d682
No known key found for this signature in database
GPG Key ID: D8EAA4C5B12A7281
7 changed files with 65 additions and 90 deletions

View File

@ -1,8 +1,6 @@
package api
import (
"io"
"net/http"
"strings"
"time"
@ -20,25 +18,16 @@ func FetchAlbum(albumID string) (types.Album, error) {
return cacheData.(types.Album), nil
}
res, err := http.Get("https://api.imgur.com/post/v1/albums/" + albumID + "?client_id=" + utils.Config["imgurId"].(string) + "&include=media%2Caccount")
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
}
body, err := io.ReadAll(res.Body)
album, err := ParseAlbum(data)
if err != nil {
return types.Album{}, err
}
data := gjson.Parse(string(body))
album := types.Album{}
if data.Get("shared_with_community").Bool() || res.StatusCode == 404 {
album, err = FetchPosts(albumID)
} else {
album, err = ParseAlbum(data)
}
albumCache.Set(albumID + "-album", album, cache.DefaultExpiration)
return album, err
}
@ -49,17 +38,11 @@ func FetchPosts(albumID string) (types.Album, error) {
return cacheData.(types.Album), nil
}
res, err := http.Get("https://api.imgur.com/post/v1/posts/" + albumID + "?client_id=" + utils.Config["imgurId"].(string) + "&include=media%2Caccount%2Ctags")
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
}
body, err := io.ReadAll(res.Body)
if err != nil {
return types.Album{}, err
}
data := gjson.Parse(string(body))
album, err := ParseAlbum(data)
if err != nil {
return types.Album{}, err
@ -75,17 +58,11 @@ func FetchMedia(mediaID string) (types.Album, error) {
return cacheData.(types.Album), nil
}
res, err := http.Get("https://api.imgur.com/post/v1/media/" + mediaID + "?client_id=" + utils.Config["imgurId"].(string) + "&include=media%2Caccount")
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
}
body, err := io.ReadAll(res.Body)
if err != nil {
return types.Album{}, err
}
data := gjson.Parse(string(body))
album, err := ParseAlbum(data)
if err != nil {
return types.Album{}, err

View File

@ -1,8 +1,6 @@
package api
import (
"io"
"net/http"
"strings"
"sync"
"time"
@ -22,18 +20,11 @@ func FetchComments(galleryID string) ([]types.Comment, error) {
return cacheData.([]types.Comment), nil
}
res, err := http.Get("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")
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{}, err
return []types.Comment{}, nil
}
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(

View File

@ -55,18 +55,11 @@ func FetchSubmissions(username string, sort string, page string) ([]types.Submis
return cacheData.([]types.Submission), nil
}
res, err := http.Get("https://api.imgur.com/3/account/" + username + "/submissions/" + page + "/" + sort + "?album_previews=1&client_id=" + utils.Config["imgurId"].(string))
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
}
body, err := io.ReadAll(res.Body)
if err != nil {
return []types.Submission{}, err
}
data := gjson.Parse(string(body))
submissions := []types.Submission{}
wg := sync.WaitGroup{}

View File

@ -44,13 +44,13 @@ func main() {
app.Get("/:postID.gifv", pages.HandleEmbed)
app.Get("/:baseName.:extension", pages.HandleMedia)
app.Get("/:postID", pages.HandlePost)
app.Get("/a/:galleryID", pages.HandleGallery)
app.Get("/a/:postID", pages.HandlePost)
app.Get("/t/:tag", pages.HandleTag)
app.Get("/user/:userID", pages.HandleUser)
app.Get("/r/:sub/:postID", pages.HandlePost)
app.Get("/user/:userID/cover", pages.HandleUserCover)
app.Get("/user/:userID/avatar", pages.HandleUserAvatar)
app.Get("/gallery/:galleryID", pages.HandleGallery)
app.Get("/gallery/:postID", pages.HandlePost)
app.Listen(":" + utils.Config["port"].(string))
}

View File

@ -1,34 +0,0 @@
package pages
import (
"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"
)
func HandleGallery(c *fiber.Ctx) error {
utils.SetHeaders(c)
c.Set("Content-Security-Policy", "default-src 'none'; media-src 'self'; style-src 'unsafe-inline' 'self'; img-src 'self'; font-src 'self'; manifest-src 'self'; block-all-mixed-content")
album, err := api.FetchAlbum(c.Params("galleryID"))
if err != nil {
return err
}
comments := []types.Comment{}
if album.SharedWithCommunity {
c.Set("Cache-Control", "public,max-age=604800")
comments, err = api.FetchComments(c.Params("galleryID"))
if err != nil {
return err
}
} else {
c.Set("Cache-Control", "public,max-age=31557600")
}
return c.Render("post", fiber.Map{
"post": album,
"comments": comments,
})
}

View File

@ -2,6 +2,7 @@ package pages
import (
"fmt"
"strings"
"codeberg.org/video-prize-ranch/rimgo/api"
"codeberg.org/video-prize-ranch/rimgo/types"
@ -13,15 +14,18 @@ func HandlePost(c *fiber.Ctx) error {
utils.SetHeaders(c)
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 := api.FetchPosts(c.Params("postID"))
if post.Id == "" {
post, err := types.Album{}, error(nil)
switch {
case strings.HasPrefix(c.Path(), "/a"):
post, err = api.FetchAlbum(c.Params("postID"))
println(post.Title)
case strings.HasPrefix(c.Path(), "/gallery"):
post, err = api.FetchPosts(c.Params("postID"))
if post.Id == "" {
post, err = api.FetchMedia(c.Params("postID"))
if post.Id == "" {
return fmt.Errorf("404 page not found")
}
}
default:
post, err = api.FetchMedia(c.Params("postID"))
}
if post.Id == "" || strings.Contains(err.Error(), "404") {
return fmt.Errorf("404 page not found")
}
if err != nil {
return err

44
utils/request.go Normal file
View File

@ -0,0 +1,44 @@
package utils
import (
"fmt"
"io"
"net/http"
"github.com/tidwall/gjson"
)
func GetJSON(url string) (gjson.Result, error) {
req, err := http.NewRequest("GET", url, nil)
if err != nil {
return gjson.Result{}, err
}
req.Header.Set("Accept", "*/*")
req.Header.Set("Accept-Language", "en-US,en;q=0.5")
req.Header.Set("Cache-Control", "no-cache")
req.Header.Set("Connection", "keep-alive")
req.Header.Set("Origin", "https://imgur.com")
req.Header.Set("Pragma", "no-cache")
req.Header.Set("Sec-Fetch-Dest", "empty")
req.Header.Set("Sec-Fetch-Mode", "cors")
req.Header.Set("Sec-Fetch-Site", "same-site")
req.Header.Set("User-Agent", "Mozilla/5.0 (Windows NT 10.0; rv:91.0) Gecko/20100101 Firefox/91.0")
client := http.Client{}
res, err := client.Do(req)
if err != nil {
return gjson.Result{}, err
}
body, err := io.ReadAll(res.Body)
if err != nil {
return gjson.Result{}, err
}
if res.StatusCode != 200 {
return gjson.Result{}, fmt.Errorf("received status %s, expected 200 OK.\n%s", res.Status, string(body))
}
return gjson.Parse(string(body)), nil
}