gostars

package
v0.0.3 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jun 15, 2026 License: MIT Imports: 17 Imported by: 0

Documentation

Overview

Package gostars provides utilities to fetch and score GitHub package popularity.

Index

Examples

Constants

This section is empty.

Variables

View Source
var GithubToken string

GithubToken is a personal access token for the GitHub API that must be assigned by the caller and must not be hard-coded in the source code.

View Source
var IOCopy = io.Copy

IOCopy is a copy of io.Copy to ease test.

View Source
var URLAliases = map[string]string{
	"https://joe-bot.net/": "https://github.com/go-joe/joe",
}

URLAliases are a mapping between the site URL and the actual URL of the GitHub repository.

View Source
var URLAwesomeGo = urlAwesomeGoDefault

URLAwesomeGo is the URL of Awesome-Go's README.md. Which is the markdown file of the awesome Go packages.

Functions

func CoolDown

func CoolDown()

CoolDown is a sleep function to avoid a large number of requests to each API.

It is currently forced to 1 second.

func GetAttractionGravity

func GetAttractionGravity(points ...int) int

GetAttractionGravity returns the distance from the point 0 to the point of "points" dimensions.

Each point should be the value of comparison. Such as number of stars, number of forks, etc.

Example
package main

import (
	"fmt"

	"github.com/KEINOS/gostars/gostars"
)

func main() {
	{
		forks := 1
		likes := 10
		followers := 0
		importedBy := 100

		gravity := gostars.GetAttractionGravity(forks, likes, followers, importedBy)

		fmt.Println("Light Star:", gravity)
	}
	{
		forks := 10
		likes := 100
		followers := 10
		importedBy := 1000

		gravity := gostars.GetAttractionGravity(forks, likes, followers, importedBy)

		fmt.Println("Heaby Star:", gravity)
	}

}
Output:
Light Star: 100
Heaby Star: 1005

func GetContentURL

func GetContentURL(urlTarget string) ([]byte, error)

GetContentURL returns the content of a given URL.

To avoid a large number of requests to the target server, it sleeps for about one second.

Example
package main

import (
	"fmt"
	"log"
	"strings"

	"github.com/KEINOS/gostars/gostars"
)

func main() {
	rawContent, err := gostars.GetContentURL("https://github.com/KEINOS")
	if err != nil {
		log.Fatal(err)
	}

	source := string(rawContent)

	fmt.Println(strings.Contains(source, "Profile of KEINOS"))

}
Output:
true

func GetURLGitHub

func GetURLGitHub(urlOrigin string) string

GetURLGitHub will return the URL of the GitHub repository if urlOrigin matches the alias list.

Example
package main

import (
	"fmt"

	"github.com/KEINOS/gostars/gostars"
)

func main() {
	urlSite := "https://joe-bot.net/"

	// Get the actual URL of the GitHub repository from the mapping
	urlGitHub := gostars.GetURLGitHub(urlSite)

	fmt.Println(urlGitHub)

}
Output:
https://github.com/go-joe/joe

func Hash256

func Hash256(input []byte) string

Hash256 returns the SHA2-256 (FIPS 180-4) hashed hex string from input.

This function is not suitable for large files, as it copies all bytes into memory.

Example
package main

import (
	"fmt"

	"github.com/KEINOS/gostars/gostars"
)

func main() {
	out := gostars.Hash256([]byte("sample"))

	fmt.Println(out)
}
Output:
af2bdbe1aa9b6ec1e2ade1d694f41fc71a831d0268e9891562113d8a62add1bf

func NewQuery

func NewQuery(sourceHTML []byte) (*goquery.Document, error)

NewQuery returns a query object that processes HTML documents in a simple, jQuery-like manner. Powered by GoQuery.

Example
package main

import (
	"fmt"
	"log"

	"github.com/KEINOS/gostars/gostars"
	"github.com/PuerkitoBio/goquery"
)

func main() {
	html := `
<body>
<ul>
<li><a href="link_to_foo">foo</a></li>
<li><a href="link_to_bar">bar</a></li>
</ul>
</body>
	`

	goQuery, err := gostars.NewQuery([]byte(html))
	if err != nil {
		log.Fatal(err)
	}

	foundLinks := make([]string, 0) // Var to store found links

	// Callback function to store the link if the selection s contains an href attribute.
	setHref := func(_ int, s *goquery.Selection) {
		href, ok := s.Attr("href")
		if !ok {
			log.Fatal("fail to get attribute's value for the first element in the selection")
		}

		foundLinks = append(foundLinks, href)
	}

	// Query to find "body.li.a" element and iterate with `setHref` function.
	goQuery.Find("body li > a:first-child").Each(setHref)

	// Print the result
	for i, link := range foundLinks {
		fmt.Println("#", i, "LINK:", link)
	}

}
Output:
# 0 LINK: link_to_foo
# 1 LINK: link_to_bar

func ParseMarkdownToHTML

func ParseMarkdownToHTML(markdown []byte) string

ParseMarkdownToHTML parses the markdown content to HTML.

Example
package main

import (
	"fmt"

	"github.com/KEINOS/gostars/gostars"
)

func main() {
	markdown := `
# Hello
Hello, world!
	`

	out := gostars.ParseMarkdownToHTML([]byte(markdown))

	fmt.Println(out)

}
Output:
<body><h1>Hello</h1>

<p>Hello, world!</p>
</body>

func PrettyFormatJSON

func PrettyFormatJSON(v any) (string, error)

PrettyFormatJSON is a formatter for printing objects in a pretty way.

Example
package main

import (
	"fmt"
	"log"

	"github.com/KEINOS/gostars/gostars"
)

func main() {
	type myStruct struct {
		Foo string `json:"foo"`
		Bar string `json:"bar"`
	}

	myObj := myStruct{
		Foo: "hoge",
		Bar: "fuga",
	}

	result, err := gostars.PrettyFormatJSON(myObj)
	if err != nil {
		log.Fatal(err)
	}

	fmt.Println(result)

}
Output:
{
  "foo": "hoge",
  "bar": "fuga"
}

Types

type PkgInfo

type PkgInfo struct {
	Name       string `json:"name"`       // Name of the package
	Repository string `json:"repository"` // Repository URL of the package
	ImportedBy int    `json:"importedBy"` // Number of packages that imports this package
}

PkgInfo holds information about the package from pkg.go.dev. It is mainly used to obtain the number of packages using this package.

func NewPkgInfo

func NewPkgInfo(pkgName string) (*PkgInfo, error)

NewPkgInfo returns the initialized object of PkgInfo from pkagName.

Example
package main

import (
	"fmt"
	"log"

	"github.com/KEINOS/gostars/gostars"
)

func main() {
	// Get package info from "pkg.go.dev"
	pkgInfo, err := gostars.NewPkgInfo("github.com/KEINOS/go-utiles/util")
	if err != nil {
		log.Fatal(err)
	}

	// Get number of packages that uses this package
	if pkgInfo.ImportedBy > 3 {
		fmt.Println("This package has been used by 3 or more packages.")
	}

	fmt.Println("The URL of the repository:", pkgInfo.Repository)

}
Output:
This package has been used by 3 or more packages.
The URL of the repository: https://github.com/KEINOS/go-utiles

func (*PkgInfo) Update

func (p *PkgInfo) Update() error

Update pulls the package information and sets to the according field.

func (*PkgInfo) UpdateImportedBy

func (p *PkgInfo) UpdateImportedBy() error

UpdateImportedBy updates the imported number by other packages if the package name is a valid package in pkg.go.dev.

func (*PkgInfo) UpdateURLRepository

func (p *PkgInfo) UpdateURLRepository() error

UpdateURLRepository adds "https://" to the repository if the package name is a valid package in pkg.go.dev.

type RepoInfo

type RepoInfo struct {
	URL         *URLInfo `json:"url"`         // Parsed URL info of the repo
	Description string   `json:"description"` // Desctiption of the repo
	Name        string   `json:"name"`        // Name of the repo
	Owner       string   `json:"owner"`       // Name of the repo owner
	Stars       int      `json:"stars"`       // Number of stars of the repo
	Forks       int      `json:"forks"`       // Number of forked repo of the repo
	Followers   int      `json:"followers"`   // Number of watching people
}

RepoInfo contains information about the repository on GitHub. It is mainly used to retrieve the number of stars, forks, followers, etc. from the repository.

func NewRepoInfo

func NewRepoInfo(urlRepo string) (*RepoInfo, error)

NewRepoInfo returns the initialized object of RepoInfo from the given GitHub's URL.

Example
package main

import (
	"fmt"
	"log"

	"github.com/KEINOS/gostars/gostars"
)

func main() {
	repoInfo, err := gostars.NewRepoInfo("https://github.com/KEINOS/dev-go")
	if err != nil {
		log.Fatal(err)
	}

	fmt.Println("Name repo:", repoInfo.Name)
	fmt.Println("Name owner:", repoInfo.Owner)

}
Output:
Name repo: dev-go
Name owner: KEINOS

func (*RepoInfo) Update

func (r *RepoInfo) Update() error

Update retrieves the repository information from GitHub and sets it in the corresponding field.

type URLInfo

type URLInfo struct {
	RawURL string   // the original url
	Scheme string   // protocol
	Host   string   // host or host:port
	Path   []string // slice of directory path
}

URLInfo contains information about the parsed URL.

func NewURLInfo

func NewURLInfo(urlTarget string) (*URLInfo, error)

NewURLInfo returns the initialized object of URLInfo from urlTarget.

Example
package main

import (
	"fmt"
	"log"

	"github.com/KEINOS/gostars/gostars"
)

func main() {
	urlInfo, err := gostars.NewURLInfo("https://github.com/KEINOS/gostars")
	if err != nil {
		log.Fatal(err)
	}

	fmt.Println("Scheme:", urlInfo.Scheme)
	fmt.Println("Host:", urlInfo.Host)
	fmt.Println("Is GitHub repo:", urlInfo.IsRepoGitHub())
	fmt.Println("Stringer:", urlInfo)

}
Output:
Scheme: https
Host: github.com
Is GitHub repo: true
Stringer: https://github.com/KEINOS/gostars

func (*URLInfo) IsRepoGitHub

func (u *URLInfo) IsRepoGitHub() bool

IsRepoGitHub returns true if the host is GitHub.

func (*URLInfo) String

func (u *URLInfo) String() string

String is an implementation of Stringer.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL