diff --git a/core/http/url.go b/core/http/url.go index bc4e1b5..6e965b6 100644 --- a/core/http/url.go +++ b/core/http/url.go @@ -71,18 +71,33 @@ func GetArtworkInformationURL(id string) string { return fmt.Sprintf(base, id) } + func GetArtworkImagesURL(id string) string { base := "https://www.pixiv.net/ajax/illust/%s/pages" return fmt.Sprintf(base, id) } + func GetArtworkRelatedURL(id string, limit int) string { base := "https://www.pixiv.net/ajax/illust/%s/recommend/init?limit=%d" return fmt.Sprintf(base, id, limit) } + func GetArtworkCommentsURL(id string) string { base := "https://www.pixiv.net/ajax/illusts/comments/roots?illust_id=%s&limit=100" return fmt.Sprintf(base, id) } + +func GetTagDetailURL(id string) string { + base := "https://www.pixiv.net/ajax/search/tags/%s" + + return fmt.Sprintf(base, id) +} + +func GetSearchArtworksURL(artworkType, name, order, age_settings, page string) string { + base := "https://www.pixiv.net/ajax/search/%s/%s?order=%s&mode=%s&p=%s" + + return fmt.Sprintf(base, artworkType, name, order, age_settings, page) +} diff --git a/core/webapi/tag.go b/core/webapi/tag.go new file mode 100644 index 0000000..7b50e95 --- /dev/null +++ b/core/webapi/tag.go @@ -0,0 +1,95 @@ +package core + +import ( + "strings" + + session "codeberg.org/vnpower/pixivfe/v2/core/config" + http "codeberg.org/vnpower/pixivfe/v2/core/http" + "github.com/goccy/go-json" + "github.com/gofiber/fiber/v2" +) + +type TagDetail struct { + Name string `json:"tag"` + AlternativeName string `json:"word"` + Metadata struct { + Detail string `json:"abstract"` + Image string `json:"image"` + Name string `json:"tag"` + ID json.Number `json:"id"` + } `json:"pixpedia"` +} + +type SearchArtworks struct { + Artworks []ArtworkBrief `json:"data"` + Total int `json:"total"` +} + +type SearchResult struct { + Artworks SearchArtworks + Popular struct { + Permanent []ArtworkBrief `json:"permanent"` + Recent []ArtworkBrief `json:"recent"` + } `json:"popular"` + RelatedTags []string `json:"relatedTags"` +} + +func GetTagData(c *fiber.Ctx, name string) (TagDetail, error) { + var tag TagDetail + + imageProxy := session.GetImageProxy(c) + URL := http.GetTagDetailURL(name) + + response, err := http.UnwrapWebAPIRequest(URL, "") + if err != nil { + return tag, err + } + + response = ProxyImages(response, imageProxy) + + err = json.Unmarshal([]byte(response), &tag) + if err != nil { + return tag, err + } + + return tag, nil +} + +func GetSearch(c *fiber.Ctx, artworkType, name, order, age_settings, page string) (*SearchResult, error) { + imageProxy := session.GetImageProxy(c) + URL := http.GetSearchArtworksURL(artworkType, name, order, age_settings, page) + + response, err := http.UnwrapWebAPIRequest(URL, "") + if err != nil { + return nil, err + } + response = ProxyImages(response, imageProxy) + + // IDK how to do better than this lol + temp := strings.ReplaceAll(string(response), `"illust"`, `"works"`) + temp = strings.ReplaceAll(temp, `"manga"`, `"works"`) + temp = strings.ReplaceAll(temp, `"illustManga"`, `"works"`) + + var resultRaw struct { + *SearchResult + ArtworksRaw json.RawMessage `json:"works"` + } + var artworks SearchArtworks + var result *SearchResult + + err = json.Unmarshal([]byte(temp), &resultRaw) + if err != nil { + return nil, err + } + + result = resultRaw.SearchResult + + err = json.Unmarshal([]byte(resultRaw.ArtworksRaw), &artworks) + if err != nil { + return nil, err + } + + result.Artworks = artworks + + return result, nil +} diff --git a/main.go b/main.go index 43f9df6..34f6c66 100644 --- a/main.go +++ b/main.go @@ -3,6 +3,7 @@ package main import ( "log" "net" + "net/http" "os" "time" @@ -130,6 +131,14 @@ func main() { self.Get("/followingWorks", pages.FollowingWorksPage) self.Get("/bookmarks", pages.LoginBookmarkPage) + server.Get("tags/:name", pages.TagPage) + server.Post("tags", + func(c *fiber.Ctx) error { + name := c.FormValue("name") + + return c.Redirect("/tags/"+name, http.StatusFound) + }) + // Legacy illust URL server.Get("member_illust.php", func(c *fiber.Ctx) error { return c.Redirect("artworks/" + c.Query("illust_id")) diff --git a/pages/tag.go b/pages/tag.go new file mode 100644 index 0000000..c99a6ff --- /dev/null +++ b/pages/tag.go @@ -0,0 +1,31 @@ +package pages + +import ( + "strconv" + + core "codeberg.org/vnpower/pixivfe/v2/core/webapi" + "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") + + name := c.Params("name") + + page := c.Query("page", "1") + pageInt, _ := strconv.Atoi(page) + + tag, err := core.GetTagData(c, name) + if err != nil { + return err + } + result, err := core.GetSearch(c, queries["Category"], name, queries["Order"], queries["Mode"], page) + if err != nil { + return err + } + + return c.Render("pages/tag", fiber.Map{"Title": "Results for " + tag.Name, "Tag": tag, "Data": result, "Queries": queries, "Page": pageInt}) +} diff --git a/run.sh b/run.sh index f04ca38..904cc48 100644 --- a/run.sh +++ b/run.sh @@ -1,6 +1,5 @@ #!/bin/sh - # Update the program every time you run? # git pull