From bb7167c3c87ff422d21dd12be4cfaefa0da11f64 Mon Sep 17 00:00:00 2001 From: medanisjbara Date: Sat, 1 Apr 2023 11:17:08 +0100 Subject: [PATCH] replace `*?type=gist` with `/gist/*` --- pages/gist.go | 17 ++++++++--------- serve/serve.go | 23 ++++++++--------------- views/gist.html | 2 +- 3 files changed, 17 insertions(+), 25 deletions(-) diff --git a/pages/gist.go b/pages/gist.go index 844f8cf..0aa9cb3 100644 --- a/pages/gist.go +++ b/pages/gist.go @@ -38,7 +38,7 @@ func HandleGist(c *fiber.Ctx) error { var gistArray []Gist var gistFilesArray []GistFiles - resp, statusErr := http.Get("https://gists.github.com/" + c.Params("user") + "/" + c.Params("repo")) + resp, statusErr := http.Get("https://gists.github.com/" + c.Params("user") + "/" + c.Params("gistID")) if statusErr != nil { log.Println(statusErr) } @@ -46,7 +46,7 @@ func HandleGist(c *fiber.Ctx) error { // I need a better way to do this return c.Status(404).Render("error", fiber.Map{ "title": "Error", - "error": "Repository " + c.Params("user") + "/" + c.Params("repo") + " not found", + "error": "Gist repository " + c.Params("user") + "/" + c.Params("gistID") + " not found", }) } @@ -61,17 +61,16 @@ func HandleGist(c *fiber.Ctx) error { sc := colly.NewCollector(colly.AllowedDomains("gist.github.com"), colly.UserAgent(UserAgent)) sc.OnHTML("div.gisthead", func(e *colly.HTMLElement) { - Scrape.Refname = c.Params("user") + "/" + c.Params("repo") - Scrape.Fullname = c.Params("user") + "/" + e.ChildText("a[href='/"+c.Params("user")+"/"+c.Params("repo")+"' i]") + Scrape.Refname = c.Params("user") + "/" + c.Params("gistID") + Scrape.Fullname = c.Params("user") + "/" + e.ChildText("a[href='/"+c.Params("user")+"/"+c.Params("gistID")+"' i]") dl := e.ChildAttr("a[href$='.zip']", "href") Scrape.Revision = dl[strings.LastIndex(dl,"/")+1 : len(dl)-len(".zip")] - Scrape.Stars = e.ChildText("ul li a:first") - Scrape.Stars = e.ChildText("li a[href*='/" + c.Params("user") + "/" + c.Params("repo") + "/stargazers' i]") + Scrape.Stars = e.ChildText("li a[href*='/" + c.Params("user") + "/" + c.Params("gistID") + "/stargazers' i]") Scrape.Parent = e.ChildText("span.note a") Scrape.Forks = e.ChildText("li span.social-count") // I need a better way to do this if Scrape.Forks == "" { - Scrape.Forks = e.ChildText("li a[href*='/" + c.Params("user") + "/" + c.Params("repo") + "/forks' i]") + Scrape.Forks = e.ChildText("li a[href*='/" + c.Params("user") + "/" + c.Params("gistID") + "/forks' i]") } }) @@ -127,12 +126,12 @@ func HandleGist(c *fiber.Ctx) error { }) }) - sc.Visit("https://gist.github.com/" + c.Params("user") + "/" + c.Params("repo") + "/") + sc.Visit("https://gist.github.com/" + c.Params("user") + "/" + c.Params("gistID") + "/") // Add scrape-based info to gistArray gistArray = append(gistArray, Scrape) return c.Render("gist", fiber.Map{ - "title": "Repository " + c.Params("user") + "/" + c.Params("repo"), + "title": "Repository " + c.Params("user") + "/" + c.Params("gistID"), "gist": gistArray, "files": gistFilesArray, }) diff --git a/serve/serve.go b/serve/serve.go index 9015e46..6ee4a3c 100644 --- a/serve/serve.go +++ b/serve/serve.go @@ -108,27 +108,20 @@ func Serve(port string) { utils.ProxyRequest(c, "https://avatars.githubusercontent.com/u/"+c.Params("id")+"?v=4") return nil }) - app.Get("/:user/:repo", func(c *fiber.Ctx) error { - if c.Query("type") == "gist" { - return pages.HandleGist(c) - } - return pages.HandleRepo(c) - }) + app.Get("/:user/:repo", pages.HandleRepo) app.Get("/:user/:repo/blob/:branch/+", pages.FileView) app.Get("/:user/:repo/tree/:branch/+", pages.DirView) app.Get("/:user/:repo/tree/:branch", pages.HandleRepo) app.Get("/download/:user/:repo/:branch", func(c *fiber.Ctx) error { - var baseURL string - if c.Query("type") == "gist" { - baseURL = "https://gist.github.com/" - } else { - baseURL = "https://github.com/" - } - - url := baseURL + c.Params("user") + "/" + c.Params("repo") + "/archive/" + c.Params("branch") + ".zip" - utils.ProxyRequest(c, url) + utils.ProxyRequest(c, "https://github.com/"+c.Params("user")+"/"+c.Params("repo")+"/archive/"+c.Params("branch")+".zip") return nil }) + app.Get("/gist/:user/:gistID", pages.HandleGist) + app.Get("/download/gist/:user/:gistID/:revision", func(c *fiber.Ctx) error { + utils.ProxyRequest(c, "https://gist.github.com/"+c.Params("user")+"/"+c.Params("gistID")+"/archive/"+c.Params("revision")+".zip") + return nil + }) + app.Get("/raw/:user/:repo/:branch/:file", func(c *fiber.Ctx) error { utils.ProxyRequest(c, "https://raw.githubusercontent.com/"+c.Params("user")+"/"+c.Params("repo")+"/"+c.Params("branch")+"/"+c.Params("file")) return nil diff --git a/views/gist.html b/views/gist.html index bf6641a..9c49931 100644 --- a/views/gist.html +++ b/views/gist.html @@ -3,7 +3,7 @@
{{ if .gist }} {{ range $key, $value := .gist}}