Skip to content

Commit 1ed277c

Browse files
authored
Merge pull request #24 from jborchma/exclude_domains
added exclude domain parameter
2 parents f59f9d6 + 8009971 commit 1ed277c

2 files changed

Lines changed: 46 additions & 32 deletions

File tree

newsapi/newsapi_client.py

Lines changed: 41 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -13,31 +13,31 @@ def get_top_headlines(self, q=None, sources=None, language=None, country=None, c
1313
page=None):
1414
"""
1515
Returns live top and breaking headlines for a country, specific category in a country, single source, or multiple sources..
16-
16+
1717
Optional parameters:
1818
(str) q - return headlines w/ specific keyword or phrase. For example:
1919
'bitcoin', 'trump', 'tesla', 'ethereum', etc.
20-
20+
2121
(str) sources - return headlines of news sources! some Valid values are:
2222
'bbc-news', 'the-verge', 'abc-news', 'crypto coins news',
2323
'ary news','associated press','wired','aftenposten','australian financial review','axios',
2424
'bbc news','bild','blasting news','bloomberg','business insider','engadget','google news',
2525
'hacker news','info money,'recode','techcrunch','techradar','the next web','the verge' etc.
26-
26+
2727
(str) language - The 2-letter ISO-639-1 code of the language you want to get headlines for. Valid values are:
2828
'ar','de','en','es','fr','he','it','nl','no','pt','ru','se','ud','zh'
29-
29+
3030
(str) country - The 2-letter ISO 3166-1 code of the country you want to get headlines! Valid values are:
31-
'ae','ar','at','au','be','bg','br','ca','ch','cn','co','cu','cz','de','eg','fr','gb','gr',
31+
'ae','ar','at','au','be','bg','br','ca','ch','cn','co','cu','cz','de','eg','fr','gb','gr',
3232
'hk','hu','id','ie','il','in','it','jp','kr','lt','lv','ma','mx','my','ng','nl','no','nz',
3333
'ph','pl','pt','ro','rs','ru','sa','se','sg','si','sk','th','tr','tw','ua','us'
34-
34+
3535
(str) category - The category you want to get headlines for! Valid values are:
3636
'business','entertainment','general','health','science','sports','technology'
37-
38-
(int) page_size - The number of results to return per page (request). 20 is the default, 100 is the maximum.
39-
40-
(int) page - Use this to page through the results if the total results found is greater than the page size.
37+
38+
(int) page_size - The number of results to return per page (request). 20 is the default, 100 is the maximum.
39+
40+
(int) page - Use this to page through the results if the total results found is greater than the page size.
4141
"""
4242

4343
# Define Payload
@@ -50,7 +50,7 @@ def get_top_headlines(self, q=None, sources=None, language=None, country=None, c
5050
else:
5151
raise TypeError('keyword/phrase q param should be a of type str')
5252

53-
# Sources
53+
# Sources
5454
if (sources is not None) and ((country is not None) or (category is not None)):
5555
raise ValueError('cannot mix country/category param with sources param.')
5656

@@ -61,7 +61,7 @@ def get_top_headlines(self, q=None, sources=None, language=None, country=None, c
6161
else:
6262
raise TypeError('sources param should be of type str')
6363

64-
# Language
64+
# Language
6565
if language is not None:
6666
if type(language) == str:
6767
if language in const.languages:
@@ -120,36 +120,39 @@ def get_top_headlines(self, q=None, sources=None, language=None, country=None, c
120120

121121
return r.json()
122122

123-
def get_everything(self, q=None, sources=None, domains=None, from_param=None, to=None, language=None,
124-
sort_by=None, page=None, page_size=None):
123+
def get_everything(self, q=None, sources=None, domains=None, exclude_domains=None,
124+
from_param=None, to=None, language=None, sort_by=None, page=None,
125+
page_size=None):
125126
"""
126127
Search through millions of articles from over 5,000 large and small news sources and blogs.
127-
128+
128129
Optional parameters:
129130
(str) q - return headlines w/ specified coin! Valid values are:
130131
'bitcoin', 'trump', 'tesla', 'ethereum', etc
131-
132+
132133
(str) sources - return headlines of news sources! some Valid values are:
133134
'bbc-news', 'the-verge', 'abc-news', 'crypto coins news',
134135
'ary news','associated press','wired','aftenposten','australian financial review','axios',
135136
'bbc news','bild','blasting news','bloomberg','business insider','engadget','google news',
136137
'hacker news','info money,'recode','techcrunch','techradar','the next web','the verge' etc.
137-
138-
(str) domains - A comma-seperated string of domains (eg bbc.co.uk, techcrunch.com, engadget.com) to restrict the search to.
138+
139+
(str) domains - A comma-seperated string of domains (eg bbc.co.uk, techcrunch.com, engadget.com) to restrict the search to.
140+
141+
(str) exclude_domains - A comma_seperated string of domains to be excluded from the search
139142
140143
(str) from_parameter - A date and optional time for the oldest article allowed.
141144
(e.g. 2018-03-05 or 2018-03-05T03:46:15)
142-
145+
143146
(str) to - A date and optional time for the newest article allowed.
144-
147+
145148
(str) language - The 2-letter ISO-639-1 code of the language you want to get headlines for. Valid values are:
146149
'ar','de','en','es','fr','he','it','nl','no','pt','ru','se','ud','zh'
147-
150+
148151
(str) sort_by - The order to sort the articles in. Valid values are: 'relevancy','popularity','publishedAt'
149-
150-
(int) page_size - The number of results to return per page (request). 20 is the default, 100 is the maximum.
151152
152-
(int) page - Use this to page through the results if the total results found is greater than the page size.
153+
(int) page_size - The number of results to return per page (request). 20 is the default, 100 is the maximum.
154+
155+
(int) page - Use this to page through the results if the total results found is greater than the page size.
153156
"""
154157

155158
# Define Payload
@@ -176,7 +179,13 @@ def get_everything(self, q=None, sources=None, domains=None, from_param=None, to
176179
else:
177180
raise TypeError('domains param should be of type str')
178181

179-
# Search From This Date ...
182+
if exclude_domains is not None:
183+
if isinstance(exclude_domains, str):
184+
payload['excludeDomains'] = exclude_domains
185+
else:
186+
raise TypeError('exclude_domains param should be of type str')
187+
188+
# Search From This Date ...
180189
if from_param is not None:
181190
if type(from_param) == str:
182191
if (len(from_param)) >= 10:
@@ -190,7 +199,7 @@ def get_everything(self, q=None, sources=None, domains=None, from_param=None, to
190199
else:
191200
raise TypeError('from_param should be of type str')
192201

193-
# ... To This Date
202+
# ... To This Date
194203
if to is not None:
195204
if type(to) == str:
196205
if (len(to)) >= 10:
@@ -256,22 +265,22 @@ def get_everything(self, q=None, sources=None, domains=None, from_param=None, to
256265
def get_sources(self, category=None, language=None, country=None):
257266
"""
258267
Returns the subset of news publishers that top headlines...
259-
268+
260269
Optional parameters:
261270
(str) category - The category you want to get headlines for! Valid values are:
262271
'business','entertainment','general','health','science','sports','technology'
263-
272+
264273
(str) language - The 2-letter ISO-639-1 code of the language you want to get headlines for. Valid values are:
265274
'ar','de','en','es','fr','he','it','nl','no','pt','ru','se','ud','zh'
266-
275+
267276
(str) country - The 2-letter ISO 3166-1 code of the country you want to get headlines! Valid values are:
268-
'ae','ar','at','au','be','bg','br','ca','ch','cn','co','cu','cz','de','eg','fr','gb','gr',
277+
'ae','ar','at','au','be','bg','br','ca','ch','cn','co','cu','cz','de','eg','fr','gb','gr',
269278
'hk','hu','id','ie','il','in','it','jp','kr','lt','lv','ma','mx','my','ng','nl','no','nz',
270279
'ph','pl','pt','ro','rs','ru','sa','se','sg','si','sk','th','tr','tw','ua','us'
271-
280+
272281
(str) category - The category you want to get headlines for! Valid values are:
273282
'business','entertainment','general','health','science','sports','technology'
274-
283+
275284
"""
276285

277286
# Define Payload

tests/test_newsapi_client.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,11 @@ def test_api_get_everything(self):
9797
with self.assertRaises(TypeError):
9898
self.api.get_everything(domains=domains)
9999

100+
# Raise TypeError is exclude_domains param is not of type str
101+
exclude_domains = 0
102+
with self.assertRaises(TypeError):
103+
self.api.get_everything(exclude_domains=exclude_domains)
104+
100105
# Raise TypeError is from_param param is not of type str
101106
from_param = 0
102107
with self.assertRaises(TypeError):

0 commit comments

Comments
 (0)