-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
56 lines (50 loc) · 1.6 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
package main
import (
"fmt"
"net/http"
"os"
"github.com/gin-gonic/gin"
"github.com/gocolly/colly"
"github.com/gocolly/colly/extensions"
"github.com/joho/godotenv"
)
type Repo struct {
Title string `json:"title"`
Description string `json:"description"`
Link string `json:"link"`
Language string `json:"language"`
Stars string `json:"stars"`
Forks string `json:"forks"`
}
func scrape(link string) []Repo {
var repos []Repo
collector := colly.NewCollector(
colly.AllowedDomains("github.com"),
)
extensions.RandomUserAgent(collector)
collector.OnHTML(".pinned-item-list-item-content", func(content *colly.HTMLElement) {
title := content.ChildText("span.repo")
description := content.ChildText("p.pinned-item-desc")
repoLink := fmt.Sprintf("https://github.com%s", content.ChildAttr("a", "href"))
language := content.ChildText("span[itemprop=programmingLanguage]")
stars := content.ChildText(`a[href$="/stargazers"]`)
forks := content.ChildText(`a[href$="/forks"]`)
repos = append(repos, Repo{Title: title, Description: description, Link: repoLink, Language: language, Stars: stars, Forks: forks})
})
collector.Visit(link)
return repos
}
func main() {
godotenv.Load()
router := gin.Default()
router.GET("/", func(context *gin.Context) {
repos := scrape("https://github.com/xilaluna")
context.IndentedJSON(http.StatusOK, repos)
})
router.GET("/:id", func(context *gin.Context) {
link := fmt.Sprintf("https://github.com/%s", context.Param("id"))
repos := scrape(link)
context.IndentedJSON(http.StatusOK, repos)
})
router.Run("0.0.0.0:" + os.Getenv("PORT"))
}