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 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: 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