newsapi

package
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Jul 9, 2023 License: MIT Imports: 14 Imported by: 0

Documentation

Index

Constants

View Source
const (
	LanguageEnglish            = "en"
	LanguageIndonesian         = "id"
	LanguageCzech              = "cs"
	LanguageGerman             = "de"
	LanguageSpanish            = "es-419"
	LanguageFrench             = "fr"
	LanguageItalian            = "it"
	LanguageLatvian            = "lv"
	LanguageLithuanian         = "lt"
	LanguageHungarian          = "hu"
	LanguageDutch              = "nl"
	LanguageNorwegian          = "no"
	LanguagePolish             = "pl"
	LanguagePortugueseBrasil   = "pt-419"
	LanguagePortuguesePortugal = "pt-150"
	LanguageRomanian           = "ro"
	LanguageSlovak             = "sk"
	LanguageSlovenian          = "sl"
	LanguageSwedish            = "sv"
	LanguageVietnamese         = "vi"
	LanguageTurkish            = "tr"
	LanguageGreek              = "el"
	LanguageBulgarian          = "bg"
	LanguageRussian            = "ru"
	LanguageSerbian            = "sr"
	LanguageUkrainian          = "uk"
	LanguageHebrew             = "he"
	LanguageArabic             = "ar"
	LanguageMarathi            = "mr"
	LanguageHindi              = "hi"
	LanguageBengali            = "bn"
	LanguageTamil              = "ta"
	LanguageTelugu             = "te"
	LanguageMalyalam           = "ml"
	LanguageThai               = "th"
	LanguageChineseSimplified  = "zh-Hans"
	LanguageChineseTraditional = "zh-Hant"
	LanguageJapanese           = "ja"
	LanguageKorean             = "ko"
)
View Source
const (
	// Africa
	LocationBotswana    = "BW"
	LocationEthiopia    = "ET"
	LocationGhana       = "GH"
	LocationKenya       = "KE"
	LocationNamibia     = "NA"
	LocationNigeria     = "NG"
	LocationSouthAfrica = "ZA"
	LocationTanzania    = "TZ"
	LocationUganda      = "UG"
	LocationZimbabwe    = "ZW"

	// Asia
	LocationIndia           = "IN"
	LocationIndonesia       = "ID"
	LocationIsrael          = "IL"
	LocationMalaysia        = "MY"
	LocationPakistan        = "PK"
	LocationPhilippines     = "PH"
	LocationSingapore       = "SG"
	LocationVietnam         = "VN"
	LocationTurkey          = "TR"
	LocationBangladesh      = "BD"
	LocationThailand        = "TH"
	LocationChina           = "CN"
	LocationTaiwan          = "TW"
	LocationHongKong        = "HK"
	LocationJapan           = "JP"
	LocationRepublicOfKorea = "KR"

	// Europe
	LocationAustralia     = "AU"
	LocationIreland       = "IE"
	LocationLatvia        = "LV"
	LocationUnitedKingdom = "GB"
	LocationCzechRepublic = "CZ"
	LocationGermany       = "DE"
	LocationAustria       = "AT"
	LocationSwitzerland   = "CH"
	LocationBelgium       = "BE"
	LocationFrance        = "FR"
	LocationItaly         = "IT"
	LocationLithuania     = "LT"
	LocationHungary       = "HU"
	LocationNetherlands   = "NL"
	LocationNorway        = "NO"
	LocationPoland        = "PL"
	LocationPortugal      = "PT"
	LocationRomania       = "RO"
	LocationSlovakia      = "SK"
	LocationSlovenia      = "SI"
	LocationSweden        = "SE"
	LocationGreece        = "GR"
	LocationBulgaria      = "BG"
	LocationRussia        = "RU"
	LocationUkraine       = "UA"
	LocationSerbia        = "RS"

	// North America
	LocationCanada       = "CA"
	LocationMexico       = "MX"
	LocationUnitedStates = "US"

	// South America
	LocationArgentina = "AR"
	LocationChile     = "CL"
	LocationColombia  = "CO"
	LocationCuba      = "CU"
	LocationPeru      = "PE"
	LocationVenezuela = "VE"

	// Middle East
	LocationUnitedArabEmirates = "AE"
	LocationSaudiArabia        = "SA"
	LocationLebanon            = "LB"
	LocationEgypt              = "EG"
)

Geographic Location

View Source
const (
	// Topic
	TopicWorld         string = "WORLD"
	TopicNation        string = "NATION"
	TopicBusiness      string = "BUSINESS"
	TopicTechnology    string = "TECHNOLOGY"
	TopicEntertainment string = "ENTERTAINMENT"
	TopicSports        string = "SPORTS"
	TopicScience       string = "SCIENCE"
	TopicHealth        string = "HEALTH"
)
View Source
const (
	MaxSearchResults = 100
)

Variables

View Source
var (
	ErrEmptyQuery = errors.New("query cannot be empty")

	ErrEmptyTopic = errors.New("topic cannot be empty")

	ErrInvalidTopic = errors.New("invalid topic")

	ErrEmptyLocation = errors.New("location cannot be empty")

	ErrFailedToGetNewsContent = errors.New("failed to get news content")

	ErrEmptyLink = errors.New("link cannot be empty")
)
View Source
var (
	USER_AGENTS = []string{}/* 184 elements not displayed */

	RandomUserAgent = func() string {
		return USER_AGENTS[rand.Intn(len(USER_AGENTS))]
	}
)
View Source
var (
	GOOGLE_NEWS_REGEX = `^http(s)?://(www.)?news.google.com*`
)
View Source
var (
	TopicMap = map[string]string{
		TopicWorld:         "w",
		TopicNation:        "n",
		TopicBusiness:      "b",
		TopicTechnology:    "t",
		TopicEntertainment: "e",
		TopicSports:        "s",
		TopicScience:       "snc",
		TopicHealth:        "m",
	}
)

Functions

func CleanHTML

func CleanHTML(content string) string

CleanHTML cleans html elements from content

func FetchNewsContent

func FetchNewsContent(link string) (string, error)

FetchNewsContent fetches the content of the news

func FormatDuration

func FormatDuration(duration time.Duration) string

func GetFeedItems

func GetFeedItems(client *http.Client, req *http.Request) ([]*gofeed.Item, error)

GetFeedItems gets feed items from a request

func GetOriginalLink(sourceLink string) (string, error)

GetOriginalLink gets the original link

func IsNewsApiLink(link string) bool

IsNewsApiLink checks if a link is a news api link

func NewNewsApi

func NewNewsApi(options ...NewsApiOption) *newsApi
func ToSourceLinks(newsList []*News)

ToSourceLinks converts the google news links to original links

Types

type News

type News struct {
	Title           string
	Description     string
	Link            string
	Links           []string
	Content         string
	Published       string
	PublishedParsed *time.Time
	Updated         string
	UpdatedParsed   *time.Time
	GUID            string
	ImageURL        string
	Categories      []string
}

type NewsApi

type NewsApi interface {
	GetTopNews() ([]*News, error)
	GetTopicNews(topic string) ([]*News, error)
	GetLocationNews(location string) ([]*News, error)
	SearchNews(query string) ([]*News, error)

	SetQueryOptions(options ...QueryOption)
}

type NewsApiOption

type NewsApiOption func(*newsApi)

func WithProxy

func WithProxy(proxy *url.URL) NewsApiOption

func WithoutProxy

func WithoutProxy() NewsApiOption

type QueryOption

type QueryOption func(*newsApi)

func WithEndDate

func WithEndDate(endDate time.Time) QueryOption

func WithLanguage

func WithLanguage(language string) QueryOption

func WithLimit

func WithLimit(limit int) QueryOption

func WithLocation

func WithLocation(location string) QueryOption

func WithPeriod

func WithPeriod(period time.Duration) QueryOption

func WithStartDate

func WithStartDate(startDate time.Time) QueryOption

func WithoutDuration

func WithoutDuration() QueryOption

func WithoutEndDate

func WithoutEndDate() QueryOption

func WithoutPeriod

func WithoutPeriod() QueryOption

func WithoutStartDate

func WithoutStartDate() QueryOption

Jump to

Keyboard shortcuts

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