mirror of
https://codeberg.org/video-prize-ranch/rimgo.git
synced 2026-02-15 04:55:59 +00:00
Compare commits
5 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
61a312aba0 | ||
|
|
33fa04e98d | ||
|
|
e5b87dc924 | ||
|
|
8cb2524924 | ||
|
|
23b66cba47 |
@@ -7,15 +7,15 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
type Client struct {
|
type Client struct {
|
||||||
ClientID string
|
ClientID string
|
||||||
Cache *cache.Cache
|
Cache *cache.Cache
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewClient(clientId string) (*Client) {
|
func NewClient(clientId string) *Client {
|
||||||
client := Client{
|
client := Client{
|
||||||
ClientID: clientId,
|
ClientID: clientId,
|
||||||
Cache: cache.New(15*time.Minute, 15*time.Minute),
|
Cache: cache.New(15*time.Minute, 15*time.Minute),
|
||||||
}
|
}
|
||||||
|
|
||||||
return &client
|
return &client
|
||||||
}
|
}
|
||||||
@@ -11,19 +11,19 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
type SearchResult struct {
|
type SearchResult struct {
|
||||||
Id string
|
Id string
|
||||||
Url string
|
Url string
|
||||||
ImageUrl string
|
ImageUrl string
|
||||||
Title string
|
Title string
|
||||||
User string
|
User string
|
||||||
Points string
|
Points string
|
||||||
Views string
|
Views string
|
||||||
RelTime string
|
RelTime string
|
||||||
}
|
}
|
||||||
|
|
||||||
func (client *Client) Search(query string, page string) ([]SearchResult, error) {
|
func (client *Client) Search(query string, page string) ([]SearchResult, error) {
|
||||||
query = url.QueryEscape(query)
|
query = url.QueryEscape(query)
|
||||||
req, err := http.NewRequest("GET", "https://imgur.com/search/all/page/" + page + "?scrolled&q_size_is_mpx=off&qs=list&q=" + query, nil)
|
req, err := http.NewRequest("GET", "https://imgur.com/search/all/page/"+page+"?scrolled&q_size_is_mpx=off&qs=list&q="+query, nil)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return []SearchResult{}, err
|
return []SearchResult{}, err
|
||||||
}
|
}
|
||||||
@@ -35,16 +35,16 @@ func (client *Client) Search(query string, page string) ([]SearchResult, error)
|
|||||||
}
|
}
|
||||||
defer res.Body.Close()
|
defer res.Body.Close()
|
||||||
if res.StatusCode != 200 {
|
if res.StatusCode != 200 {
|
||||||
return []SearchResult{}, fmt.Errorf("invalid status code, got %d", res.StatusCode)
|
return []SearchResult{}, fmt.Errorf("invalid status code, got %d", res.StatusCode)
|
||||||
}
|
}
|
||||||
|
|
||||||
doc, err := goquery.NewDocumentFromReader(res.Body)
|
doc, err := goquery.NewDocumentFromReader(res.Body)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return []SearchResult{}, err
|
return []SearchResult{}, err
|
||||||
}
|
}
|
||||||
|
|
||||||
results := []SearchResult{}
|
results := []SearchResult{}
|
||||||
doc.Find(".post-list").Each(func(i int, s *goquery.Selection) {
|
doc.Find(".post-list").Each(func(i int, s *goquery.Selection) {
|
||||||
url, _ := s.Find("a").Attr("href")
|
url, _ := s.Find("a").Attr("href")
|
||||||
imageUrl, _ := s.Find("img").Attr("src")
|
imageUrl, _ := s.Find("img").Attr("src")
|
||||||
|
|
||||||
@@ -55,14 +55,14 @@ func (client *Client) Search(query string, page string) ([]SearchResult, error)
|
|||||||
views = strings.TrimSuffix(views, " views")
|
views = strings.TrimSuffix(views, " views")
|
||||||
|
|
||||||
result := SearchResult{
|
result := SearchResult{
|
||||||
Id: strings.Split(url, "/")[2],
|
Id: strings.Split(url, "/")[2],
|
||||||
Url: url,
|
Url: url,
|
||||||
ImageUrl: strings.ReplaceAll(imageUrl, "//i.imgur.com", ""),
|
ImageUrl: strings.ReplaceAll(imageUrl, "//i.imgur.com", ""),
|
||||||
Title: s.Find(".search-item-title a").Text(),
|
Title: s.Find(".search-item-title a").Text(),
|
||||||
User: s.Find(".account").Text(),
|
User: s.Find(".account").Text(),
|
||||||
Views: views,
|
Views: views,
|
||||||
Points: points,
|
Points: points,
|
||||||
RelTime: strings.TrimSpace(postInfo[2]),
|
RelTime: strings.TrimSpace(postInfo[2]),
|
||||||
}
|
}
|
||||||
|
|
||||||
results = append(results, result)
|
results = append(results, result)
|
||||||
|
|||||||
@@ -36,6 +36,8 @@ func (client *Client) FetchTrending(section, sort, page string) ([]Submission, e
|
|||||||
case "best":
|
case "best":
|
||||||
q.Add("filter[window]", "all")
|
q.Add("filter[window]", "all")
|
||||||
q.Add("sort", "-top")
|
q.Add("sort", "-top")
|
||||||
|
case "random":
|
||||||
|
q.Add("sort", "random")
|
||||||
case "popular":
|
case "popular":
|
||||||
fallthrough
|
fallthrough
|
||||||
default:
|
default:
|
||||||
@@ -51,6 +53,8 @@ func (client *Client) FetchTrending(section, sort, page string) ([]Submission, e
|
|||||||
case "top":
|
case "top":
|
||||||
q.Add("filter[section]", "eq:top")
|
q.Add("filter[section]", "eq:top")
|
||||||
q.Add("filter[window]", "day")
|
q.Add("filter[window]", "day")
|
||||||
|
case "random":
|
||||||
|
q.Add("filter[section]", "eq:random")
|
||||||
default:
|
default:
|
||||||
q.Add("filter[section]", "eq:hot")
|
q.Add("filter[section]", "eq:hot")
|
||||||
section = "hot"
|
section = "hot"
|
||||||
|
|||||||
2
main.go
2
main.go
@@ -96,7 +96,7 @@ func main() {
|
|||||||
Expiration: 30 * time.Minute,
|
Expiration: 30 * time.Minute,
|
||||||
MaxBytes: 25000000,
|
MaxBytes: 25000000,
|
||||||
KeyGenerator: func(c *fiber.Ctx) string {
|
KeyGenerator: func(c *fiber.Ctx) string {
|
||||||
return utils.GetInstanceProtocol(c) + " " + c.OriginalURL()
|
return utils.GetInstanceUrl(c) + c.OriginalURL()
|
||||||
},
|
},
|
||||||
CacheControl: true,
|
CacheControl: true,
|
||||||
StoreResponseHeaders: true,
|
StoreResponseHeaders: true,
|
||||||
|
|||||||
@@ -1,13 +1,10 @@
|
|||||||
package pages
|
package pages
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"os"
|
|
||||||
|
|
||||||
"codeberg.org/rimgo/rimgo/utils"
|
"codeberg.org/rimgo/rimgo/utils"
|
||||||
"github.com/gofiber/fiber/v2"
|
"github.com/gofiber/fiber/v2"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
func HandleAbout(c *fiber.Ctx) error {
|
func HandleAbout(c *fiber.Ctx) error {
|
||||||
utils.SetHeaders(c)
|
utils.SetHeaders(c)
|
||||||
c.Set("X-Frame-Options", "DENY")
|
c.Set("X-Frame-Options", "DENY")
|
||||||
@@ -15,8 +12,8 @@ func HandleAbout(c *fiber.Ctx) error {
|
|||||||
c.Set("Content-Security-Policy", "default-src 'none'; frame-ancestors 'none'; base-uri 'none'; form-action 'self'; style-src 'self'; img-src 'self'; manifest-src 'self'; block-all-mixed-content")
|
c.Set("Content-Security-Policy", "default-src 'none'; frame-ancestors 'none'; base-uri 'none'; form-action 'self'; style-src 'self'; img-src 'self'; manifest-src 'self'; block-all-mixed-content")
|
||||||
|
|
||||||
return c.Render("about", fiber.Map{
|
return c.Render("about", fiber.Map{
|
||||||
"proto": c.Protocol(),
|
"proto": c.Protocol(),
|
||||||
"domain": c.Hostname(),
|
"domain": c.Hostname(),
|
||||||
"force_webp": os.Getenv("FORCE_WEBP"),
|
"force_webp": utils.Config.ForceWebp,
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
@@ -8,5 +8,5 @@ import (
|
|||||||
var ApiClient *api.Client
|
var ApiClient *api.Client
|
||||||
|
|
||||||
func InitializeApiClient() {
|
func InitializeApiClient() {
|
||||||
ApiClient = api.NewClient(utils.Config.ImgurId)
|
ApiClient = api.NewClient(utils.Config.ImgurId)
|
||||||
}
|
}
|
||||||
@@ -28,7 +28,7 @@ func HandleEmbed(c *fiber.Ctx) error {
|
|||||||
if err != nil && post.Id == "" && strings.Contains(err.Error(), "404") {
|
if err != nil && post.Id == "" && strings.Contains(err.Error(), "404") {
|
||||||
return utils.RenderError(c, 404)
|
return utils.RenderError(c, 404)
|
||||||
}
|
}
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -35,11 +35,11 @@ func handleMedia(c *fiber.Ctx, url string) error {
|
|||||||
utils.SetHeaders(c)
|
utils.SetHeaders(c)
|
||||||
|
|
||||||
if utils.Config.ForceWebp &&
|
if utils.Config.ForceWebp &&
|
||||||
!strings.HasSuffix(c.Path(), ".webp") &&
|
!strings.HasSuffix(c.Path(), ".webp") &&
|
||||||
c.Get("Sec-Fetch-Dest") == "image" &&
|
c.Get("Sec-Fetch-Dest") == "image" &&
|
||||||
c.Query("no_webp") == "" &&
|
c.Query("no_webp") == "" &&
|
||||||
c.Accepts("image/webp") == "image/webp" &&
|
c.Accepts("image/webp") == "image/webp" &&
|
||||||
!strings.HasPrefix(c.Path(), "/stack") {
|
!strings.HasPrefix(c.Path(), "/stack") {
|
||||||
url = strings.ReplaceAll(url, ".png", ".webp")
|
url = strings.ReplaceAll(url, ".png", ".webp")
|
||||||
url = strings.ReplaceAll(url, ".jpg", ".webp")
|
url = strings.ReplaceAll(url, ".jpg", ".webp")
|
||||||
url = strings.ReplaceAll(url, ".jpeg", ".webp")
|
url = strings.ReplaceAll(url, ".jpeg", ".webp")
|
||||||
|
|||||||
@@ -35,10 +35,10 @@ func HandleSearch(c *fiber.Ctx) error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
return c.Render("search", fiber.Map{
|
return c.Render("search", fiber.Map{
|
||||||
"query": query,
|
"query": query,
|
||||||
"results": results,
|
"results": results,
|
||||||
"page": pageNumber,
|
"page": pageNumber,
|
||||||
"nextPage": pageNumber + 1,
|
"nextPage": pageNumber + 1,
|
||||||
"prevPage": pageNumber - 1,
|
"prevPage": pageNumber - 1,
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -25,7 +25,7 @@ func HandleTrending(c *fiber.Ctx) error {
|
|||||||
|
|
||||||
section := c.Query("section")
|
section := c.Query("section")
|
||||||
switch section {
|
switch section {
|
||||||
case "hot", "new", "top":
|
case "hot", "new", "top", "random":
|
||||||
default:
|
default:
|
||||||
section = "hot"
|
section = "hot"
|
||||||
}
|
}
|
||||||
@@ -42,11 +42,11 @@ func HandleTrending(c *fiber.Ctx) error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
return c.Render("trending", fiber.Map{
|
return c.Render("trending", fiber.Map{
|
||||||
"results": results,
|
"results": results,
|
||||||
"section": section,
|
"section": section,
|
||||||
"sort": sort,
|
"sort": sort,
|
||||||
"page": pageNumber,
|
"page": pageNumber,
|
||||||
"nextPage": pageNumber + 1,
|
"nextPage": pageNumber + 1,
|
||||||
"prevPage": pageNumber - 1,
|
"prevPage": pageNumber - 1,
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -21,51 +21,38 @@ type config struct {
|
|||||||
|
|
||||||
var Config config
|
var Config config
|
||||||
|
|
||||||
|
func envString(name, def string) string {
|
||||||
|
env := os.Getenv(name)
|
||||||
|
if env != "" {
|
||||||
|
return env
|
||||||
|
}
|
||||||
|
return def
|
||||||
|
}
|
||||||
|
func envBool(name string) bool {
|
||||||
|
return os.Getenv(name) == "true" || os.Getenv(name) == "1"
|
||||||
|
}
|
||||||
|
|
||||||
func LoadConfig() {
|
func LoadConfig() {
|
||||||
port := "3000"
|
|
||||||
if os.Getenv("PORT") != "" {
|
|
||||||
port = os.Getenv("PORT")
|
|
||||||
}
|
|
||||||
if os.Getenv("RIMGU_PORT") != "" {
|
|
||||||
port = os.Getenv("RIMGU_PORT")
|
|
||||||
}
|
|
||||||
|
|
||||||
addr := "0.0.0.0"
|
|
||||||
if os.Getenv("ADDRESS") != "" {
|
|
||||||
addr = os.Getenv("ADDRESS")
|
|
||||||
}
|
|
||||||
if os.Getenv("RIMGU_ADDRESS") != "" {
|
|
||||||
addr = os.Getenv("RIMGU_ADDRESS")
|
|
||||||
}
|
|
||||||
|
|
||||||
imgurId := "546c25a59c58ad7"
|
|
||||||
if os.Getenv("IMGUR_CLIENT_ID") != "" {
|
|
||||||
imgurId = os.Getenv("IMGUR_CLIENT_ID")
|
|
||||||
}
|
|
||||||
if os.Getenv("RIMGU_IMGUR_CLIENT_ID") != "" {
|
|
||||||
imgurId = os.Getenv("RIMGU_IMGUR_CLIENT_ID")
|
|
||||||
}
|
|
||||||
|
|
||||||
Config = config{
|
Config = config{
|
||||||
Port: port,
|
Port: envString("PORT", "3000"),
|
||||||
Addr: addr,
|
Addr: envString("ADDR", "0.0.0.0"),
|
||||||
ImgurId: imgurId,
|
ImgurId: envString("IMGUR_CLIENT_ID", "546c25a59c58ad7"),
|
||||||
ProtocolDetection: os.Getenv("PROTOCOL_DETECTION") == "true" || os.Getenv("PROTOCOL_DETECTION") == "1",
|
ProtocolDetection: envBool("PROTOCOL_DETECTION"),
|
||||||
Secure: os.Getenv("SECURE") == "true" || os.Getenv("SECURE") == "1",
|
Secure: envBool("SECURE"),
|
||||||
FiberPrefork: os.Getenv("FIBER_PREFORK") == "true" || os.Getenv("FIBER_PREFORK") == "1",
|
FiberPrefork: envBool("FIBER_PREFORK"),
|
||||||
ForceWebp: os.Getenv("FORCE_WEBP") == "true" || os.Getenv("FORCE_WEBP") == "1",
|
ForceWebp: envBool("FORCE_WEBP"),
|
||||||
Privacy: map[string]interface{}{
|
Privacy: map[string]interface{}{
|
||||||
"set": os.Getenv("PRIVACY_NOT_COLLECTED") != "",
|
"set": os.Getenv("PRIVACY_NOT_COLLECTED") != "",
|
||||||
"policy": os.Getenv("PRIVACY_POLICY"),
|
"policy": os.Getenv("PRIVACY_POLICY"),
|
||||||
"message": os.Getenv("PRIVACY_MESSAGE"),
|
"message": os.Getenv("PRIVACY_MESSAGE"),
|
||||||
"country": os.Getenv("PRIVACY_COUNTRY"),
|
"country": os.Getenv("PRIVACY_COUNTRY"),
|
||||||
"provider": os.Getenv("PRIVACY_PROVIDER"),
|
"provider": os.Getenv("PRIVACY_PROVIDER"),
|
||||||
"cloudflare": os.Getenv("PRIVACY_CLOUDFLARE") == "true" || os.Getenv("PRIVACY_CLOUDFLARE") == "1",
|
"cloudflare": envBool("PRIVACY_CLOUDFLARE"),
|
||||||
"not_collected": os.Getenv("PRIVACY_NOT_COLLECTED") == "true" || os.Getenv("PRIVACY_NOT_COLLECTED") == "1",
|
"not_collected": envBool("PRIVACY_NOT_COLLECTED"),
|
||||||
"ip": os.Getenv("PRIVACY_IP") == "true" || os.Getenv("PRIVACY_IP") == "1",
|
"ip": envBool("PRIVACY_IP"),
|
||||||
"url": os.Getenv("PRIVACY_URL") == "true" || os.Getenv("PRIVACY_URL") == "1",
|
"url": envBool("PRIVACY_URL"),
|
||||||
"device": os.Getenv("PRIVACY_DEVICE") == "true" || os.Getenv("PRIVACY_DEVICE") == "1",
|
"device": envBool("PRIVACY_DEVICE"),
|
||||||
"diagnostics": os.Getenv("PRIVACY_DIAGNOSTICS") == "true" || os.Getenv("PRIVACY_DIAGNOSTICS") == "1",
|
"diagnostics": envBool("PRIVACY_DIAGNOSTICS"),
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -18,7 +18,7 @@ func RenderError(c *fiber.Ctx, code int) error {
|
|||||||
c.Set("Content-Type", "image/png")
|
c.Set("Content-Type", "image/png")
|
||||||
return c.Status(code).Send(img)
|
return c.Status(code).Send(img)
|
||||||
} else {
|
} else {
|
||||||
return c.Status(code).Render("errors/" + strconv.Itoa(code), fiber.Map{
|
return c.Status(code).Render("errors/"+strconv.Itoa(code), fiber.Map{
|
||||||
"path": c.Path(),
|
"path": c.Path(),
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -35,7 +35,7 @@ func GetJSON(url string) (gjson.Result, error) {
|
|||||||
return gjson.Result{}, err
|
return gjson.Result{}, err
|
||||||
}
|
}
|
||||||
|
|
||||||
switch (res.StatusCode) {
|
switch res.StatusCode {
|
||||||
case 200:
|
case 200:
|
||||||
return gjson.Parse(string(body)), nil
|
return gjson.Parse(string(body)), nil
|
||||||
case 429:
|
case 429:
|
||||||
|
|||||||
@@ -30,20 +30,35 @@
|
|||||||
<a href="?section=hot&sort={{sort}}"><b>Hot</b></a>
|
<a href="?section=hot&sort={{sort}}"><b>Hot</b></a>
|
||||||
<a href="?section=new&sort={{sort}}">New</a>
|
<a href="?section=new&sort={{sort}}">New</a>
|
||||||
<a href="?section=top&sort={{sort}}">Top</a>
|
<a href="?section=top&sort={{sort}}">Top</a>
|
||||||
|
<a href="?section=random&sort=random">Random</a>
|
||||||
{{/equal}}
|
{{/equal}}
|
||||||
{{#equal section "new"}}
|
{{#equal section "new"}}
|
||||||
<a href="?section=hot&sort={{sort}}">Hot</a>
|
<a href="?section=hot&sort={{sort}}">Hot</a>
|
||||||
<a href="?section=new&sort={{sort}}"><b>New</b></a>
|
<a href="?section=new&sort={{sort}}"><b>New</b></a>
|
||||||
<a href="?section=top&sort={{sort}}">Top</a>
|
<a href="?section=top&sort={{sort}}">Top</a>
|
||||||
|
<a href="?section=random&sort=random">Random</a>
|
||||||
{{/equal}}
|
{{/equal}}
|
||||||
{{#equal section "top"}}
|
{{#equal section "top"}}
|
||||||
<a href="?section=hot&sort={{sort}}">Hot</a>
|
<a href="?section=hot&sort={{sort}}">Hot</a>
|
||||||
<a href="?section=new&sort={{sort}}">New</a>
|
<a href="?section=new&sort={{sort}}">New</a>
|
||||||
<a href="?section=top&sort={{sort}}"><b>Top</b></a>
|
<a href="?section=top&sort={{sort}}"><b>Top</b></a>
|
||||||
|
<a href="?section=random&sort=random">Random</a>
|
||||||
|
{{/equal}}
|
||||||
|
{{#equal section "random"}}
|
||||||
|
<a href="?section=hot&sort={{sort}}">Hot</a>
|
||||||
|
<a href="?section=new&sort={{sort}}">New</a>
|
||||||
|
<a href="?section=top&sort={{sort}}">Top</a>
|
||||||
|
<a href="?section=random&sort=random"><b>Random</b></a>
|
||||||
{{/equal}}
|
{{/equal}}
|
||||||
</div>
|
</div>
|
||||||
<hr class="sm:hidden my-2" />
|
<hr class="sm:hidden my-2" />
|
||||||
<div class="flex flex-col sm:items-end">
|
<div class="flex flex-col sm:items-end">
|
||||||
|
{{#equal section "random"}}
|
||||||
|
<a href="?section=hot&sort=popular">Popular</a>
|
||||||
|
<a href="?section=hot&sort=newest">Newest</a>
|
||||||
|
<a href="?section=hot&sort=best">Best</a>
|
||||||
|
{{/equal}}
|
||||||
|
{{#noteq section "random"}}
|
||||||
{{#equal sort "popular"}}
|
{{#equal sort "popular"}}
|
||||||
<a href="?section={{section}}&sort=popular"><b>Popular</b></a>
|
<a href="?section={{section}}&sort=popular"><b>Popular</b></a>
|
||||||
<a href="?section={{section}}&sort=newest">Newest</a>
|
<a href="?section={{section}}&sort=newest">Newest</a>
|
||||||
@@ -59,6 +74,7 @@
|
|||||||
<a href="?section={{section}}&sort=newest">Newest</a>
|
<a href="?section={{section}}&sort=newest">Newest</a>
|
||||||
<a href="?section={{section}}&sort=best"><b>Best</b></a>
|
<a href="?section={{section}}&sort=best"><b>Best</b></a>
|
||||||
{{/equal}}
|
{{/equal}}
|
||||||
|
{{/noteq}}
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
</header>
|
</header>
|
||||||
|
|||||||
Reference in New Issue
Block a user