Skip to content

Commit

Permalink
Merge pull request #71 from bckenstler/feature/SiteSearch
Browse files Browse the repository at this point in the history
Feature/site search
  • Loading branch information
ranahaani authored Oct 26, 2023
2 parents 725f8ec + eccc7b0 commit 417dc80
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 0 deletions.
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
13 changes: 13 additions & 0 deletions gnews/gnews.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
7 changes: 7 additions & 0 deletions tests/test_gnews.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 417dc80

Please sign in to comment.