Giter VIP home page Giter VIP logo

newsapi-go's Introduction

Google News API

Go Report Card

The News API is a Go package that allows you to fetch news articles from Google News. It provides a simple and convenient way to retrieve news based on various criteria such as language, location, topic, and search query.

Installation

To use the News API package in your Go project, you can install it using the go get command:

go get github.com/Zhima-Mochi/newsApi-go/newsapi

Usage

Import the News API package in your Go code:

import "github.com/Zhima-Mochi/newsApi-go/newsapi"

Creating a News API instance

You can create a new instance of the News API by calling the NewNewsApi function. You can also provide optional configuration options to customize the behavior of the API.

api := newsapi.NewNewsApi()

Fetching top news

To retrieve the top news articles, you can use the GetTopNews method:

newsList, err := api.GetTopNews()
if err != nil {
    // handle error
}

// Process the news articles
for _, news := range newsList {
    // Access news properties such as title, description, link, etc.
    fmt.Println(news.Title)
}

Fetching news by location

You can retrieve news articles based on a specific location using the GetLocationNews method:

newsList, err := api.GetLocationNews(newsapi.LocationUnitedStates)
if err != nil {
    // handle error
}

// Process the news articles
for _, news := range newsList {
    // Access news properties
    fmt.Println(news.Title)
}

Fetching news by topic

To fetch news articles related to a specific topic, you can use the GetTopicNews method:

newsList, err := api.GetTopicNews(newsapi.TopicTechnology)
if err != nil {
    // handle error
}

// Process the news articles
for _, news := range newsList {
    // Access news properties
    fmt.Println(news.Title)
}

Searching for news

You can search for news articles using a specific query using the SearchNews method:

newsList, err := api.SearchNews("Go programming language")
if err != nil {
    // handle error
}

// Process the news articles
for _, news := range newsList {
    // Access news properties
    fmt.Println(news.Title)
}

Customizing the API options

The News API provides various options to customize the behavior of the API. You can set the query options using the SetQueryOptions method:

api.SetQueryOptions(
    newsapi.WithLanguage("en"),
    newsapi.WithLocation("US"),
    newsapi.WithLimit(20),
)

// Fetch news based on the configured options
newsList, err := api.GetTopNews()
// ...

Fetching content of a news article

newsContent, err := newsapi.FetchNewsContent(news.Link)
if err != nil {
    // handle error
}

// Access news content
fmt.Println(newsContent)

Example

Please refer to the example for a complete example of using the News API package.

Todo

  • FetchNewsContent() is not working properly for some news's website.
  • Implement FetchAllNewsContent(newsList []*News) with goroutine.

License

The News API package is open source and available under the MIT License.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.