@@ -32,7 +32,7 @@ def __init__(self, api_key, session=None):
32
32
self .request_method = session
33
33
34
34
def get_top_headlines ( # noqa: C901
35
- self , q = None , qintitle = None , sources = None , language = "en" , country = None , category = None , page_size = None , page = None
35
+ self , q = None , qintitle = None , sources = None , language = None , country = None , category = None , page_size = None , page = None
36
36
):
37
37
"""Call the `/top-headlines` endpoint.
38
38
@@ -116,17 +116,18 @@ def get_top_headlines( # noqa: C901
116
116
# Language
117
117
if language is not None :
118
118
if is_valid_string (language ):
119
- if language in const .languages :
119
+ if language in const .LANGUAGES :
120
120
payload ["language" ] = language
121
121
else :
122
122
raise ValueError ("invalid language" )
123
123
else :
124
124
raise TypeError ("language param should be of type str" )
125
125
126
+
126
127
# Country
127
128
if country is not None :
128
129
if is_valid_string (country ):
129
- if country in const .countries :
130
+ if country in const .COUNTRIES :
130
131
payload ["country" ] = country
131
132
else :
132
133
raise ValueError ("invalid country" )
@@ -136,7 +137,7 @@ def get_top_headlines( # noqa: C901
136
137
# Category
137
138
if category is not None :
138
139
if is_valid_string (category ):
139
- if category in const .categories :
140
+ if category in const .CATEGORIES :
140
141
payload ["category" ] = category
141
142
else :
142
143
raise ValueError ("invalid category" )
@@ -162,7 +163,8 @@ def get_top_headlines( # noqa: C901
162
163
raise ValueError ("page param should be an int greater than 0" )
163
164
else :
164
165
raise TypeError ("page param should be an int" )
165
-
166
+ if payload .get ("language" ) is None :
167
+ payload ["language" ] = const .DEFAULT_LANGUAGES .get (country )
166
168
# Send Request
167
169
r = self .request_method .get (const .TOP_HEADLINES_URL , auth = self .auth , timeout = 30 , params = payload )
168
170
@@ -291,7 +293,7 @@ def get_everything( # noqa: C901
291
293
# Language
292
294
if language is not None :
293
295
if is_valid_string (language ):
294
- if language not in const .languages :
296
+ if language not in const .LANGUAGES :
295
297
raise ValueError ("invalid language" )
296
298
else :
297
299
payload ["language" ] = language
@@ -301,7 +303,7 @@ def get_everything( # noqa: C901
301
303
# Sort Method
302
304
if sort_by is not None :
303
305
if is_valid_string (sort_by ):
304
- if sort_by in const .sort_method :
306
+ if sort_by in const .SORT_METHOD :
305
307
payload ["sortBy" ] = sort_by
306
308
else :
307
309
raise ValueError ("invalid sort" )
@@ -330,7 +332,7 @@ def get_everything( # noqa: C901
330
332
331
333
# Send Request
332
334
r = self .request_method .get (const .EVERYTHING_URL , auth = self .auth , timeout = 30 , params = payload )
333
-
335
+
334
336
# Check Status of Request
335
337
if r .status_code != requests .codes .ok :
336
338
raise NewsAPIException (r .json ())
@@ -364,7 +366,7 @@ def get_sources(self, category=None, language=None, country=None): # noqa: C901
364
366
# Language
365
367
if language is not None :
366
368
if is_valid_string (language ):
367
- if language in const .languages :
369
+ if language in const .LANGUAGES :
368
370
payload ["language" ] = language
369
371
else :
370
372
raise ValueError ("invalid language" )
@@ -374,7 +376,7 @@ def get_sources(self, category=None, language=None, country=None): # noqa: C901
374
376
# Country
375
377
if country is not None :
376
378
if is_valid_string (country ):
377
- if country in const .countries :
379
+ if country in const .COUNTRIES :
378
380
payload ["country" ] = country
379
381
else :
380
382
raise ValueError ("invalid country" )
@@ -384,14 +386,16 @@ def get_sources(self, category=None, language=None, country=None): # noqa: C901
384
386
# Category
385
387
if category is not None :
386
388
if is_valid_string (category ):
387
- if category in const .categories :
389
+ if category in const .CATEGORIES :
388
390
payload ["category" ] = category
389
391
else :
390
392
raise ValueError ("invalid category" )
391
393
else :
392
394
raise TypeError ("category param should be of type str" )
393
395
394
396
# Send Request
397
+ if payload .get ("language" ) is None :
398
+ payload ["language" ] = const .DEFAULT_LANGUAGES .get (country )
395
399
r = self .request_method .get (const .SOURCES_URL , auth = self .auth , timeout = 30 , params = payload )
396
400
397
401
# Check Status of Request
0 commit comments