add support for organizations

このコミットが含まれているのは:
Arya Kiran 2023-02-13 19:29:44 +05:30
コミット 924a41ddca
この署名に対応する既知のキーがデータベースに存在しません
GPGキーID: 842D12BDA50DF120
3個のファイルの変更35行の追加11行の削除

ファイルの表示

@ -23,12 +23,13 @@ type User struct {
StatusEmoji string
AvatarUrl string
Location string
Email string
Timezone string
Following int64
Followers int64
Link string
Social []string
Organization []string
Organizations []string
Company string
Type string
Contributions string
@ -115,7 +116,6 @@ func HandleUser(c *fiber.Ctx) error {
// scraping
Scrape := User{
Link: link,
Readme: string(readmeOutput),
}
@ -131,7 +131,6 @@ func HandleUser(c *fiber.Ctx) error {
sc1.Visit("https://github.com/" + c.Params("user") + "/")
sc := colly.NewCollector(colly.AllowedDomains("github.com"), colly.UserAgent(UserAgent))
if Scrape.Type == "http://schema.org/Person" {
// Bio
sc.OnHTML("div[data-bio-text]", func(e *colly.HTMLElement) {
@ -149,7 +148,7 @@ func HandleUser(c *fiber.Ctx) error {
e.ForEach("li[itemprop*='social']", func(i int, el *colly.HTMLElement) {
Scrape.Social = append(Scrape.Social, el.ChildText("a.Link--primary"))
})
Scrape.Link = e.ChildText("a[itemprop*='url']")
})
// Followers/Following
sc.OnHTML("a[href*='https://github.com/"+c.Params("user")+"?tab=followers' i]", func(e *colly.HTMLElement) {
@ -178,21 +177,42 @@ func HandleUser(c *fiber.Ctx) error {
// Organizations
sc.OnHTML("div.mt-3", func(e *colly.HTMLElement) {
e.ForEach("a[data-hovercard-type*='organization']", func(i int, el *colly.HTMLElement) {
Scrape.Organization = append(Scrape.Organization, el.Attr("aria-label"))
Scrape.Organizations = append(Scrape.Organizations, el.Attr("aria-label"))
})
})
} else {
sc.OnHTML("div.container-xl div.flex-md-items-center div.flex-1", func(e *colly.HTMLElement) {
Scrape.Bio = e.ChildText("div.color-fg-muted div")
Scrape.Followers, err = strconv.ParseInt(e.ChildText("a[href*='/orgs/"+c.Params("user")+"/followers' i] span"), 10, 64)
Scrape.Name = e.ChildText("h1.h2")
Scrape.Location = e.ChildText("span[itemprop*='location']")
Scrape.Link = e.ChildText("a[itemprop*='url']")
Scrape.Email = e.ChildText("a[itemprop*='email']")
e.ForEach("a.Link--primary", func(i int, el *colly.HTMLElement) {
Scrape.Social = append(Scrape.Social, el.Attr("href"))
})
})
sc.OnHTML("img[alt*='@"+c.Params("user")+"' i]", func(e *colly.HTMLElement) {
Scrape.AvatarUrl = e.Attr("src")
Scrape.Login = e.Attr("alt")
})
log.Println("Bio and Location routes cannot be scraped for organizations")
}
sc.Visit("https://github.com/" + c.Params("user") + "/")
// Fixing the output a bit
Scrape.AvatarUrl = strings.TrimPrefix(Scrape.AvatarUrl, "https://avatars.githubusercontent.com/u/")
Scrape.AvatarUrl = "/avatar/" + Scrape.AvatarUrl
Scrape.StatusEmoji = emoji.Parse(":"+Scrape.StatusEmoji+":")
if Scrape.StatusEmoji != "" {
Scrape.StatusEmoji = emoji.Parse(":" + Scrape.StatusEmoji + ":")
}
Scrape.Login = strings.TrimPrefix(Scrape.Login, "@") // Only for orgs
if strings.HasPrefix(Scrape.Link, "https://") {
Scrape.Link = strings.TrimPrefix(Scrape.Link, "https://")
} else if strings.HasPrefix(Scrape.Link, "http://") {
Scrape.Link = strings.TrimPrefix(Scrape.Link, "http://")
} else {
log.Println("Has no prefix")
}
// Add scrape-based info to userArray
userArray = append(userArray, Scrape)

ファイルの表示

@ -1,8 +1,10 @@
package utils
import (
"regexp"
"github.com/microcosm-cc/bluemonday"
"regexp"
)
// copied from bluemonday's GitHub repostiory, with some adaptations
func UGCPolicy() *bluemonday.Policy {
@ -194,4 +196,3 @@ func UGCPolicy() *bluemonday.Policy {
return p
}

ファイルの表示

@ -29,14 +29,17 @@
{{ if .Link }}
<p>🌐 <a href="https://{{.Link}}" target="_blank">{{.Link}}</a></p>
{{ end }}
{{ if .Email }}
<p>✉️ {{.Email}}</p>
{{ end }}
{{ if .Social }}
{{range .Social}}
<p>🔗 <a href="{{.}}" target="_blank">{{.}}</a></p>
{{ end }}
{{ end }}
{{ if .Organization }}
{{ if .Organizations }}
<p>Organizations:
{{range .Organization}}
{{range .Organizations}}
<a href="/{{.}}" target="_blank">{{.}}</a>
{{ end }}
</p>