Module: GoogleNews

Defined in:
lib/google_news.rb,
lib/google_news/version.rb

Defined Under Namespace

Classes: InvalidTopicError

Constant Summary collapse

HEADLINES_RSS =
'https://news.google.com/news/rss'.freeze
TOPICS_RSS =
'https://news.google.com/news/rss/headlines/section/topic/'.freeze
GEO_RSS =
'https://news.google.com/news/rss/headlines/section/geo/'.freeze
SEARCH_RSS =
'https://news.google.com/rss/search?q='.freeze
TOPICS =
%w[WORLD NATION BUSINESS TECHNOLOGY ENTERTAINMENT SPORTS SCIENCE HEALTH].freeze
VERSION =
'0.0.1'.freeze

Class Method Summary collapse

Class Method Details

.geo(position, country: 'us', language: 'en', n: 10) ⇒ Array<Hash>

Get top headlines for a specific geographic location from Google News

Note: Google may not support all locations.

Parameters:

  • position (String)

    e.g. “48.8566,2.3522” for Paris

  • country (String) (defaults to: 'us')
  • language (String) (defaults to: 'en')
  • n (Integer) (defaults to: 10)

Returns:

  • (Array<Hash>)

See Also:



59
60
61
62
63
# File 'lib/google_news.rb', line 59

def self.geo(position, country: 'us', language: 'en', n: 10)
  encoded = URI.encode_www_form_component(position.to_s)
  url = GEO_RSS + encoded + '?' + fill_country_lang_params(country, language)
  limited_items(url, n)
end

.headlines(country: 'us', language: 'en', n: 10) ⇒ Array<Hash>

Get top headlines from Google News

Parameters:

  • country (String) (defaults to: 'us')
  • language (String) (defaults to: 'en')
  • n (Integer) (defaults to: 10)

Returns:

  • (Array<Hash>)


26
27
28
29
# File 'lib/google_news.rb', line 26

def self.headlines(country: 'us', language: 'en', n: 10)
  url = HEADLINES_RSS + '?' + fill_country_lang_params(country, language)
  limited_items(url, n)
end

.search(query, country: 'us', language: 'en', n: 10) ⇒ Array<Hash>

Search Google News for a query

Note: This is not the same as a web search. Only news articles are returned.

For a web search, consider using the Google Custom Search API.

Parameters:

  • query (String)
  • country (String) (defaults to: 'us')
  • language (String) (defaults to: 'en')
  • n (Integer) (defaults to: 10)

Returns:

  • (Array<Hash>)

See Also:



80
81
82
83
84
# File 'lib/google_news.rb', line 80

def self.search(query, country: 'us', language: 'en', n: 10)
  encoded = URI.encode_www_form_component(query.to_s)
  url = SEARCH_RSS + encoded + '&' + fill_country_lang_params(country, language)
  limited_items(url, n)
end

.topic(topic_name, country: 'us', language: 'en', n: 10) ⇒ Array<Hash>

Get top headlines for a specific topic from Google News

Parameters:

  • topic_name (String, Symbol)

    One of GoogleNews::TOPICS

  • country (String) (defaults to: 'us')
  • language (String) (defaults to: 'en')
  • n (Integer) (defaults to: 10)

Returns:

  • (Array<Hash>)

Raises:



41
42
43
44
45
46
# File 'lib/google_news.rb', line 41

def self.topic(topic_name, country: 'us', language: 'en', n: 10)
  topic_name = topic_name.to_s.upcase
  raise InvalidTopicError, 'Invalid topic name. See GoogleNews::TOPICS.' unless TOPICS.include?(topic_name)
  url = TOPICS_RSS + topic_name + '?' + fill_country_lang_params(country, language)
  limited_items(url, n)
end

.website(query, country: 'us', language: 'en', n: 10) ⇒ Array<Hash>

Search Google News for articles from a specific website

Note: This is not the same as a web search. Only news articles are returned.

For a web search, consider using the Google Custom Search API.

Note: The parameter logic mimics the (possibly flawed) JavaScript version.

It may not yield expected results for all websites.

Examples:


GoogleNews.website("example.com", country: "us", language: "en", n: 5)

Returns up to 5 news articles from "example.com" in English for the US region.

Parameters:

  • query (String)

    e.g. “example.com”

  • country (String) (defaults to: 'us')
  • language (String) (defaults to: 'en')
  • n (Integer) (defaults to: 10)

Returns:

  • (Array<Hash>)

See Also:



111
112
113
114
115
116
# File 'lib/google_news.rb', line 111

def self.website(query, country: 'us', language: 'en', n: 10)
  # Repliziert die (möglicherweise fehlerhafte) Param-Logik aus JS: "site%3A..." + fillWebsiteParams
  encoded = 'site%3A' + URI.encode_www_form_component(query.to_s)
  url = SEARCH_RSS + encoded + '&' + fill_website_params(country, language)
  limited_items(url, n)
end