google_news 0.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +7 -0
- data/.gitattributes +1 -0
- data/.gitignore +15 -0
- data/.rspec +2 -0
- data/CODE_OF_CONDUCT.md +74 -0
- data/Gemfile +4 -0
- data/LICENSE +21 -0
- data/README.md +119 -0
- data/Rakefile +17 -0
- data/bin/console +14 -0
- data/bin/google_news +31 -0
- data/bin/google_news.bat +2 -0
- data/bin/setup +8 -0
- data/google_news.gemspec +39 -0
- data/lib/custom_errors/invalid_key_type_error.rb +2 -0
- data/lib/google_news/version.rb +3 -0
- data/lib/google_news.rb +234 -0
- metadata +141 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: df4beeb3e71a539fd40e36aff03536fd8239976510093a0af1f7c8cbc91df797
|
4
|
+
data.tar.gz: dbc67f83eed942876a9ac46e7763de6d19dbebdd135fe6d9ca09867ea0bc2282
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 68e12a403a5a857760ae821d4b8a6d9532b4825fbf7302a0cdfa40753945730ead7d0788bb459e5f67aa716c83be85d48bbf98e15fb53b434c1556e29f26676f
|
7
|
+
data.tar.gz: 695d594b22921aabc3ca570407c34bd9d47d227c188f39ad001b3c5d12f3ff1a6c515fdf9d87b5a3c3a1646ada27e5e76101159b654da89721d58a771e93c048
|
data/.gitattributes
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
* text=auto eol=lf
|
data/.gitignore
ADDED
data/.rspec
ADDED
data/CODE_OF_CONDUCT.md
ADDED
@@ -0,0 +1,74 @@
|
|
1
|
+
# Contributor Covenant Code of Conduct
|
2
|
+
|
3
|
+
## Our Pledge
|
4
|
+
|
5
|
+
In the interest of fostering an open and welcoming environment, we as
|
6
|
+
contributors and maintainers pledge to making participation in our project and
|
7
|
+
our community a harassment-free experience for everyone, regardless of age, body
|
8
|
+
size, disability, ethnicity, gender identity and expression, level of experience,
|
9
|
+
nationality, personal appearance, race, religion, or sexual identity and
|
10
|
+
orientation.
|
11
|
+
|
12
|
+
## Our Standards
|
13
|
+
|
14
|
+
Examples of behavior that contributes to creating a positive environment
|
15
|
+
include:
|
16
|
+
|
17
|
+
* Using welcoming and inclusive language
|
18
|
+
* Being respectful of differing viewpoints and experiences
|
19
|
+
* Gracefully accepting constructive criticism
|
20
|
+
* Focusing on what is best for the community
|
21
|
+
* Showing empathy towards other community members
|
22
|
+
|
23
|
+
Examples of unacceptable behavior by participants include:
|
24
|
+
|
25
|
+
* The use of sexualized language or imagery and unwelcome sexual attention or
|
26
|
+
advances
|
27
|
+
* Trolling, insulting/derogatory comments, and personal or political attacks
|
28
|
+
* Public or private harassment
|
29
|
+
* Publishing others' private information, such as a physical or electronic
|
30
|
+
address, without explicit permission
|
31
|
+
* Other conduct which could reasonably be considered inappropriate in a
|
32
|
+
professional setting
|
33
|
+
|
34
|
+
## Our Responsibilities
|
35
|
+
|
36
|
+
Project maintainers are responsible for clarifying the standards of acceptable
|
37
|
+
behavior and are expected to take appropriate and fair corrective action in
|
38
|
+
response to any instances of unacceptable behavior.
|
39
|
+
|
40
|
+
Project maintainers have the right and responsibility to remove, edit, or
|
41
|
+
reject comments, commits, code, wiki edits, issues, and other contributions
|
42
|
+
that are not aligned to this Code of Conduct, or to ban temporarily or
|
43
|
+
permanently any contributor for other behaviors that they deem inappropriate,
|
44
|
+
threatening, offensive, or harmful.
|
45
|
+
|
46
|
+
## Scope
|
47
|
+
|
48
|
+
This Code of Conduct applies both within project spaces and in public spaces
|
49
|
+
when an individual is representing the project or its community. Examples of
|
50
|
+
representing a project or community include using an official project e-mail
|
51
|
+
address, posting via an official social media account, or acting as an appointed
|
52
|
+
representative at an online or offline event. Representation of a project may be
|
53
|
+
further defined and clarified by project maintainers.
|
54
|
+
|
55
|
+
## Enforcement
|
56
|
+
|
57
|
+
Instances of abusive, harassing, or otherwise unacceptable behavior may be
|
58
|
+
reported by contacting the project team at google_news.gemspec@mail.magynhard.de. All
|
59
|
+
complaints will be reviewed and investigated and will result in a response that
|
60
|
+
is deemed necessary and appropriate to the circumstances. The project team is
|
61
|
+
obligated to maintain confidentiality with regard to the reporter of an incident.
|
62
|
+
Further details of specific enforcement policies may be posted separately.
|
63
|
+
|
64
|
+
Project maintainers who do not follow or enforce the Code of Conduct in good
|
65
|
+
faith may face temporary or permanent repercussions as determined by other
|
66
|
+
members of the project's leadership.
|
67
|
+
|
68
|
+
## Attribution
|
69
|
+
|
70
|
+
This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 1.4,
|
71
|
+
available at [http://contributor-covenant.org/version/1/4][version]
|
72
|
+
|
73
|
+
[homepage]: http://contributor-covenant.org
|
74
|
+
[version]: http://contributor-covenant.org/version/1/4/
|
data/Gemfile
ADDED
data/LICENSE
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
MIT License
|
2
|
+
|
3
|
+
Copyright (c) 2025 Matthäus J. N. Beyrle
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
7
|
+
in the Software without restriction, including without limitation the rights
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
10
|
+
furnished to do so, subject to the following conditions:
|
11
|
+
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
13
|
+
copies or substantial portions of the Software.
|
14
|
+
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
21
|
+
SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,119 @@
|
|
1
|
+
# google_news
|
2
|
+
[](https://rubygems.org/gems/google_news)
|
3
|
+

|
4
|
+
[](LICENSE)
|
5
|
+
|
6
|
+
> Unofficial Ruby gem to get Google News RSS feeds easily from Ruby or the command line.
|
7
|
+
|
8
|
+
Inspired by [google-news-js](https://github.com/DatanewsOrg/google-news-js).
|
9
|
+
|
10
|
+
> [!WARNING]
|
11
|
+
> Early alpha release. Use with caution.
|
12
|
+
>
|
13
|
+
> TODOs:
|
14
|
+
> - CLI
|
15
|
+
> - Support at all
|
16
|
+
> - Support for different output formats (json, text, ...)
|
17
|
+
> - More tests
|
18
|
+
> - More documentation
|
19
|
+
|
20
|
+
### Contents
|
21
|
+
* [Common information](#common-information)
|
22
|
+
* [Installation](#installation)
|
23
|
+
* [Usage](#usage)
|
24
|
+
* [Command line](#command-line-usage)
|
25
|
+
* [Documentation](#documentation)
|
26
|
+
* [Contributing](#contributing)
|
27
|
+
|
28
|
+
|
29
|
+
|
30
|
+
<a name="common-information"></a>
|
31
|
+
## Common information
|
32
|
+
|
33
|
+
This is just a simple RSS feed parser for Google News RSS feeds.
|
34
|
+
|
35
|
+
Can also be used from the [command line](#command-line-usage)!
|
36
|
+
|
37
|
+
There is support for:
|
38
|
+
* Top stories
|
39
|
+
* News by topic
|
40
|
+
* News by location
|
41
|
+
* News by keyword
|
42
|
+
* News by source
|
43
|
+
* News by language
|
44
|
+
|
45
|
+
|
46
|
+
<a name="installation"></a>
|
47
|
+
## Installation
|
48
|
+
### Ruby
|
49
|
+
Add this line to your application's Gemfile:
|
50
|
+
|
51
|
+
```ruby
|
52
|
+
gem 'google_news'
|
53
|
+
```
|
54
|
+
|
55
|
+
And then execute:
|
56
|
+
|
57
|
+
bundle install
|
58
|
+
|
59
|
+
### Command line
|
60
|
+
If you just want to use the command line then run
|
61
|
+
|
62
|
+
gem install google_news
|
63
|
+
|
64
|
+
|
65
|
+
|
66
|
+
|
67
|
+
|
68
|
+
<a name="usage"></a>
|
69
|
+
## Usage
|
70
|
+
|
71
|
+
```ruby
|
72
|
+
require 'google_news'
|
73
|
+
|
74
|
+
results = GoogleNews.headlines n: 5 # get top 5 news in default language (country: us, language: en)
|
75
|
+
puts results
|
76
|
+
# => [{"title"=>"Title of the news article", "link"=>"https://link.to/the/article", "pubDate"=>"Wed, 01 Jan 2024 00:00:00 GMT", "source"=>"Source Name"}, ...]
|
77
|
+
```
|
78
|
+
|
79
|
+
|
80
|
+
|
81
|
+
<a name="command-line-usage"></a>
|
82
|
+
## Command line
|
83
|
+
|
84
|
+
`google_news` is also available on the command line after installation.
|
85
|
+
|
86
|
+
The results are printed in JSON format by default.
|
87
|
+
|
88
|
+
|
89
|
+
|
90
|
+
```bash
|
91
|
+
$ google_news --help
|
92
|
+
Usage: google_news [options]
|
93
|
+
-n, --number NUMBER Number of news articles to fetch (default: 10)
|
94
|
+
-c, --country COUNTRY Country code (default: 'us')
|
95
|
+
-l, --language LANGUAGE Language code (default: 'en')
|
96
|
+
-t, --topic TOPIC News topic (e.g., 'world', 'business', 'technology', etc.)
|
97
|
+
-s, --source SOURCE News source (e.g., 'bbc-news', 'cnn', etc.)
|
98
|
+
-k, --keyword KEYWORD Keyword to search for in news articles
|
99
|
+
-f, --format FORMAT Output format: 'json' (default) or 'text'
|
100
|
+
-h, --help Show this help message
|
101
|
+
```
|
102
|
+
|
103
|
+
### Examples
|
104
|
+
|
105
|
+
Fetch top 5 news articles in the default language (country: us, language: en):
|
106
|
+
```bash
|
107
|
+
$ google_news -n 5
|
108
|
+
```
|
109
|
+
|
110
|
+
<a name="documentation"></a>
|
111
|
+
## Documentation
|
112
|
+
Detailed (code) documentation is available at [https://www.rubydoc.info/gems/google_news](https://www.rubydoc.info/gems/google_news).
|
113
|
+
|
114
|
+
|
115
|
+
|
116
|
+
<a name="contributing"></a>
|
117
|
+
## Contributing
|
118
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/magynhard/google_news. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](http://contributor-covenant.org) code of conduct.
|
119
|
+
|
data/Rakefile
ADDED
@@ -0,0 +1,17 @@
|
|
1
|
+
require 'bundler/gem_tasks'
|
2
|
+
require 'rspec/core/rake_task'
|
3
|
+
|
4
|
+
#
|
5
|
+
# run default task to see tasks to build and publish gem
|
6
|
+
#
|
7
|
+
task :default do
|
8
|
+
system 'rake --tasks'
|
9
|
+
end
|
10
|
+
|
11
|
+
task :console do
|
12
|
+
require_relative '../lib/google_news'
|
13
|
+
require 'irb'
|
14
|
+
IRB.start(__FILE__)
|
15
|
+
end
|
16
|
+
|
17
|
+
RSpec::Core::RakeTask.new(:spec)
|
data/bin/console
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
require 'bundler/setup'
|
4
|
+
require_relative '../lib/google_news'
|
5
|
+
|
6
|
+
# You can add fixtures and/or initialization code here to make experimenting
|
7
|
+
# with your gem easier. You can also use a different console, if you like.
|
8
|
+
|
9
|
+
# (If you use this, don't forget to add pry to your Gemfile!)
|
10
|
+
# require 'pry'
|
11
|
+
# Pry.start
|
12
|
+
|
13
|
+
require 'irb'
|
14
|
+
IRB.start(__FILE__)
|
data/bin/google_news
ADDED
@@ -0,0 +1,31 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
require_relative '../lib/google_news'
|
4
|
+
|
5
|
+
arg = ARGV[0]
|
6
|
+
|
7
|
+
if %w[help -h --help].include?(arg) || arg.nil? || arg == ''
|
8
|
+
puts <<-TEXT
|
9
|
+
|
10
|
+
google_news #{GoogleNews::VERSION}
|
11
|
+
https://github.com/magynhard/google_news
|
12
|
+
|
13
|
+
Usage:
|
14
|
+
google_news [options]
|
15
|
+
Options:
|
16
|
+
-h, --help Show this help message
|
17
|
+
-v, --version Show version
|
18
|
+
-n, --number N Number of headlines to fetch (default: 10)
|
19
|
+
-c, --country CC Country code (default: "us")
|
20
|
+
-l, --language LL Language code (default: "en")
|
21
|
+
--headlines Fetch top headlines
|
22
|
+
--topic TOPIC Fetch headlines for a specific topic (WORLD, NATION, BUSINESS, TECHNOLOGY, ENTERTAINMENT, SPORTS, SCIENCE, HEALTH)
|
23
|
+
--geo LOCATION Fetch headlines for a specific geographic location (e.g. "48.8566,2.3522" for Paris)
|
24
|
+
--search QUERY Search headlines for a specific query
|
25
|
+
--website SITE Fetch headlines from a specific website (e.g. "bbc.com")
|
26
|
+
|
27
|
+
TEXT
|
28
|
+
elsif %w[]
|
29
|
+
else
|
30
|
+
puts "Invalid command or option. Use --help for usage information."
|
31
|
+
end
|
data/bin/google_news.bat
ADDED
data/bin/setup
ADDED
data/google_news.gemspec
ADDED
@@ -0,0 +1,39 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'google_news/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "google_news"
|
8
|
+
spec.version = GoogleNews::VERSION
|
9
|
+
spec.executables = %w[google_news]
|
10
|
+
spec.authors = ["Matthäus J. N. Beyrle"]
|
11
|
+
spec.email = ["google_news.gemspec@mail.magynhard.de"]
|
12
|
+
|
13
|
+
spec.summary = %q{Get Google News headlines from Ruby or the command line.}
|
14
|
+
spec.homepage = "https://github.com/magynhard/google_news"
|
15
|
+
spec.license = "MIT"
|
16
|
+
|
17
|
+
# Prevent pushing this gem to RubyGems.org. To allow pushes either set the 'allowed_push_host'
|
18
|
+
# to allow pushing to a single host or delete this section to allow pushing to any host.
|
19
|
+
if spec.respond_to?(:metadata)
|
20
|
+
spec.metadata['allowed_push_host'] = "https://rubygems.org"
|
21
|
+
else
|
22
|
+
raise "RubyGems 2.0 or newer is required to protect against " \
|
23
|
+
"public gem pushes."
|
24
|
+
end
|
25
|
+
|
26
|
+
spec.files = `git ls-files -z`.split("\x0").reject do |f|
|
27
|
+
f.match(%r{^(test|spec|features)/})
|
28
|
+
end
|
29
|
+
|
30
|
+
spec.require_paths = ['lib']
|
31
|
+
|
32
|
+
spec.add_runtime_dependency 'rss', '>= 0.2.6'
|
33
|
+
spec.add_runtime_dependency 'open-uri', '>= 0.1.0'
|
34
|
+
|
35
|
+
spec.add_development_dependency 'bundler', '>= 2.0'
|
36
|
+
spec.add_development_dependency 'rake', '>= 10.0'
|
37
|
+
spec.add_development_dependency 'rspec', '>= 3.0'
|
38
|
+
spec.add_development_dependency 'pry', '>= 0.10.0'
|
39
|
+
end
|
data/lib/google_news.rb
ADDED
@@ -0,0 +1,234 @@
|
|
1
|
+
#
|
2
|
+
# Google News RSS Feed Parser
|
3
|
+
#
|
4
|
+
|
5
|
+
require 'open-uri'
|
6
|
+
require 'rss'
|
7
|
+
require 'uri'
|
8
|
+
|
9
|
+
module GoogleNews
|
10
|
+
HEADLINES_RSS = 'https://news.google.com/news/rss'.freeze
|
11
|
+
TOPICS_RSS = 'https://news.google.com/news/rss/headlines/section/topic/'.freeze
|
12
|
+
GEO_RSS = 'https://news.google.com/news/rss/headlines/section/geo/'.freeze
|
13
|
+
SEARCH_RSS = 'https://news.google.com/rss/search?q='.freeze
|
14
|
+
|
15
|
+
TOPICS = %w[WORLD NATION BUSINESS TECHNOLOGY ENTERTAINMENT SPORTS SCIENCE HEALTH].freeze
|
16
|
+
|
17
|
+
class InvalidTopicError < StandardError; end
|
18
|
+
|
19
|
+
#
|
20
|
+
# Get top headlines from Google News
|
21
|
+
#
|
22
|
+
# @param [String] country
|
23
|
+
# @param [String] language
|
24
|
+
# @param [Integer] n
|
25
|
+
# @return [Array<Hash>]
|
26
|
+
def self.headlines(country: 'us', language: 'en', n: 10)
|
27
|
+
url = HEADLINES_RSS + '?' + fill_country_lang_params(country, language)
|
28
|
+
limited_items(url, n)
|
29
|
+
end
|
30
|
+
|
31
|
+
#
|
32
|
+
# Get top headlines for a specific topic from Google News
|
33
|
+
#
|
34
|
+
# @param [String, Symbol] topic_name One of GoogleNews::TOPICS
|
35
|
+
# @param [String] country
|
36
|
+
# @param [String] language
|
37
|
+
# @param [Integer] n
|
38
|
+
# @return [Array<Hash>]
|
39
|
+
#
|
40
|
+
# @raise [InvalidTopicError] if topic_name is not valid
|
41
|
+
def self.topic(topic_name, country: 'us', language: 'en', n: 10)
|
42
|
+
topic_name = topic_name.to_s.upcase
|
43
|
+
raise InvalidTopicError, 'Invalid topic name. See GoogleNews::TOPICS.' unless TOPICS.include?(topic_name)
|
44
|
+
url = TOPICS_RSS + topic_name + '?' + fill_country_lang_params(country, language)
|
45
|
+
limited_items(url, n)
|
46
|
+
end
|
47
|
+
|
48
|
+
#
|
49
|
+
# Get top headlines for a specific geographic location from Google News
|
50
|
+
#
|
51
|
+
# @param [String] position e.g. "48.8566,2.3522" for Paris
|
52
|
+
# @param [String] country
|
53
|
+
# @param [String] language
|
54
|
+
# @param [Integer] n
|
55
|
+
# @return [Array<Hash>]
|
56
|
+
#
|
57
|
+
# Note: Google may not support all locations.
|
58
|
+
# @see https://support.google.com/news/answer/6343863
|
59
|
+
def self.geo(position, country: 'us', language: 'en', n: 10)
|
60
|
+
encoded = URI.encode_www_form_component(position.to_s)
|
61
|
+
url = GEO_RSS + encoded + '?' + fill_country_lang_params(country, language)
|
62
|
+
limited_items(url, n)
|
63
|
+
end
|
64
|
+
|
65
|
+
#
|
66
|
+
# Search Google News for a query
|
67
|
+
#
|
68
|
+
# @param [String] query
|
69
|
+
# @param [String] country
|
70
|
+
# @param [String] language
|
71
|
+
# @param [Integer] n
|
72
|
+
# @return [Array<Hash>]
|
73
|
+
#
|
74
|
+
# Note: This is not the same as a web search. Only news articles are returned.
|
75
|
+
# For a web search, consider using the Google Custom Search API.
|
76
|
+
#
|
77
|
+
# @see https://developers.google.com/custom-search/v1/overview
|
78
|
+
#
|
79
|
+
# @see https://support.google.com/news/answer/6343863
|
80
|
+
def self.search(query, country: 'us', language: 'en', n: 10)
|
81
|
+
encoded = URI.encode_www_form_component(query.to_s)
|
82
|
+
url = SEARCH_RSS + encoded + '&' + fill_country_lang_params(country, language)
|
83
|
+
limited_items(url, n)
|
84
|
+
end
|
85
|
+
|
86
|
+
#
|
87
|
+
# Search Google News for articles from a specific website
|
88
|
+
#
|
89
|
+
# @param [String] query e.g. "example.com"
|
90
|
+
# @param [String] country
|
91
|
+
# @param [String] language
|
92
|
+
# @param [Integer] n
|
93
|
+
# @return [Array<Hash>]
|
94
|
+
#
|
95
|
+
# Note: This is not the same as a web search. Only news articles are returned.
|
96
|
+
# For a web search, consider using the Google Custom Search API.
|
97
|
+
#
|
98
|
+
# @see https://developers.google.com/custom-search/v1/overview
|
99
|
+
#
|
100
|
+
# @see https://support.google.com/news/answer/6343863
|
101
|
+
#
|
102
|
+
# Note: The parameter logic mimics the (possibly flawed) JavaScript version.
|
103
|
+
# It may not yield expected results for all websites.
|
104
|
+
#
|
105
|
+
# @example
|
106
|
+
#
|
107
|
+
# GoogleNews.website("example.com", country: "us", language: "en", n: 5)
|
108
|
+
#
|
109
|
+
# Returns up to 5 news articles from "example.com" in English for the US region.
|
110
|
+
#
|
111
|
+
def self.website(query, country: 'us', language: 'en', n: 10)
|
112
|
+
# Repliziert die (möglicherweise fehlerhafte) Param-Logik aus JS: "site%3A..." + fillWebsiteParams
|
113
|
+
encoded = 'site%3A' + URI.encode_www_form_component(query.to_s)
|
114
|
+
url = SEARCH_RSS + encoded + '&' + fill_website_params(country, language)
|
115
|
+
limited_items(url, n)
|
116
|
+
end
|
117
|
+
|
118
|
+
private
|
119
|
+
|
120
|
+
#
|
121
|
+
# Fill country and language parameters for Google News RSS URLs
|
122
|
+
#
|
123
|
+
# @param [String] country
|
124
|
+
# @param [String] language
|
125
|
+
# @return [String] URL parameters
|
126
|
+
#
|
127
|
+
# Note: This method intentionally matches the JavaScript version,
|
128
|
+
# even though Google typically expects 'hl' to denote language.
|
129
|
+
#
|
130
|
+
# @example
|
131
|
+
#
|
132
|
+
# fill_country_lang_params("us", "en")
|
133
|
+
# # => "hl=US&gl=en&ceid=US%3Aen"
|
134
|
+
#
|
135
|
+
def self.fill_country_lang_params(country, language)
|
136
|
+
c = country.to_s.upcase
|
137
|
+
l = language.to_s.downcase
|
138
|
+
# Bewusst identisch zur JS-Version (auch wenn Google typischerweise hl=Sprache erwartet)
|
139
|
+
"hl=#{c}&gl=#{l}&ceid=#{c}%3A#{l}"
|
140
|
+
end
|
141
|
+
|
142
|
+
#
|
143
|
+
# Fill parameters for website-specific Google News searches
|
144
|
+
#
|
145
|
+
# @param [String] country
|
146
|
+
# @param [String] language
|
147
|
+
# @return [String] URL parameters
|
148
|
+
#
|
149
|
+
# Note: This method replicates the unusual JavaScript parameter string.
|
150
|
+
#
|
151
|
+
# @example
|
152
|
+
#
|
153
|
+
# fill_website_params("us", "en")
|
154
|
+
# # => "3ahl=en-US-&gl=en&ceid=US%3Aen-419"
|
155
|
+
#
|
156
|
+
# This replicates the unusual JS string (3ahl= ... -419)
|
157
|
+
#
|
158
|
+
def self.fill_website_params(country, language)
|
159
|
+
c = country.to_s.upcase
|
160
|
+
l = language.to_s.downcase
|
161
|
+
# Repliziert exakt die ungewöhnliche JS-Zeichenkette (3ahl= ... -419)
|
162
|
+
"3ahl=#{l}-#{c}-&gl=#{l}&ceid=#{c}%3A#{l}-419"
|
163
|
+
end
|
164
|
+
|
165
|
+
#
|
166
|
+
# Fetch and limit items from a Google News RSS feed URL
|
167
|
+
#
|
168
|
+
# @param [String] url
|
169
|
+
# @param [Integer] n
|
170
|
+
# @return [Array<Hash>] limited to n items, returns an empty array if n <= 0 or on error occurs
|
171
|
+
#
|
172
|
+
# Note: Handles network errors gracefully by returning an empty array.
|
173
|
+
#
|
174
|
+
def self.limited_items(url, n)
|
175
|
+
n = n.to_i
|
176
|
+
return [] if n <= 0
|
177
|
+
items = fetch_rss(url)
|
178
|
+
items.first([n, items.size].min)
|
179
|
+
rescue OpenURI::HTTPError, SocketError, IOError => e
|
180
|
+
warn "[GoogleNews] Fehler beim Abruf #{url}: #{e.class}: #{e.message}"
|
181
|
+
[]
|
182
|
+
end
|
183
|
+
|
184
|
+
#
|
185
|
+
# Fetch and parse a Google News RSS feed URL
|
186
|
+
#
|
187
|
+
# @param [String] url
|
188
|
+
# @return [Array<Hash>] parsed items, returns an empty array if no items are found
|
189
|
+
#
|
190
|
+
# Note: Each item is represented as a hash with keys :title, :link, :pub_date, :description, and :raw_item.
|
191
|
+
#
|
192
|
+
def self.fetch_rss(url)
|
193
|
+
content = URI.open(url, 'User-Agent' => default_user_agent, read_timeout: 10).read
|
194
|
+
feed = RSS::Parser.parse(content, false)
|
195
|
+
(feed&.items || []).map { |it| item_to_hash(it) }
|
196
|
+
end
|
197
|
+
|
198
|
+
#
|
199
|
+
# Convert an RSS item to a hash
|
200
|
+
#
|
201
|
+
# @param [RSS::Rss::Channel::Item] item
|
202
|
+
# @return [Hash] with keys :title, :link, :pub_date, :description, and :raw_item
|
203
|
+
#
|
204
|
+
def self.item_to_hash(item)
|
205
|
+
{
|
206
|
+
title: item.title,
|
207
|
+
link: extract_link(item),
|
208
|
+
pub_date: (item.respond_to?(:pubDate) ? item.pubDate : nil),
|
209
|
+
description: (item.respond_to?(:description) ? item.description : nil),
|
210
|
+
raw_item: item
|
211
|
+
}
|
212
|
+
end
|
213
|
+
#
|
214
|
+
# Extract the link from an RSS item
|
215
|
+
#
|
216
|
+
# @param [RSS::Rss::Channel::Item] item
|
217
|
+
# @return [String, nil] the link if available, otherwise nil
|
218
|
+
#
|
219
|
+
# Note: This method checks if the item responds to :link before accessing it.
|
220
|
+
#
|
221
|
+
def self.extract_link(item)
|
222
|
+
return item.link if item.respond_to?(:link)
|
223
|
+
nil
|
224
|
+
end
|
225
|
+
|
226
|
+
#
|
227
|
+
# Default User-Agent string for HTTP requests
|
228
|
+
#
|
229
|
+
# @return [String] User-Agent string
|
230
|
+
#
|
231
|
+
def self.default_user_agent
|
232
|
+
"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/136.0.0.0 Safari/537.36"
|
233
|
+
end
|
234
|
+
end
|
metadata
ADDED
@@ -0,0 +1,141 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: google_news
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Matthäus J. N. Beyrle
|
8
|
+
bindir: bin
|
9
|
+
cert_chain: []
|
10
|
+
date: 1980-01-02 00:00:00.000000000 Z
|
11
|
+
dependencies:
|
12
|
+
- !ruby/object:Gem::Dependency
|
13
|
+
name: rss
|
14
|
+
requirement: !ruby/object:Gem::Requirement
|
15
|
+
requirements:
|
16
|
+
- - ">="
|
17
|
+
- !ruby/object:Gem::Version
|
18
|
+
version: 0.2.6
|
19
|
+
type: :runtime
|
20
|
+
prerelease: false
|
21
|
+
version_requirements: !ruby/object:Gem::Requirement
|
22
|
+
requirements:
|
23
|
+
- - ">="
|
24
|
+
- !ruby/object:Gem::Version
|
25
|
+
version: 0.2.6
|
26
|
+
- !ruby/object:Gem::Dependency
|
27
|
+
name: open-uri
|
28
|
+
requirement: !ruby/object:Gem::Requirement
|
29
|
+
requirements:
|
30
|
+
- - ">="
|
31
|
+
- !ruby/object:Gem::Version
|
32
|
+
version: 0.1.0
|
33
|
+
type: :runtime
|
34
|
+
prerelease: false
|
35
|
+
version_requirements: !ruby/object:Gem::Requirement
|
36
|
+
requirements:
|
37
|
+
- - ">="
|
38
|
+
- !ruby/object:Gem::Version
|
39
|
+
version: 0.1.0
|
40
|
+
- !ruby/object:Gem::Dependency
|
41
|
+
name: bundler
|
42
|
+
requirement: !ruby/object:Gem::Requirement
|
43
|
+
requirements:
|
44
|
+
- - ">="
|
45
|
+
- !ruby/object:Gem::Version
|
46
|
+
version: '2.0'
|
47
|
+
type: :development
|
48
|
+
prerelease: false
|
49
|
+
version_requirements: !ruby/object:Gem::Requirement
|
50
|
+
requirements:
|
51
|
+
- - ">="
|
52
|
+
- !ruby/object:Gem::Version
|
53
|
+
version: '2.0'
|
54
|
+
- !ruby/object:Gem::Dependency
|
55
|
+
name: rake
|
56
|
+
requirement: !ruby/object:Gem::Requirement
|
57
|
+
requirements:
|
58
|
+
- - ">="
|
59
|
+
- !ruby/object:Gem::Version
|
60
|
+
version: '10.0'
|
61
|
+
type: :development
|
62
|
+
prerelease: false
|
63
|
+
version_requirements: !ruby/object:Gem::Requirement
|
64
|
+
requirements:
|
65
|
+
- - ">="
|
66
|
+
- !ruby/object:Gem::Version
|
67
|
+
version: '10.0'
|
68
|
+
- !ruby/object:Gem::Dependency
|
69
|
+
name: rspec
|
70
|
+
requirement: !ruby/object:Gem::Requirement
|
71
|
+
requirements:
|
72
|
+
- - ">="
|
73
|
+
- !ruby/object:Gem::Version
|
74
|
+
version: '3.0'
|
75
|
+
type: :development
|
76
|
+
prerelease: false
|
77
|
+
version_requirements: !ruby/object:Gem::Requirement
|
78
|
+
requirements:
|
79
|
+
- - ">="
|
80
|
+
- !ruby/object:Gem::Version
|
81
|
+
version: '3.0'
|
82
|
+
- !ruby/object:Gem::Dependency
|
83
|
+
name: pry
|
84
|
+
requirement: !ruby/object:Gem::Requirement
|
85
|
+
requirements:
|
86
|
+
- - ">="
|
87
|
+
- !ruby/object:Gem::Version
|
88
|
+
version: 0.10.0
|
89
|
+
type: :development
|
90
|
+
prerelease: false
|
91
|
+
version_requirements: !ruby/object:Gem::Requirement
|
92
|
+
requirements:
|
93
|
+
- - ">="
|
94
|
+
- !ruby/object:Gem::Version
|
95
|
+
version: 0.10.0
|
96
|
+
email:
|
97
|
+
- google_news.gemspec@mail.magynhard.de
|
98
|
+
executables:
|
99
|
+
- google_news
|
100
|
+
extensions: []
|
101
|
+
extra_rdoc_files: []
|
102
|
+
files:
|
103
|
+
- ".gitattributes"
|
104
|
+
- ".gitignore"
|
105
|
+
- ".rspec"
|
106
|
+
- CODE_OF_CONDUCT.md
|
107
|
+
- Gemfile
|
108
|
+
- LICENSE
|
109
|
+
- README.md
|
110
|
+
- Rakefile
|
111
|
+
- bin/console
|
112
|
+
- bin/google_news
|
113
|
+
- bin/google_news.bat
|
114
|
+
- bin/setup
|
115
|
+
- google_news.gemspec
|
116
|
+
- lib/custom_errors/invalid_key_type_error.rb
|
117
|
+
- lib/google_news.rb
|
118
|
+
- lib/google_news/version.rb
|
119
|
+
homepage: https://github.com/magynhard/google_news
|
120
|
+
licenses:
|
121
|
+
- MIT
|
122
|
+
metadata:
|
123
|
+
allowed_push_host: https://rubygems.org
|
124
|
+
rdoc_options: []
|
125
|
+
require_paths:
|
126
|
+
- lib
|
127
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
128
|
+
requirements:
|
129
|
+
- - ">="
|
130
|
+
- !ruby/object:Gem::Version
|
131
|
+
version: '0'
|
132
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
133
|
+
requirements:
|
134
|
+
- - ">="
|
135
|
+
- !ruby/object:Gem::Version
|
136
|
+
version: '0'
|
137
|
+
requirements: []
|
138
|
+
rubygems_version: 3.7.1
|
139
|
+
specification_version: 4
|
140
|
+
summary: Get Google News headlines from Ruby or the command line.
|
141
|
+
test_files: []
|