Major internal changes to the tag page

This commit is contained in:
VnPower
2024-06-05 20:55:31 +07:00
parent 1b09f77c84
commit 1c21b6fa5b
6 changed files with 97 additions and 42 deletions
+30 -5
View File
@@ -1,35 +1,60 @@
package core
import "fmt"
import (
"fmt"
"strings"
)
type URLConstructor struct {
path string
hash map[string]string
fragment string
}
func lowercaseFirstChar(s string) string {
return strings.ToLower(s[0:1]) + s[1:]
}
func (obj *URLConstructor) Replace(name, value string) string {
url := fmt.Sprintf("/%s", obj.path)
first := true
var matchedV string
for k, v := range obj.hash {
k = lowercaseFirstChar(k)
if k == name {
matchedV = value
continue
}
if first {
url += "?"
first = false
} else {
url += "&"
}
if k == name {
v = value
}
url += fmt.Sprintf("%s=%s", k, v)
}
// This is to move the matched query to the end of the URL
var t string
if first {
t = "?"
} else {
t = "&"
}
url += fmt.Sprintf("%s%s=%s", t, name, matchedV)
url += obj.fragment
return url
}
func NewURLConstruct(path string, obj map[string]string) URLConstructor {
func NewURLConstruct(path string, obj map[string]string, fragment string) URLConstructor {
return URLConstructor{
path: path,
hash: obj,
fragment: fragment,
}
}
+2 -2
View File
@@ -116,10 +116,10 @@ func GetTagDetailURL(unescapedTag string) string {
return fmt.Sprintf(base, url.PathEscape(unescapedTag))
}
func GetSearchArtworksURL(artworkType, name, order, age_settings, ratio, page string) string {
func GetSearchArtworksURL(s map[string]string) string {
base := "https://www.pixiv.net/ajax/search/%s/%s?order=%s&mode=%s&ratio=%s&p=%s"
return fmt.Sprintf(base, artworkType, name, order, age_settings, ratio, page)
return fmt.Sprintf(base, s["Category"], s["Name"], s["Order"], s["Mode"], s["Ratio"], s["Page"])
}
func GetLandingURL(mode string) string {
+33 -3
View File
@@ -34,6 +34,37 @@ type SearchResult struct {
RelatedTags []string `json:"relatedTags"`
}
type SearchPageSettings struct {
Name string // Tag to search for
Category string // Filter by type, could be illusts or mangas
Order string // Sort by date
Mode string // Safe, R18 or both
Ratio string // Landscape, portrait, or squared
Page string // Page number
// To implement
SearchMode string // Exact match, partial match, or match with title
Wlt string // Minimum image width
Wgt string // Maximum image width
Hlt string // Minimum image height
Hgt string // Maximum image height
Tool string // Filter by production tools (ex. Photoshop)
Scd string // After this date
Ecd string // Before this date
}
func (s SearchPageSettings) ReturnMap() map[string]string {
return map[string]string {
"Name": s.Name,
"Category": s.Category,
"Order": s.Order,
"Mode": s.Mode,
"Ratio": s.Ratio,
"Page": s.Page, // This field should always be the last
}
}
func GetTagData(c *fiber.Ctx, name string) (TagDetail, error) {
var tag TagDetail
@@ -54,9 +85,8 @@ func GetTagData(c *fiber.Ctx, name string) (TagDetail, error) {
return tag, nil
}
func GetSearch(c *fiber.Ctx, artworkType, name, order, age_settings, ratio, page string) (*SearchResult, error) {
URL := http.GetSearchArtworksURL(artworkType, name, order, age_settings, ratio, page)
func GetSearch(c *fiber.Ctx, settings SearchPageSettings) (*SearchResult, error) {
URL := http.GetSearchArtworksURL(settings.ReturnMap())
response, err := http.UnwrapWebAPIRequest(c.Context(), URL, "")
if err != nil {
+1 -1
View File
@@ -14,7 +14,7 @@ func DiscoveryPage(c *fiber.Ctx) error {
return err
}
urlc := site.NewURLConstruct("discovery", map[string]string{"mode": mode})
urlc := site.NewURLConstruct("discovery", map[string]string{"mode": mode}, "#checkpoint")
return c.Render("discovery", fiber.Map{
"Artworks": works,
+17 -8
View File
@@ -5,16 +5,11 @@ import (
"strconv"
core "codeberg.org/vnpower/pixivfe/v2/core/webapi"
site "codeberg.org/vnpower/pixivfe/v2/core/http"
"github.com/gofiber/fiber/v2"
)
func TagPage(c *fiber.Ctx) error {
queries := make(map[string]string, 3)
queries["Mode"] = c.Query("mode", "safe")
queries["Category"] = c.Query("category", "artworks")
queries["Order"] = c.Query("order", "date_d")
queries["Ratio"] = c.Query("ratio", "")
param := c.Params("name")
name, err := url.PathUnescape(param)
if err != nil {
@@ -27,14 +22,28 @@ func TagPage(c *fiber.Ctx) error {
return err
}
// Because of the large amount of queries available for this route,
// I made a struct type just to manage the queries
queries := core.SearchPageSettings {
Name: name,
Category: c.Query("category", "artworks"),
Order: c.Query("order", "date_d"),
Mode: c.Query("mode", "safe"),
Ratio: c.Query("ratio", ""),
Page: page,
}
tag, err := core.GetTagData(c, name)
if err != nil {
return err
}
result, err := core.GetSearch(c, queries["Category"], name, queries["Order"], queries["Mode"], queries["Ratio"], page)
result, err := core.GetSearch(c, queries)
if err != nil {
return err
}
return c.Render("tag", fiber.Map{"Title": "Results for " + name, "Tag": tag, "Data": result, "Queries": queries, "TrueTag": param, "Page": pageInt})
urlc := site.NewURLConstruct("tags", queries.ReturnMap(), "")
return c.Render("tag", fiber.Map{"Title": "Results for " + name, "Tag": tag, "Data": result, "Queries": queries.ReturnMap(), "TrueTag": param, "Page": pageInt, "URLC": urlc.Replace})
}
+14 -23
View File
@@ -28,36 +28,28 @@
</div>
<div class="switcher">
{{ url := "/tags/" + TrueTag + "?order=" + Queries.Order + "&mode=" +
Queries.Mode + "&ratio=" + Queries.Ratio + "&page=1" + "&category=" }}
<div class="switch-title">Type</div>
<a href="{{url }}artworks" class="switch-button">All</a>
<a href="{{url }}illustrations" class="switch-button">Illustrations</a>
<a href="{{url }}manga" class="switch-button">Manga</a>
<a href='{{URLC("category", "artworks")}}' class="switch-button">All</a>
<a href='{{URLC("category", "illustrations")}}' class="switch-button">Illustrations</a>
<a href='{{URLC("category", "manga")}}' class="switch-button">Manga</a>
</div>
<div class="switcher">
{{ url := "/tags/" + TrueTag + "?category=" + Queries.Category + "&mode=" +
Queries.Mode + "&ratio=" + Queries.Ratio + "&page=1" + "&order=" }}
<div class="switch-title">Order</div>
<a href="{{url }}date_d" class="switch-button">Newest</a>
<a href="{{url }}date" class="switch-button">Oldest</a>
<a href='{{URLC("order", "date_d")}}' class="switch-button">Newest</a>
<a href='{{URLC("order", "date")}}' class="switch-button">Oldest</a>
</div>
<div class="switcher">
<div class="switch-title">Filter</div>
{{ url := "/tags/" + TrueTag + "?category=" + Queries.Category + "&order="
+ Queries.Order + "&ratio=" + Queries.Ratio + "&page=1" + "&mode=" }}
<a href="{{url }}all" class="switch-button">All</a>
<a href="{{url }}safe" class="switch-button">Safe</a>
<a href="{{url }}r18" class="switch-button">R-18</a>
<a href='{{URLC("mode", "all")}}' class="switch-button">All</a>
<a href='{{URLC("mode", "safe")}}' class="switch-button">Safe</a>
<a href='{{URLC("mode", "r18")}}' class="switch-button">R-18</a>
</div>
<div class="switcher">
<div class="switch-title">Ratio</div>
{{ url := "/tags/" + TrueTag + "?category=" + Queries.Category + "&order="
+ Queries.Order + "&page=1" + "&mode=" + Queries.Mode + "&ratio=" }}
<a href="{{url }}" class="switch-button">All</a>
<a href="{{url }}-0.5" class="switch-button">Portrait</a>
<a href="{{url }}0" class="switch-button">Square</a>
<a href="{{url }}0.5" class="switch-button">Landscape</a>
<a href='{{URLC("ratio", "")}}' class="switch-button">All</a>
<a href='{{URLC("ratio", "-0.5")}}' class="switch-button">Portrait</a>
<a href='{{URLC("ratio", "0")}}' class="switch-button">Square</a>
<a href='{{URLC("ratio", "0.5")}}' class="switch-button">Landscape</a>
</div>
{{ if Data.Popular.Recent }}
<h2>Popular artworks</h2>
@@ -78,10 +70,9 @@
</div>
<div class="pagination">
{{ url := "/tags/" + TrueTag + "?category=" + Queries.Category + "&order="
+ Queries.Order + "&mode=" + Queries.Mode + "&ratio=" + Queries.Ratio + "&page=" }}
{{ url := URLC("page", "") }}
<small>Page {{ Page }}</small>
<br />
{{ raw: createPaginator(url, "#checkpoint", Page, -1) }}
</div>
</div>
</div>