From 47baf7f620acdb4e6fd788ca7a0b2df8d6aaec36 Mon Sep 17 00:00:00 2001 From: Brad Kenstler Date: Mon, 23 Oct 2023 13:41:13 -0700 Subject: [PATCH 1/3] Aed feature get_news_by_site Adds site filter to URL to query by site. --- gnews/gnews.py | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/gnews/gnews.py b/gnews/gnews.py index 4894ff4..22a4979 100644 --- a/gnews/gnews.py +++ b/gnews/gnews.py @@ -281,6 +281,19 @@ def get_news_by_location(self, location: str): logger.warning("Enter a valid location.") return [] + @docstring_parameter(standard_output) + def get_news_by_site(self, site: str): + """ + This function is used to get news from a specific site + :param site: (type: str) The site domain for which you want to get headlines. E.g., 'cnn.com' + :return: A list of news articles from the specified site. + """ + if site: + key = "site:{}".format(site) + return self.get_news(key) + logger.warning("Enter a valid site domain.") + return [] + def _get_news(self, query): url = BASE_URL + query + self._ceid() try: From 9304b53e24a263167602b229f1a6c3079bd5db6e Mon Sep 17 00:00:00 2001 From: Brad Kenstler Date: Mon, 23 Oct 2023 13:44:05 -0700 Subject: [PATCH 2/3] Update README.md for get_news_by_site --- README.md | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/README.md b/README.md index edb0280..389c3df 100644 --- a/README.md +++ b/README.md @@ -181,6 +181,11 @@ print(pakistan_news[0]) * `GNews.get_news_by_location(location)` * location can be name of city/state/country +### Get news by site + +* `GNews.get_news_by_site(site)` +* site should be in the format of: `"cnn.com"` + ### Results specification * It's possible to pass proxy, country, language, period, start date, end date exclude websites and size during initialization From eccc7b0993c38f35ed56b4db94adbd3ecf46592c Mon Sep 17 00:00:00 2001 From: Brad Kenstler Date: Tue, 24 Oct 2023 12:04:06 -0700 Subject: [PATCH 3/3] Update test_gnews.py --- tests/test_gnews.py | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/tests/test_gnews.py b/tests/test_gnews.py index 1114a2c..d8fca5c 100644 --- a/tests/test_gnews.py +++ b/tests/test_gnews.py @@ -33,6 +33,13 @@ def test_get_news_by_location(self): self.assertTrue(isinstance(news_articles, list)) self.assertTrue(len(news_articles) > 0) + def test_get_news_by_site(self): + # Test that get_news_by_site returns a non-empty list of news articles for a valid site + site = "cnn.com" + news_articles - self.gnews.get_news_by_site(site) + self.assertTrue(isinstance(news_articles, list)) + self.assertTrue(len(news_articles) > 0) + def test_get_full_article(self): pass # Test that get_full_article returns a valid article object for a valid URL