trending

package module
Version: v0.0.0-...-435db01 Latest Latest
Warning

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

Go to latest
Published: Jul 12, 2021 License: MIT Imports: 6 Imported by: 13

README

GoDoc Build Status Go Report Card

A package to retrieve trending repositories and developers from Github written in golang.

trending package showcase

This package were inspired by rochefort/git-trend (Ruby) and sheharyarn/github-trending (Ruby).

Features

  • Get trending repositories
  • Get trending developers
  • Get trending languages
  • Get all programming languages known by GitHub
  • Filtering by time and (programming) language
  • Support for GitHub Enterprise

Installation

It is go gettable

$ go get github.com/andygrunwald/go-trending

(optional) to run unit / example tests:

$ cd $GOPATH/src/github.com/andygrunwald/go-trending
$ go test -v

API

Please have a look at the GoDoc documentation for a detailed API description.

Examples

Further a few examples how the API can be used. A few more examples are available in the GoDoc examples section.

package main

import (
	"fmt"
	"github.com/andygrunwald/go-trending"
	"log"
)

func main() {
	trend := trending.NewTrending()

	// Show projects of today
	projects, err := trend.GetProjects(trending.TimeToday, "")
	if err != nil {
		log.Fatal(err)
	}
	for index, project := range projects {
		no := index + 1
		if len(project.Language) > 0 {
			fmt.Printf("%d: %s (written in %s with %d ★ )\n", no, project.Name, project.Language, project.Stars)
		} else {
			fmt.Printf("%d: %s (with %d ★ )\n", no, project.Name, project.Stars)
		}
	}
}
package main

import (
	"fmt"
	"github.com/andygrunwald/go-trending"
	"log"
)

func main() {
	trend := trending.NewTrending()

	// Show projects of today
	projects, err := trend.GetProjects(trending.TimeWeek, "go")
	if err != nil {
		log.Fatal(err)
	}
	for index, project := range projects {
		no := index + 1
		if len(project.Language) > 0 {
			fmt.Printf("%d: %s (written in %s with %d ★ )\n", no, project.Name, project.Language, project.Stars)
		} else {
			fmt.Printf("%d: %s (with %d ★ )\n", no, project.Name, project.Stars)
		}
	}
}
package main

import (
	"fmt"
	"github.com/andygrunwald/go-trending"
	"log"
)

func main() {
	trend := trending.NewTrending()

	developers, err := trend.GetDevelopers(trending.TimeMonth, "swift")
	if err != nil {
		log.Fatal(err)
	}
	for index, developer := range developers {
		no := index + 1
		fmt.Printf("%d: %s (%s)\n", no, developer.DisplayName, developer.FullName)
	}
}
List available languages
package main

import (
	"fmt"
	"github.com/andygrunwald/go-trending"
	"log"
)

func main() {
	trend := trending.NewTrending()

	// Show languages
	languages, err := trend.GetLanguages()
	if err != nil {
		log.Fatal(err)
	}
	for index, language := range languages {
		no := index + 1
		fmt.Printf("%d: %s (%s)\n", no, language.Name, language.URLName)
	}
}

Implementations

License

This project is released under the terms of the MIT license.

Documentation

Overview

Package trending provides access to github`s trending repositories and developers. The data will be collected from githubs website at https://github.com/trending and https://github.com/trending/developers.

Construct a new trending client, then use the various functions on the client to access the trending content from GitHub. For example:

trend := trending.NewTrending()

// Get trending projects of language "go" for today.
projects, err := trend.GetProjects(trending.TimeToday, "go")

GitHub Enterprise

If you are running a GitHub Enterprise yourself you can use this library as well. For such cases you can change the BaseURL of the library:

myURL, _ := url.Parse("https://my.github.enterprise.com")
trend := trending.NewTrending()
trend.BaseURL = myURL

// Get trending projects of language "go" for today.
projects, err := trend.GetProjects(trending.TimeToday, "go")

Index

Examples

Constants

View Source
const (
	// TimeToday is limit of the current day.
	TimeToday = "daily"
	// TimeWeek will focus on the complete week
	TimeWeek = "weekly"
	// TimeMonth include the complete month
	TimeMonth = "monthly"
)

These are predefined constants to define the timerange of the requested repository or developer. If trending repositories or developer are requested, a timeframe has to be added. It is suggested to use this constants for this.

Variables

This section is empty.

Functions

This section is empty.

Types

type Developer

type Developer struct {
	// ID is the github`s unique identifier of the user / organisation like 1342004 (google) or 698437 (airbnb).
	ID int

	// // DisplayName is the username of the developer / organisation like "torvalds" or "apache".
	DisplayName string

	// FullName is the real name of the developer / organisation like "Linus Torvalds" (for "torvalds") or "The Apache Software Foundation" (for "apache").
	FullName string

	// URL is the http(s) address of the developer / organisation reflected as url.URL datastructure like https://github.com/torvalds.
	URL *url.URL

	// Avatar is the http(s) address of the developer / organisation avatar as url.URL datastructure like https://avatars1.githubusercontent.com/u/1024025?v=3&s=192.
	Avatar *url.URL
}

Developer reflects a single trending developer / organisation. It provides information as printed on the source website https://github.com/trending/developers.

type Language

type Language struct {
	// Name is the human readable name of the language like "Go" or "Web Ontology Language"
	Name string

	// URLName is the machine readable / usable name of the language used for filtering / url parameters like "go" or "web-ontology-language".
	// Please use URLName if you want to filter your requests.
	URLName string

	// URL is the filter URL for the language like "https://github.com/trending?l=go" for "go" or "https://github.com/trending?l=unknown" or "unknown".
	URL *url.URL
}

Language reflects a single (programing) language offered by github for filtering. If you call "GetProjects" you are able to filter by programing language. For filter input you should use the URLName of Language.

type Project

type Project struct {
	// Name is the name of the repository including user / organisation like "andygrunwald/go-trending" or "airbnb/javascript".
	Name string

	// Owner is the name of the user or organisation. "andygrunwald" in "andygrunwald/go-trending" or "airbnb" in "airbnb/javascript".
	Owner string

	// RepositoryName is the name of therepository. "go-trending" in "andygrunwald/go-trending" or "javascript" in "airbnb/javascript".
	RepositoryName string

	// Description is the description of the repository like "JavaScript Style Guide" (for "airbnb/javascript").
	Description string

	// Language is the determined programing language of the project (by Github).
	// Sometimes Language is an empty string, because Github can`t determine the (main) programing language (like for "google/deepdream").
	Language string

	// Stars is the number of github stars this project received in the given timeframe (see TimeToday / TimeWeek / TimeMonth constants).
	// This number don`t reflect the overall stars of the project.
	Stars int

	// URL is the http(s) address of the project reflected as url.URL datastructure like "https://github.com/Workiva/go-datastructures".
	URL *url.URL

	// ContributorURL is the http(s) address of the contributors page of the project reflected as url.URL datastructure like "https://github.com/Workiva/go-datastructures/graphs/contributors".
	ContributorURL *url.URL

	// Contributor are a collection of Developer.
	// Be aware that this collection don`t covers all contributor.
	// Only those who are mentioned at githubs trending page.
	Contributor []Developer
}

Project reflects a single trending repository. It provides information as printed on the source website https://github.com/trending.

type Trending struct {
	// Base URL for requests.
	// Defaults to the public GitHub website, but can be set to a domain endpoint to use with GitHub Enterprise.
	// BaseURL should always be specified with a trailing slash.
	BaseURL *url.URL

	// Client to use for requests
	Client *http.Client
}

Trending reflects the main datastructure of this package. It doesn`t provide an exported state, but based on this the methods are called. To receive a new instance just add

package main

import (
	"github.com/andygrunwald/go-trending"
)

func main() {
	trend := trending.NewTrending()
	...
}

func NewTrending

func NewTrending() *Trending

NewTrending is the main entry point of the trending package. It provides access to the API of this package by returning a Trending datastructure. Usage:

trend := trending.NewTrending()
projects, err := trend.GetProjects(trending.TimeToday, "")
...

func NewTrendingWithClient

func NewTrendingWithClient(client *http.Client) *Trending

NewTrendingWithClient allows providing a custom http.Client to use for fetching trending items. It allows setting timeouts or using 3rd party http.Client implementations, such as Google App Engine urlfetch.Client.

func (*Trending) GetDevelopers

func (t *Trending) GetDevelopers(time, language string) ([]Developer, error)

GetDevelopers provides a slice of Developer filtered by the given time and language.

time can be filtered by applying by one of the Time* constants (e.g. TimeToday, TimeWeek, ...). If an empty string will be applied TimeToday will be the default (current default by Github).

language can be filtered by applying a programing language by your choice The input must be a known language by Github and be part of GetLanguages(). Further more it must be the Language.URLName and not the human readable Language.Name. If language is an empty string "All languages" will be applied (current default by Github).

Example
client := &http.Client{
	Timeout: 30 * time.Second,
}
trend := trending.NewTrendingWithClient(client)
developers, err := trend.GetDevelopers(trending.TimeToday, "")
if err != nil {
	log.Fatal(err)
}

if len(developers) > 0 {
	fmt.Println("Developers received.")
} else {
	fmt.Printf("Number of developer received: %d", len(developers))
}
Output:

Developers received.

func (*Trending) GetLanguages

func (t *Trending) GetLanguages() ([]Language, error)

GetLanguages will return a slice of Language known by gitub. With the Language.URLName you can filter your GetProjects / GetDevelopers calls.

Example
client := &http.Client{
	Timeout: 30 * time.Second,
}
trend := trending.NewTrendingWithClient(client)
languages, err := trend.GetLanguages()
if err != nil {
	log.Fatal(err)
}

// We need more as 15 languages, because 9 are trending languages
if len(languages) > 15 {
	fmt.Println("Languages received.")
} else {
	fmt.Printf("Number of languages received: %d", len(languages))
}
Output:

Languages received.

func (*Trending) GetProjects

func (t *Trending) GetProjects(time, language string) ([]Project, error)

GetProjects provides a slice of Projects filtered by the given time and language.

time can be filtered by applying by one of the Time* constants (e.g. TimeToday, TimeWeek, ...). If an empty string will be applied TimeToday will be the default (current default by Github).

language can be filtered by applying a programing language by your choice. The input must be a known language by Github and be part of GetLanguages(). Further more it must be the Language.URLName and not the human readable Language.Name. If language is an empty string "All languages" will be applied (current default by Github).

Example
client := &http.Client{
	Timeout: 30 * time.Second,
}
trend := trending.NewTrendingWithClient(client)
projects, err := trend.GetProjects(trending.TimeToday, "go")
if err != nil {
	log.Fatal(err)
}

onlyGoProjects := true
for _, project := range projects {
	if len(project.Language) > 0 && project.Language != "Go" {
		onlyGoProjects = false
	}
}

if len(projects) > 0 && onlyGoProjects == true {
	fmt.Println("Projects (filtered by Go) received.")
} else {
	fmt.Printf("Number of projectes received: %d (filtered by golang %v)", len(projects), onlyGoProjects)
}
Output:

Projects (filtered by Go) received.

func (*Trending) GetTrendingLanguages

func (t *Trending) GetTrendingLanguages() ([]Language, error)

GetTrendingLanguages will return a slice of Language that are currently trending. Trending languages are displayed at https://github.com/trending on the right side. With the Language.URLName you can filter your GetProjects / GetDevelopers calls.

Example
client := &http.Client{
	Timeout: 30 * time.Second,
}
trend := trending.NewTrendingWithClient(client)
languages, err := trend.GetTrendingLanguages()
if err != nil {
	log.Fatal(err)
}

if len(languages) > 0 {
	fmt.Println("Trending Languages received.")
} else {
	fmt.Printf("Number of languages received: %d", len(languages))
}
Output:

Trending Languages received.

Source Files

Jump to

Keyboard shortcuts

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