mirror of
https://codeberg.org/VnPower/PixivFE
synced 2024-12-06 19:16:23 +01:00
Add bookmark route #7
This commit is contained in:
+15
-1
@@ -55,7 +55,7 @@ func GetToken(c *fiber.Ctx) string {
|
||||
func CheckToken(c *fiber.Ctx) string {
|
||||
sess, err := Store.Get(c)
|
||||
if err != nil {
|
||||
log.Fatalln("Failed to get current session and its values! Falling back to server default!")
|
||||
log.Fatalln("Failed to get current session and its values!")
|
||||
return ""
|
||||
}
|
||||
value := sess.Get("Token")
|
||||
@@ -66,6 +66,20 @@ func CheckToken(c *fiber.Ctx) string {
|
||||
return ""
|
||||
}
|
||||
|
||||
func GetCSRFToken(c *fiber.Ctx) string {
|
||||
sess, err := Store.Get(c)
|
||||
if err != nil {
|
||||
log.Fatalln("Failed to get current session and its values!")
|
||||
return ""
|
||||
}
|
||||
value := sess.Get("CSRF")
|
||||
if value != nil {
|
||||
return value.(string)
|
||||
}
|
||||
|
||||
return ""
|
||||
}
|
||||
|
||||
func SetSessionValue(c *fiber.Ctx, name, value string) error {
|
||||
sess, err := Store.Get(c)
|
||||
if err != nil {
|
||||
|
||||
@@ -130,6 +130,7 @@ func main() {
|
||||
self.Get("/", pages.LoginUserPage)
|
||||
self.Get("/followingWorks", pages.FollowingWorksPage)
|
||||
self.Get("/bookmarks", pages.LoginBookmarkPage)
|
||||
self.Get("/addBookmark/:id", pages.AddBookmarkRoute)
|
||||
|
||||
server.Get("tags/:name", pages.TagPage)
|
||||
server.Post("tags",
|
||||
|
||||
@@ -0,0 +1,82 @@
|
||||
package pages
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"errors"
|
||||
"fmt"
|
||||
"io"
|
||||
"net/http"
|
||||
|
||||
session "codeberg.org/vnpower/pixivfe/v2/core/config"
|
||||
"github.com/gofiber/fiber/v2"
|
||||
"github.com/tidwall/gjson"
|
||||
)
|
||||
|
||||
func pixivPostRequest(id, token, csrf string) error {
|
||||
URL := "https://www.pixiv.net/ajax/illusts/bookmarks/add"
|
||||
|
||||
requestBody := []byte(fmt.Sprintf(`{
|
||||
"illust_id": "%s",
|
||||
"restrict": 0,
|
||||
"comment": "",
|
||||
"tags": []
|
||||
}`, id))
|
||||
|
||||
req, _ := http.NewRequest("POST", URL, bytes.NewBuffer(requestBody))
|
||||
req.Header.Add("User-Agent", "Mozilla/5.0")
|
||||
req.Header.Add("Accept", "application/json")
|
||||
req.Header.Add("Content-Type", "application/json; charset=utf-8")
|
||||
req.Header.Add("Cookie", "PHPSESSID="+token)
|
||||
req.Header.Add("x-csrf-token", csrf)
|
||||
// req.AddCookie(&http.Cookie{
|
||||
// Name: "PHPSESSID",
|
||||
// Value: token,
|
||||
// })
|
||||
|
||||
resp, err := http.DefaultClient.Do(req)
|
||||
if err != nil {
|
||||
return errors.New("Failed to do this action.")
|
||||
}
|
||||
|
||||
body, err := io.ReadAll(resp.Body)
|
||||
if err != nil {
|
||||
return errors.New("Cannot parse the response from Pixiv. Please report this issue.")
|
||||
}
|
||||
|
||||
errr := gjson.Get(string(body), "error")
|
||||
|
||||
if !errr.Exists() {
|
||||
return errors.New("Incompatible request body.")
|
||||
}
|
||||
|
||||
if errr.Bool() {
|
||||
return errors.New("Pixiv: Invalid request.")
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func AddBookmarkRoute(c *fiber.Ctx) error {
|
||||
token := session.CheckToken(c)
|
||||
if token == "" {
|
||||
return c.Redirect("/login")
|
||||
}
|
||||
|
||||
csrf := session.GetCSRFToken(c)
|
||||
if csrf == "" {
|
||||
return c.Redirect("/login")
|
||||
}
|
||||
|
||||
id := c.Params("id")
|
||||
if id == "" {
|
||||
return errors.New("No ID provided.")
|
||||
}
|
||||
println("2")
|
||||
|
||||
if err := pixivPostRequest(id, token, csrf); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
println("1")
|
||||
|
||||
return c.SendString("Success")
|
||||
}
|
||||
+9
-1
@@ -1,9 +1,17 @@
|
||||
package pages
|
||||
|
||||
import (
|
||||
core "codeberg.org/vnpower/pixivfe/v2/core/webapi"
|
||||
"github.com/gofiber/fiber/v2"
|
||||
)
|
||||
|
||||
func IndexPage(c *fiber.Ctx) error {
|
||||
return c.Render("pages/index", fiber.Map{"Title": "Landing", "Token": false})
|
||||
works, err := core.GetRanking(c, "daily", "illust", "", "1")
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return c.Render("pages/index", fiber.Map{
|
||||
"Title": "Landing", "Token": false, "Data": works,
|
||||
})
|
||||
}
|
||||
|
||||
+34
-2
@@ -2,9 +2,12 @@ package pages
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"io"
|
||||
"net/http"
|
||||
"regexp"
|
||||
"strings"
|
||||
|
||||
session "codeberg.org/vnpower/pixivfe/v2/core/config"
|
||||
http "codeberg.org/vnpower/pixivfe/v2/core/http"
|
||||
"github.com/gofiber/fiber/v2"
|
||||
)
|
||||
|
||||
@@ -12,12 +15,41 @@ func setToken(c *fiber.Ctx) error {
|
||||
// Parse the value from the form
|
||||
token := c.FormValue("token")
|
||||
if token != "" {
|
||||
if _, err := http.UnwrapWebAPIRequest("https://www.pixiv.net/ajax/user/extra", token); err != nil {
|
||||
// Make a test request to verify the token.
|
||||
// THE TEST URL IS NSFW!
|
||||
req, _ := http.NewRequest("GET", "https://www.pixiv.net/en/artworks/115365120", nil)
|
||||
req.Header.Add("User-Agent", "Mozilla/5.0")
|
||||
req.AddCookie(&http.Cookie{
|
||||
Name: "PHPSESSID",
|
||||
Value: token,
|
||||
})
|
||||
|
||||
resp, err := http.DefaultClient.Do(req)
|
||||
if err != nil {
|
||||
return errors.New("Cannot authorize with supplied token.")
|
||||
}
|
||||
|
||||
body, err := io.ReadAll(resp.Body)
|
||||
if err != nil {
|
||||
return errors.New("Cannot parse the response from Pixiv. Please report this issue.")
|
||||
}
|
||||
|
||||
// CSRF token
|
||||
r := regexp.MustCompile(`"token":"([0-9a-f]+)"`)
|
||||
csrf := strings.Split(r.FindString(string(body)), ":")[1]
|
||||
csrf = csrf[1 : len(csrf)-1]
|
||||
|
||||
if csrf == "" {
|
||||
return errors.New("Cannot authorize with supplied token.")
|
||||
}
|
||||
|
||||
// Set the tokens
|
||||
if err := session.SetSessionValue(c, "Token", token); err != nil {
|
||||
return err
|
||||
}
|
||||
if err := session.SetSessionValue(c, "CSRF", csrf); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
@@ -1,4 +1,31 @@
|
||||
<div class="container">
|
||||
<h2>Welcome!</h2>
|
||||
<p>Please check out the sidebar for more features. <br>There used to be a small ranking container here... but I am planning to add more than just that!</p>
|
||||
<div class="component-header">
|
||||
<h2>Today's illustration rankings</h2>
|
||||
<a href="/ranking?content=all">See more</a>
|
||||
</div>
|
||||
<div class="artwork-container-scroll">
|
||||
{{ range Data.Contents }}
|
||||
<div class="artwork-small artwork">
|
||||
<div class="artwork-additional">
|
||||
<div class="artwork-position">{{ .Rank }}</div>
|
||||
{{ if toInt(.Pages) > 1 }}
|
||||
<div class="artwork-page-count"><span>⧉ {{ .Pages }}</span></div>
|
||||
{{ end }}
|
||||
</div>
|
||||
|
||||
<a href="/artworks/{{ .ID }}">
|
||||
<img src="{{ .Image }}" alt="{{ .Title }}" />
|
||||
</a>
|
||||
<div class="artwork-title">
|
||||
<a href="/artworks/{{ .ID }}"> {{ .Title }} </a>
|
||||
</div>
|
||||
<div class="artwork-author">
|
||||
<a href="/users/{{ .ArtistID }}"
|
||||
><img src="{{ .ArtistAvatar }}" alt="{{ .ArtistName }}" />
|
||||
<span>{{ .ArtistName }}</span></a
|
||||
>
|
||||
</div>
|
||||
</div>
|
||||
{{ end }}
|
||||
</div>
|
||||
</div>
|
||||
|
||||
Reference in New Issue
Block a user