11import requests
22from newsapi .newsapi_auth import NewsApiAuth
33from newsapi import const
4+ from newsapi .newsapi_exception import NewsAPIException
45
56
67class NewsApiClient (object ):
@@ -38,7 +39,7 @@ def get_top_headlines(self, q=None, sources=None, language=None, country=None, c
3839
3940 (int) page - Use this to page through the results if the total results found is greater than the page size.
4041 """
41-
42+
4243 # Define Payload
4344 payload = {}
4445
@@ -49,36 +50,38 @@ def get_top_headlines(self, q=None, sources=None, language=None, country=None, c
4950 else :
5051 raise TypeError ('keyword/phrase q param should be a str' )
5152
52- # Sources
53- if (country is not None ) or (category is not None ):
54- raise ValueError ('cannot mix country/category param with sources param.' )
55- else :
53+ # Sources
54+ if sources is not None :
5655 if type (sources ) == str :
57- payload ['sources' ] = sources
56+ payload ['sources' ] = sources
5857 else :
5958 raise TypeError ('sources param should be a str' )
6059
60+ # Sources
61+ if (country is not None ) and (category is not None ):
62+ raise ValueError ('cannot mix country/category param with sources param.' )
63+
6164 # Language
6265 if language is not None :
6366 if type (language ) == str :
6467 if language in const .languages :
6568 payload ['language' ] = language
6669 else :
6770 raise ValueError ('invalid language' )
68- else :
71+ else :
6972 raise TypeError ('language param should be a string' )
7073
7174 # Country
7275 if country is not None :
7376 if type (country ) == str :
74- if country in const .countries :
77+ if country in const .countries :
7578 payload ['country' ] = country
7679 else :
7780 raise ValueError ('invalid country' )
7881 else :
79- raise TypeError ('country param should be a string' )
80-
81- # Category
82+ raise TypeError ('country param should be a string' )
83+
84+ # Category
8285 if category is not None :
8386 if type (category ) == str :
8487 if category in const .categories :
@@ -87,7 +90,7 @@ def get_top_headlines(self, q=None, sources=None, language=None, country=None, c
8790 raise ValueError ('invalid category' )
8891 else :
8992 raise TypeError ('category param should be a string' )
90-
93+
9194 # Page Size
9295 if page_size is not None :
9396 if type (page_size ) == int :
@@ -149,7 +152,7 @@ def get_everything(self, q=None, sources=None, domains=None, from_param=None, to
149152
150153 (int) page - Use this to page through the results if the total results found is greater than the page size.
151154 """
152-
155+
153156 # Define Payload
154157 payload = {}
155158
@@ -159,16 +162,14 @@ def get_everything(self, q=None, sources=None, domains=None, from_param=None, to
159162 payload ['q' ] = q
160163 else :
161164 raise TypeError ('keyword/phrase q param should be a str' )
162-
163- # Sources
164- if (country is not None ) or (category is not None ):
165- raise ValueError ('cannot mix country or category param with sources param.' )
166- else :
165+
166+ # Sources
167+ if sources is not None :
167168 if type (sources ) == str :
168169 payload ['sources' ] = sources
169170 else :
170171 raise TypeError ('sources param should be a str' )
171-
172+
172173 # Domains To Search
173174 if domains is not None :
174175 if type (domains ) == str :
@@ -204,7 +205,6 @@ def get_everything(self, q=None, sources=None, domains=None, from_param=None, to
204205 else :
205206 raise TypeError ('to param should be a string' )
206207
207-
208208 # Language
209209 if language is not None :
210210 if type (language ) == str :
@@ -214,7 +214,6 @@ def get_everything(self, q=None, sources=None, domains=None, from_param=None, to
214214 payload ['language' ] = language
215215 else :
216216 raise TypeError ('language param should be a string' )
217-
218217
219218 # Sort Method
220219 if sort_by is not None :
@@ -225,7 +224,7 @@ def get_everything(self, q=None, sources=None, domains=None, from_param=None, to
225224 raise ValueError ('invalid sort' )
226225 else :
227226 raise TypeError ('sort_by param should be a string' )
228-
227+
229228 # Page Size
230229 if page_size is not None :
231230 if type (page_size ) == int :
@@ -246,11 +245,10 @@ def get_everything(self, q=None, sources=None, domains=None, from_param=None, to
246245 else :
247246 raise TypeError ('page param should be an int' )
248247
249-
250248 # Send Request
251249 r = requests .get (const .EVERYTHING_URL , auth = self .auth , timeout = 30 , params = payload )
252250
253- #Check Status of Request
251+ # Check Status of Request
254252 if r .status_code != requests .codes .ok :
255253 raise NewsAPIException (r .json ())
256254
@@ -275,8 +273,8 @@ def get_sources(self, category=None, language=None, country=None):
275273 (str) category - The category you want to get headlines for! Valid values are:
276274 'business','entertainment','general','health','science','sports','technology'
277275
278- """
279-
276+ """
277+
280278 # Define Payload
281279 payload = {}
282280
@@ -287,20 +285,20 @@ def get_sources(self, category=None, language=None, country=None):
287285 payload ['language' ] = language
288286 else :
289287 raise ValueError ('invalid language' )
290- else :
288+ else :
291289 raise TypeError ('language param should be a string' )
292290
293291 # Country
294292 if country is not None :
295293 if type (country ) == str :
296- if country in const .countries :
294+ if country in const .countries :
297295 payload ['country' ] = country
298296 else :
299297 raise ValueError ('invalid country' )
300298 else :
301- raise TypeError ('country param should be a string' )
299+ raise TypeError ('country param should be a string' )
302300
303- # Category
301+ # Category
304302 if category is not None :
305303 if type (category ) == str :
306304 if category in const .categories :
@@ -318,4 +316,3 @@ def get_sources(self, category=None, language=None, country=None):
318316 raise NewsAPIException (r .json ())
319317
320318 return r .json ()
321-
0 commit comments