mirror of
https://github.com/SphericalKat/medium.rip.git
synced 2024-12-26 01:25:57 +00:00
feat: add id parsing
Signed-off-by: Sphericalkat <amolele@gmail.com>
This commit is contained in:
parent
665d33d9d3
commit
aacabdd54d
@ -10,9 +10,9 @@ import (
|
||||
)
|
||||
|
||||
func show(c *fiber.Ctx) error {
|
||||
postId := c.Params("id", "")
|
||||
postId := converters.ConvertId(c)
|
||||
if postId == "" {
|
||||
return c.Redirect("/")
|
||||
return c.Status(422).SendString("Invalid post ID")
|
||||
}
|
||||
|
||||
e, err := client.PostData(postId)
|
||||
@ -42,5 +42,5 @@ func index(c *fiber.Ctx) error {
|
||||
|
||||
func RegisterRoutes(app *fiber.App) {
|
||||
app.Get("/", index)
|
||||
app.Get("/:id", show)
|
||||
app.Get("/*", show)
|
||||
}
|
@ -14,7 +14,7 @@ import (
|
||||
)
|
||||
|
||||
func PostData(postId string) (*entities.MediumResponse, error) {
|
||||
if config.Conf.Env == "dev" {
|
||||
if config.Conf.Env == "devd" {
|
||||
file, err := os.ReadFile("response.json")
|
||||
if err != nil {
|
||||
return nil, err
|
||||
@ -33,49 +33,7 @@ func PostData(postId string) (*entities.MediumResponse, error) {
|
||||
url := "https://medium.com/_/graphql"
|
||||
method := "POST"
|
||||
|
||||
payload := strings.NewReader(fmt.Sprintf(`query {
|
||||
post(id: "%s") {
|
||||
title
|
||||
createdAt
|
||||
creator {
|
||||
id
|
||||
name
|
||||
}
|
||||
content {
|
||||
bodyModel {
|
||||
paragraphs {
|
||||
name
|
||||
text
|
||||
type
|
||||
href
|
||||
layout
|
||||
markups {
|
||||
title
|
||||
type
|
||||
href
|
||||
userId
|
||||
start
|
||||
end
|
||||
anchorType
|
||||
}
|
||||
iframe {
|
||||
mediaResource {
|
||||
href
|
||||
iframeSrc
|
||||
iframeWidth
|
||||
iframeHeight
|
||||
}
|
||||
}
|
||||
metadata {
|
||||
id
|
||||
originalWidth
|
||||
originalHeight
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}`, postId))
|
||||
payload := strings.NewReader(fmt.Sprintf("{\"query\":\"query {\\n post(id: \\\"%s\\\") {\\n title\\n createdAt\\n creator {\\n id\\n name\\n }\\n content {\\n bodyModel {\\n paragraphs {\\n name\\n text\\n type\\n href\\n layout\\n markups {\\n title\\n type\\n href\\n userId\\n start\\n end\\n anchorType\\n }\\n iframe {\\n mediaResource {\\n href\\n iframeSrc\\n iframeWidth\\n iframeHeight\\n }\\n }\\n metadata {\\n id\\n originalWidth\\n originalHeight\\n }\\n }\\n }\\n }\\n }\\n }\",\"variables\":{}}", postId))
|
||||
|
||||
client := &http.Client{}
|
||||
req, err := http.NewRequest(method, url, payload)
|
||||
|
46
pkg/converters/id_converter.go
Normal file
46
pkg/converters/id_converter.go
Normal file
@ -0,0 +1,46 @@
|
||||
package converters
|
||||
|
||||
import (
|
||||
"net/url"
|
||||
"regexp"
|
||||
"strings"
|
||||
|
||||
"github.com/gofiber/fiber/v2"
|
||||
)
|
||||
|
||||
var r = regexp.MustCompile(`[\/\-]([0-9a-f]+)\/?$`)
|
||||
|
||||
func ConvertId(c *fiber.Ctx) string {
|
||||
path := c.Path()
|
||||
postId := idFromParams(c)
|
||||
if postId == "" {
|
||||
return idFromPath(path)
|
||||
}
|
||||
return ""
|
||||
}
|
||||
|
||||
func idFromPath(path string) string {
|
||||
if strings.HasPrefix(path, "/tag/") {
|
||||
return ""
|
||||
}
|
||||
|
||||
matches := r.FindStringSubmatch(path)
|
||||
if len(matches) != 2 {
|
||||
return ""
|
||||
}
|
||||
|
||||
return matches[1]
|
||||
}
|
||||
|
||||
func idFromParams(c *fiber.Ctx) string {
|
||||
ru := c.Query("redirectUrl", "")
|
||||
if ru != "" {
|
||||
pu, err := url.Parse(ru)
|
||||
if err != nil {
|
||||
return ""
|
||||
}
|
||||
|
||||
return idFromPath(pu.Path)
|
||||
}
|
||||
return ""
|
||||
}
|
12
pkg/converters/id_converter_test.go
Normal file
12
pkg/converters/id_converter_test.go
Normal file
@ -0,0 +1,12 @@
|
||||
package converters
|
||||
|
||||
import "testing"
|
||||
|
||||
func TestConvertId(t *testing.T) {
|
||||
id := "@fareedkhandev/pandas-ai-the-future-of-data-analysis-8f0be9b5ab6f"
|
||||
convertedId := idFromPath(id)
|
||||
expected := "8f0be9b5ab6f"
|
||||
if convertedId != expected {
|
||||
t.Errorf("ConvertId(%s) = %s; want %s", id, convertedId, expected)
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user