1
1
import requests
2
2
from newsapi .newsapi_auth import NewsApiAuth
3
3
from newsapi import const
4
+ from newsapi .newsapi_exception import NewsAPIException
4
5
5
6
6
7
class NewsApiClient (object ):
@@ -38,7 +39,7 @@ def get_top_headlines(self, q=None, sources=None, language=None, country=None, c
38
39
39
40
(int) page - Use this to page through the results if the total results found is greater than the page size.
40
41
"""
41
-
42
+
42
43
# Define Payload
43
44
payload = {}
44
45
@@ -49,36 +50,38 @@ def get_top_headlines(self, q=None, sources=None, language=None, country=None, c
49
50
else :
50
51
raise TypeError ('keyword/phrase q param should be a str' )
51
52
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 :
56
55
if type (sources ) == str :
57
- payload ['sources' ] = sources
56
+ payload ['sources' ] = sources
58
57
else :
59
58
raise TypeError ('sources param should be a str' )
60
59
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
+
61
64
# Language
62
65
if language is not None :
63
66
if type (language ) == str :
64
67
if language in const .languages :
65
68
payload ['language' ] = language
66
69
else :
67
70
raise ValueError ('invalid language' )
68
- else :
71
+ else :
69
72
raise TypeError ('language param should be a string' )
70
73
71
74
# Country
72
75
if country is not None :
73
76
if type (country ) == str :
74
- if country in const .countries :
77
+ if country in const .countries :
75
78
payload ['country' ] = country
76
79
else :
77
80
raise ValueError ('invalid country' )
78
81
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
82
85
if category is not None :
83
86
if type (category ) == str :
84
87
if category in const .categories :
@@ -87,7 +90,7 @@ def get_top_headlines(self, q=None, sources=None, language=None, country=None, c
87
90
raise ValueError ('invalid category' )
88
91
else :
89
92
raise TypeError ('category param should be a string' )
90
-
93
+
91
94
# Page Size
92
95
if page_size is not None :
93
96
if type (page_size ) == int :
@@ -149,7 +152,7 @@ def get_everything(self, q=None, sources=None, domains=None, from_param=None, to
149
152
150
153
(int) page - Use this to page through the results if the total results found is greater than the page size.
151
154
"""
152
-
155
+
153
156
# Define Payload
154
157
payload = {}
155
158
@@ -159,16 +162,14 @@ def get_everything(self, q=None, sources=None, domains=None, from_param=None, to
159
162
payload ['q' ] = q
160
163
else :
161
164
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 :
167
168
if type (sources ) == str :
168
169
payload ['sources' ] = sources
169
170
else :
170
171
raise TypeError ('sources param should be a str' )
171
-
172
+
172
173
# Domains To Search
173
174
if domains is not None :
174
175
if type (domains ) == str :
@@ -204,7 +205,6 @@ def get_everything(self, q=None, sources=None, domains=None, from_param=None, to
204
205
else :
205
206
raise TypeError ('to param should be a string' )
206
207
207
-
208
208
# Language
209
209
if language is not None :
210
210
if type (language ) == str :
@@ -214,7 +214,6 @@ def get_everything(self, q=None, sources=None, domains=None, from_param=None, to
214
214
payload ['language' ] = language
215
215
else :
216
216
raise TypeError ('language param should be a string' )
217
-
218
217
219
218
# Sort Method
220
219
if sort_by is not None :
@@ -225,7 +224,7 @@ def get_everything(self, q=None, sources=None, domains=None, from_param=None, to
225
224
raise ValueError ('invalid sort' )
226
225
else :
227
226
raise TypeError ('sort_by param should be a string' )
228
-
227
+
229
228
# Page Size
230
229
if page_size is not None :
231
230
if type (page_size ) == int :
@@ -246,11 +245,10 @@ def get_everything(self, q=None, sources=None, domains=None, from_param=None, to
246
245
else :
247
246
raise TypeError ('page param should be an int' )
248
247
249
-
250
248
# Send Request
251
249
r = requests .get (const .EVERYTHING_URL , auth = self .auth , timeout = 30 , params = payload )
252
250
253
- #Check Status of Request
251
+ # Check Status of Request
254
252
if r .status_code != requests .codes .ok :
255
253
raise NewsAPIException (r .json ())
256
254
@@ -275,8 +273,8 @@ def get_sources(self, category=None, language=None, country=None):
275
273
(str) category - The category you want to get headlines for! Valid values are:
276
274
'business','entertainment','general','health','science','sports','technology'
277
275
278
- """
279
-
276
+ """
277
+
280
278
# Define Payload
281
279
payload = {}
282
280
@@ -287,20 +285,20 @@ def get_sources(self, category=None, language=None, country=None):
287
285
payload ['language' ] = language
288
286
else :
289
287
raise ValueError ('invalid language' )
290
- else :
288
+ else :
291
289
raise TypeError ('language param should be a string' )
292
290
293
291
# Country
294
292
if country is not None :
295
293
if type (country ) == str :
296
- if country in const .countries :
294
+ if country in const .countries :
297
295
payload ['country' ] = country
298
296
else :
299
297
raise ValueError ('invalid country' )
300
298
else :
301
- raise TypeError ('country param should be a string' )
299
+ raise TypeError ('country param should be a string' )
302
300
303
- # Category
301
+ # Category
304
302
if category is not None :
305
303
if type (category ) == str :
306
304
if category in const .categories :
@@ -318,4 +316,3 @@ def get_sources(self, category=None, language=None, country=None):
318
316
raise NewsAPIException (r .json ())
319
317
320
318
return r .json ()
321
-
0 commit comments