newsapi

package
v0.0.2 Latest Latest
Warning

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

Go to latest
Published: Aug 5, 2023 License: MIT Imports: 17 Imported by: 2

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")

	ErrEmptyLink = errors.New("link cannot be empty")

	ErrNoSourceLink = errors.New("no source link")

	ErrFailedToGetNewsContent = errors.New("failed to get news content")
)
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 deprecated

func FetchNewsContent(link string) (string, error)

Deprecated: use FetchSourceContents instead

func FetchSourceContents added in v0.0.2

func FetchSourceContents(newsList []*News)

FetchSourceContents fetches the source contents by the source links

func FetchSourceLinks(newsList []*News)

FetchSourceLinks fetches the source links by the google news links

func FormatDuration added in v0.0.1

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)

Deprecated: use FetchSourceLinks instead

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

	SourceLink        string
	SourceTitle       string
	SourceImageURL    string
	SourceImageWidth  int
	SourceImageHeight int
	SourceDescription string
	SourceKeywords    []string
	SourceSiteName    string
	SourceContent     string
}

func NewNews added in v0.0.2

func NewNews(item *gofeed.Item) *News

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