diff --git a/pages/topics.go.wip b/pages/topics.go.wip new file mode 100644 index 0000000..3ad84a4 --- /dev/null +++ b/pages/topics.go.wip @@ -0,0 +1,102 @@ +package pages + +import ( + "bytes" + "context" + "log" + "net/http" + "os" + "strings" + + "codeberg.org/gothub/gothub/utils" + "github.com/gocolly/colly" + "github.com/gofiber/fiber/v2" + "github.com/sirupsen/logrus" +) + +type TopicMeta struct { + Name string + Desc string + ImageLink string + CreatedBy string + Released string + Followers string + Company string + Links []string + Related []string + Language []string + Sort string +} + +type TopicStuff struct { + FullName string + Desc string + Stars string + Language string + OtherTags []string + LastUpdate []string + ImageLink string +} + +func HandleTopics(c *fiber.Ctx) error { + var topicMetaArray []TopicMeta + var topicStuffArray []TopicStuff + // Scraping + 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("github.com"), colly.UserAgent(UserAgent)) + sc.OnHTML("div.border h1", func(e *colly.HTMLElement) { + topicMetaArray.FullName = e.Text + } + sc.OnHTML("div.p-4.mb-5", func(e *colly.HTMLElement) { + topicMetaArray.Desc = e.ChildText("div.markdown-body p") + topicMetaArray.Image = strings.TrimPrefix(e.ChildAttr("div.ml-sm-4 img", "src"), "https://raw.githubusercontent.com/") + }) + sc.OnHTML("div.col-md-4", func(e *colly.HTMLElement) { + e.ForEach("p", func(i int, el *colly.HTMLElement) { + if e.ChildText("span") == "Created by" { + topicMetaArray.CreatedBy = strings.TrimPrefix(e.Text, "Created by") + } else if e.ChildText("span") == "Released" { + topicMetaArray.Released = strings.TrimPrefix(e.Text, "Released") + } + }) + e.ForEach("dl dd", func(i int, el *colly.HTMLElement) { + if e.ChildAttr("svg", "aria-label") == "Topic followers" { + topicMetaArray.Followers = e.Text + } else if e.ChildText("svg.octicon-organization") { + topicMetaArray.Company = e.ChildText("span") + } else if e.ChildText("svg.octicon-link") { + topicMetaArray.Link = append(topicMetaArray.Link, el.ChildAttr("a", "href")) + } + }) + e.ForEach("a.topic-tag", func(i int, el *colly.HTMLElement) { + topicMetaArray.Related = append(topicMetaArray.Related, e.Text) + }) + }) + sc.OnHTML("div.js-details-container div.Details-content--hidden-not-important", func(e *colly.HTMLElement) { + e.ForEach("div.js-navigation-item", func(i int, el *colly.HTMLElement) { + var FileType string + if el.ChildAttr("div.flex-shrink-0 svg", "aria-label") == "Directory" { + FileType = "dir" + } else { + FileType = "file" + } + repoFilesArray = append(repoFilesArray, RepoFiles{ + Name: el.ChildText("div.flex-auto span.d-block a.js-navigation-open"), + Path: el.ChildText("div.flex-auto span.d-block a.js-navigation-open"), + Type: FileType, + Fullname: topicMetaArray.Fullname, + DefaultBranch: topicMetaArray.DefaultBranch, + }) + }) + }) + sc.Visit("https://github.com/topics/" + c.Params("topic")) + return c.Render("repo", fiber.Map{ + "title": "Topic " + topicMetaArray.Name, + "topicMeta": topicMetaArray, + "topicStuff": topicStuffArray, + }) +} diff --git a/pages/user.go b/pages/user.go index 610cccd..76fa6ed 100644 --- a/pages/user.go +++ b/pages/user.go @@ -40,6 +40,7 @@ type User struct { type RepoList struct { Name string Type string + Link string Desc string Lang string Stars string @@ -154,15 +155,30 @@ func HandleUser(c *fiber.Ctx) error { var MainRepo RepoList MainRepo = RepoList{} // Clear data if old data is present MainRepo.Name = strings.TrimPrefix(el.ChildAttr("div.width-full a", "href"), "/") - MainRepo.Type = el.ChildText("div.width-full span.Label") + MainRepo.Link = el.ChildAttr("div.width-full a", "href") + if strings.Contains(MainRepo.Name, "https://gist.github.com/") { + MainRepo.Name = el.ChildAttr("div.width-full a span", "title") + MainRepo.Link = "/gist" + strings.TrimPrefix(el.ChildAttr("div.width-full a", "href"), "https://gist.github.com") + } + if strings.Contains(MainRepo.Link, "/gist") { + MainRepo.Type = "Gist" + } else { + MainRepo.Type = "Repository" + } MainRepo.Desc = el.ChildText("p.pinned-item-desc") + if MainRepo.Type == "Gist" { + MainRepo.Desc = "" + el.ForEach("div.rounded-bottom-2 div.flex-items-center", func(in int, ele *colly.HTMLElement) { + MainRepo.Desc = MainRepo.Desc + "\n" + ele.ChildText("pre") + }) + } MainRepo.Lang = el.ChildText("p.color-fg-muted span[itemprop*='programmingLanguage']") MainRepo.Stars = el.ChildText("p.color-fg-muted a[href*='/stargazers' i]") MainRepo.Forks = el.ChildText("p.color-fg-muted a[href*='/forks' i]") MainRepo.ForkOf = el.ChildText("p.text-small a.Link--muted") Scrape.MainRepos = append(Scrape.MainRepos, MainRepo) }) - Scrape.PinOrPopular = e.ChildText("h2.text-normal") + Scrape.PinOrPopular = strings.TrimSuffix(e.ChildText("h2.text-normal"), " repositories") }) sc.Visit("https://github.com/" + c.Params("user") + "/") // Fixing the output a bit diff --git a/views/gist.html b/views/gist.html index 6205f26..2d728ca 100644 --- a/views/gist.html +++ b/views/gist.html @@ -22,14 +22,14 @@ {{end}} {{ if .files}}
{{ range $key, $value := .files}} -

{{.Name}}

+

{{.Name}}

{{.File}} {{ end }}
diff --git a/views/index.html b/views/index.html index 5ae2ff5..0caf10a 100644 --- a/views/index.html +++ b/views/index.html @@ -3,19 +3,26 @@

GotHub is an alternative front-end for GitHub.

{{ if not .setupstatus }} -
- GotHub is not set up! Please look at this issue to see how to set up GotHub, or set the GOTHUB_SETUP_COMPLETE environment variable to true to suppress this message. If you are a user of this instance, please contact the instance maintainer. -
+
+ GotHub is not set up! Please look at + this issue to see + how to set up GotHub, or set the GOTHUB_SETUP_COMPLETE environment variable + to true to suppress this message. If you are a user of this instance, please + contact the instance maintainer. +
{{ end}}

Features

Privacy

@@ -24,15 +31,16 @@ href="https://docs.github.com/en/site-policy/privacy-policies/github-privacy-statement" target="_blank" >If you care, you can read GitHub's privacy policy here (TOS;DR). GotHub solves this by proxying all GitHub requests for you! + > + (TOS;DR). + GotHub solves this by proxying all GitHub requests for you!

Usage

Just replace github.com with - {{.host}} or gist.github.com - with {{.host}}/gist! + {{.host}} or + gist.github.com with + {{.host}}/gist!

DMCA/Copyright Notice

@@ -53,9 +61,11 @@ yourself private from them instead.

Credits

- Midou36O - desigining the GotHub logo
- Odyssey346 - majority of the code, maintainer
- Arya - code contributions and second maintainer
+ Midou36O - desigining the GotHub logo
+ Odyssey346 - majority of the code, + maintainer
+ Arya - code contributions and + second maintainer

Info

{{ if eq .version "unknown, please build with Go 1.13+ or use Git"}} GotHub version: unknown {{ else }} GotHub version: diff --git a/views/user.html b/views/user.html index fa29e9e..53c9e36 100644 --- a/views/user.html +++ b/views/user.html @@ -73,15 +73,22 @@ {{ end }} {{ if .MainRepos }}
-

{{.PinOrPopular}} repositories

+

{{.PinOrPopular}} repositories

{{range .MainRepos}} - -

{{.Name}}

- {{if .ForkOf}} - Forked from:
{{.ForkOf}} - {{else}} {{end}} {{if .Desc}} + +

{{.Name}} ({{.Type}})

+ {{if .ForkOf}} Forked from:
{{.ForkOf}} + {{else}} {{end}} {{if .Desc}} {{if eq .Type "Gist"}} + +
 {{.Desc}} 
+ {{else}}

{{.Desc}}

- {{else}} {{end}} + {{end}} {{else}} {{end}}

{{if .Stars}}⭐ {{.Stars}}{{else}}{{end}} {{if .Forks}} 🍴 {{.Forks}} {{else}}{{end}} {{if .Lang}} 🗒️ {{.Lang}} {{else}}{{end}}