项目作者: leoafarias

项目描述 :
A NewsAPI client for Go
高级语言: Go
项目地址: git://github.com/leoafarias/go-newsapi.git
创建时间: 2018-04-19T03:37:36Z
项目社区:https://github.com/leoafarias/go-newsapi

开源协议:MIT License

下载


NewsAPI Go Client

Go Report Card MIT Licence
stability-unstable
Maintainability

A Go client for NewsAPI v2

Installation

  1. $ go get github.com/leoafarias/go-newsapi

Usage

This library is a GO client you can use to interact with the NewsAPI v2. Here are some examples

TopHeadlines

This method provides live top and breaking headlines for a country, specific category in a country, single source, or multiple sources. You can also search with keywords. Articles are sorted by the earliest date published first.

  1. package main
  2. import (
  3. "fmt"
  4. "log"
  5. "github.com/leoafarias/go-newsapi"
  6. )
  7. const apikey = "API_KEY"
  8. func main() {
  9. c, _ := newsapi.NewClient(apikey)
  10. params := make(map[string]string)
  11. params["country"] = "us"
  12. params["category"] = "technology"
  13. res, err := c.TopHeadlines(params)
  14. if err != nil {
  15. log.Fatal(err)
  16. os.Exit(1)
  17. }
  18. for _, a := range res.Articles {
  19. fmt.Println(a)
  20. }
  21. }

Everything

Search through millions of articles from over 30,000 large and small news sources and blogs. This includes breaking news as well as lesser articles.

  1. package main
  2. import (
  3. "fmt"
  4. "log"
  5. "github.com/leoafarias/go-newsapi"
  6. )
  7. const apikey = "API_KEY"
  8. func main() {
  9. c, _ := newsapi.NewClient(apikey)
  10. params := make(map[string]string)
  11. params["q"] = "(ethereum OR litecoin OR bitcoin)"
  12. params["domains"] = "bbc.co.uk, techcrunch.com, engadget.com"
  13. params["pageSize"] = "10"
  14. res, err := c.Everything(params)
  15. if err != nil {
  16. log.Fatal(err)
  17. os.Exit(1)
  18. }
  19. for _, a := range res.Articles {
  20. fmt.Println(a)
  21. }
  22. }

Sources

This method returns the subset of news publishers that top headlines (TopHeadlines) are available from. It’s mainly a convenience endpoint that you can use to keep track of the publishers available on the API, and you can pipe it straight through to your users.

  1. package main
  2. import (
  3. "fmt"
  4. "log"
  5. "github.com/leoafarias/go-newsapi"
  6. )
  7. const apikey = "API_KEY"
  8. func main() {
  9. c, _ := newsapi.NewClient(apikey)
  10. params := make(map[string]string)
  11. params["category"] = "technology"
  12. res, err := c.Sources(params)
  13. if err != nil {
  14. log.Fatal(err)
  15. os.Exit(1)
  16. }
  17. for _, a := range res.Sources {
  18. fmt.Println(a)
  19. }
  20. }

To Do

  • Implement TopHeadlines
  • Implement Everything
  • Implement Source
  • Write tests
  • Implement Cancelable requests