@@ -32,7 +32,7 @@ def __init__(self, api_key, session=None):
3232 self .request_method = session
3333
3434 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
3636 ):
3737 """Call the `/top-headlines` endpoint.
3838
@@ -116,17 +116,18 @@ def get_top_headlines( # noqa: C901
116116 # Language
117117 if language is not None :
118118 if is_valid_string (language ):
119- if language in const .languages :
119+ if language in const .LANGUAGES :
120120 payload ["language" ] = language
121121 else :
122122 raise ValueError ("invalid language" )
123123 else :
124124 raise TypeError ("language param should be of type str" )
125125
126+
126127 # Country
127128 if country is not None :
128129 if is_valid_string (country ):
129- if country in const .countries :
130+ if country in const .COUNTRIES :
130131 payload ["country" ] = country
131132 else :
132133 raise ValueError ("invalid country" )
@@ -136,7 +137,7 @@ def get_top_headlines( # noqa: C901
136137 # Category
137138 if category is not None :
138139 if is_valid_string (category ):
139- if category in const .categories :
140+ if category in const .CATEGORIES :
140141 payload ["category" ] = category
141142 else :
142143 raise ValueError ("invalid category" )
@@ -162,7 +163,8 @@ def get_top_headlines( # noqa: C901
162163 raise ValueError ("page param should be an int greater than 0" )
163164 else :
164165 raise TypeError ("page param should be an int" )
165-
166+ if payload .get ("language" ) is None :
167+ payload ["language" ] = const .DEFAULT_LANGUAGES .get (country )
166168 # Send Request
167169 r = self .request_method .get (const .TOP_HEADLINES_URL , auth = self .auth , timeout = 30 , params = payload )
168170
@@ -291,7 +293,7 @@ def get_everything( # noqa: C901
291293 # Language
292294 if language is not None :
293295 if is_valid_string (language ):
294- if language not in const .languages :
296+ if language not in const .LANGUAGES :
295297 raise ValueError ("invalid language" )
296298 else :
297299 payload ["language" ] = language
@@ -301,7 +303,7 @@ def get_everything( # noqa: C901
301303 # Sort Method
302304 if sort_by is not None :
303305 if is_valid_string (sort_by ):
304- if sort_by in const .sort_method :
306+ if sort_by in const .SORT_METHOD :
305307 payload ["sortBy" ] = sort_by
306308 else :
307309 raise ValueError ("invalid sort" )
@@ -330,7 +332,7 @@ def get_everything( # noqa: C901
330332
331333 # Send Request
332334 r = self .request_method .get (const .EVERYTHING_URL , auth = self .auth , timeout = 30 , params = payload )
333-
335+
334336 # Check Status of Request
335337 if r .status_code != requests .codes .ok :
336338 raise NewsAPIException (r .json ())
@@ -364,7 +366,7 @@ def get_sources(self, category=None, language=None, country=None): # noqa: C901
364366 # Language
365367 if language is not None :
366368 if is_valid_string (language ):
367- if language in const .languages :
369+ if language in const .LANGUAGES :
368370 payload ["language" ] = language
369371 else :
370372 raise ValueError ("invalid language" )
@@ -374,7 +376,7 @@ def get_sources(self, category=None, language=None, country=None): # noqa: C901
374376 # Country
375377 if country is not None :
376378 if is_valid_string (country ):
377- if country in const .countries :
379+ if country in const .COUNTRIES :
378380 payload ["country" ] = country
379381 else :
380382 raise ValueError ("invalid country" )
@@ -384,14 +386,16 @@ def get_sources(self, category=None, language=None, country=None): # noqa: C901
384386 # Category
385387 if category is not None :
386388 if is_valid_string (category ):
387- if category in const .categories :
389+ if category in const .CATEGORIES :
388390 payload ["category" ] = category
389391 else :
390392 raise ValueError ("invalid category" )
391393 else :
392394 raise TypeError ("category param should be of type str" )
393395
394396 # Send Request
397+ if payload .get ("language" ) is None :
398+ payload ["language" ] = const .DEFAULT_LANGUAGES .get (country )
395399 r = self .request_method .get (const .SOURCES_URL , auth = self .auth , timeout = 30 , params = payload )
396400
397401 # Check Status of Request
0 commit comments