mirror of
https://codeberg.org/gothub/gothub
synced 2024-12-06 19:16:24 +01:00
add basic gist support
This commit is contained in:
@@ -0,0 +1,90 @@
|
||||
package pages
|
||||
|
||||
import (
|
||||
"github.com/gocolly/colly"
|
||||
"github.com/gofiber/fiber/v2"
|
||||
"log"
|
||||
"net/http"
|
||||
"os"
|
||||
)
|
||||
|
||||
type Gist struct {
|
||||
Fullname string
|
||||
Refname string
|
||||
Description string
|
||||
DownloadLink string
|
||||
Parent string
|
||||
Stars string
|
||||
Forks string
|
||||
Language []string
|
||||
License string
|
||||
DefaultBranch string
|
||||
Readme string
|
||||
}
|
||||
|
||||
type GistFiles struct {
|
||||
Name string
|
||||
}
|
||||
|
||||
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"))
|
||||
if statusErr != nil {
|
||||
log.Println(statusErr)
|
||||
}
|
||||
if resp.StatusCode == 404 {
|
||||
// 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",
|
||||
})
|
||||
}
|
||||
|
||||
// Scraping
|
||||
Scrape := Gist{}
|
||||
|
||||
UserAgent, ok := os.LookupEnv("GOTHUB_USER_AGENT")
|
||||
if !ok {
|
||||
UserAgent = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/110.0.0.0 Safari/537.36"
|
||||
}
|
||||
|
||||
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.DownloadLink = e.ChildAttr("a[href$='.zip']", "href")
|
||||
Scrape.Stars = e.ChildText("ul li a:first")
|
||||
Scrape.Stars = e.ChildText("li a[href*='/" + c.Params("user") + "/" + c.Params("repo") + "/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]")
|
||||
}
|
||||
})
|
||||
|
||||
sc.OnHTML("div[itemprop='about']", func(e *colly.HTMLElement) {
|
||||
Scrape.Description = e.Text
|
||||
})
|
||||
|
||||
sc.OnHTML("div.repository-content.gist-content", func(e *colly.HTMLElement) {
|
||||
e.ForEach("strong.user-select-contain.gist-blob-name.css-truncate-target", func(i int, el *colly.HTMLElement) {
|
||||
gistFilesArray = append(gistFilesArray, GistFiles{
|
||||
Name: el.Text,
|
||||
})
|
||||
})
|
||||
})
|
||||
|
||||
sc.Visit("https://gist.github.com/" + c.Params("user") + "/" + c.Params("repo") + "/")
|
||||
// Add scrape-based info to gistArray
|
||||
gistArray = append(gistArray, Scrape)
|
||||
|
||||
return c.Render("gist", fiber.Map{
|
||||
"title": "Repository " + c.Params("user") + "/" + c.Params("repo"),
|
||||
"gist": gistArray,
|
||||
"files": gistFilesArray,
|
||||
})
|
||||
}
|
||||
+14
-1
@@ -3,6 +3,7 @@ package serve
|
||||
import (
|
||||
"html/template"
|
||||
"log"
|
||||
"net/url"
|
||||
"os"
|
||||
"regexp"
|
||||
"runtime"
|
||||
@@ -108,7 +109,19 @@ func Serve(port string) {
|
||||
utils.ProxyRequest(c, "https://avatars.githubusercontent.com/u/"+c.Params("id")+"?v=4")
|
||||
return nil
|
||||
})
|
||||
app.Get("/:user/:repo", pages.HandleRepo)
|
||||
app.Get("/:user/:repo", ratelimiter, func(c *fiber.Ctx) error {
|
||||
u, err := url.Parse(c.Request().URI().String())
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
params, _ := url.ParseQuery(u.RawQuery)
|
||||
typ := params.Get("type")
|
||||
|
||||
if typ == "gist" {
|
||||
return pages.HandleGist(c)
|
||||
}
|
||||
return pages.HandleRepo(c)
|
||||
})
|
||||
app.Get("/:user/:repo/blob/:branch/+", pages.FileView)
|
||||
app.Get("/:user/:repo/tree/:branch/+", pages.DirView)
|
||||
app.Get("/:user/:repo/tree/:branch", pages.HandleRepo)
|
||||
|
||||
@@ -0,0 +1,57 @@
|
||||
{{ template "header" .}}
|
||||
|
||||
<main>
|
||||
{{ if .gist }}
|
||||
{{ range $key, $value := .gist}}
|
||||
<div class="buttonParent">
|
||||
<a href="https://gist.github.com/{{.DownloadLink}}" class="button">Download (zip)</a>
|
||||
<a href="https://gist.github.com/{{.Refname}}" class="button">View on GitHub</a>
|
||||
</div>
|
||||
|
||||
<div class="userProfile">
|
||||
<h1>{{.Fullname}}</h1>
|
||||
{{ if .Parent }}
|
||||
<p>This repository is a fork of <a href="/{{.Parent}}">{{.Parent}}</a>.</p>
|
||||
{{ end }}
|
||||
{{ if .Description }}
|
||||
<p>{{.Description}}</p>
|
||||
{{ end }}
|
||||
<p>⭐ {{.Stars}} 🍴 {{.Forks}}</p>
|
||||
{{ if .Language }}
|
||||
<p>🗒️
|
||||
{{range .Language}}
|
||||
{{.}}
|
||||
{{end}}
|
||||
</p>
|
||||
{{end}}
|
||||
</div>
|
||||
{{end}}
|
||||
{{ if .files}}
|
||||
<div class="userReadme">
|
||||
<h3>Files</h3>
|
||||
<div class="userReadmeText">
|
||||
<ul class="filesUList">
|
||||
{{ range $key, $value := .files}}
|
||||
<li class="filesList"><a href="#{{.Name}}">{{.Name}}</a></li>
|
||||
{{ end }}
|
||||
</ul>
|
||||
</div>
|
||||
</div>
|
||||
{{ end }}
|
||||
{{ if .readme}}
|
||||
<div class="userReadme">
|
||||
{{ if .repo }}
|
||||
{{ range $key, $value := .repo}}
|
||||
<h3>{{.Readme}}</h3>
|
||||
{{end}}
|
||||
{{end}}
|
||||
<div class="userReadmeText">
|
||||
{{ unescape .readme}}
|
||||
</div>
|
||||
</div>
|
||||
{{ end }}
|
||||
{{ else }}
|
||||
<h2>Repository not found</h2>
|
||||
<p>That repository doesn't exist.</p>
|
||||
{{ end }}
|
||||
</main>
|
||||
Reference in New Issue
Block a user