From 1c21b6fa5b83edcfd0b057d8b6843eb6df312f21 Mon Sep 17 00:00:00 2001 From: VnPower Date: Wed, 5 Jun 2024 20:55:31 +0700 Subject: [PATCH] Major internal changes to the tag page --- core/http/constructurl.go | 35 ++++++++++++++++++++++++++++++----- core/http/url.go | 4 ++-- core/webapi/tag.go | 36 +++++++++++++++++++++++++++++++++--- pages/discovery.go | 2 +- pages/tag.go | 25 +++++++++++++++++-------- views/pages/tag.jet.html | 37 ++++++++++++++----------------------- 6 files changed, 97 insertions(+), 42 deletions(-) diff --git a/core/http/constructurl.go b/core/http/constructurl.go index 0bc9a62..0614b93 100644 --- a/core/http/constructurl.go +++ b/core/http/constructurl.go @@ -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, } } diff --git a/core/http/url.go b/core/http/url.go index c056137..f8e5e91 100644 --- a/core/http/url.go +++ b/core/http/url.go @@ -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 { diff --git a/core/webapi/tag.go b/core/webapi/tag.go index 6ce52a8..6e91633 100644 --- a/core/webapi/tag.go +++ b/core/webapi/tag.go @@ -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 { diff --git a/pages/discovery.go b/pages/discovery.go index 88b0675..42cf519 100644 --- a/pages/discovery.go +++ b/pages/discovery.go @@ -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, diff --git a/pages/tag.go b/pages/tag.go index a2a158f..b7393cd 100644 --- a/pages/tag.go +++ b/pages/tag.go @@ -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}) + } diff --git a/views/pages/tag.jet.html b/views/pages/tag.jet.html index b399396..534a674 100644 --- a/views/pages/tag.jet.html +++ b/views/pages/tag.jet.html @@ -28,36 +28,28 @@
- {{ url := "/tags/" + TrueTag + "?order=" + Queries.Order + "&mode=" + - Queries.Mode + "&ratio=" + Queries.Ratio + "&page=1" + "&category=" }}
Type
- All - Illustrations - Manga + All + Illustrations + Manga
- {{ url := "/tags/" + TrueTag + "?category=" + Queries.Category + "&mode=" + - Queries.Mode + "&ratio=" + Queries.Ratio + "&page=1" + "&order=" }}
Order
- Newest - Oldest + Newest + Oldest
Filter
- {{ url := "/tags/" + TrueTag + "?category=" + Queries.Category + "&order=" - + Queries.Order + "&ratio=" + Queries.Ratio + "&page=1" + "&mode=" }} - All - Safe - R-18 + All + Safe + R-18
Ratio
- {{ url := "/tags/" + TrueTag + "?category=" + Queries.Category + "&order=" - + Queries.Order + "&page=1" + "&mode=" + Queries.Mode + "&ratio=" }} - All - Portrait - Square - Landscape + All + Portrait + Square + Landscape
{{ if Data.Popular.Recent }}

Popular artworks

@@ -78,10 +70,9 @@ - \ No newline at end of file +