22from newsapi .newsapi_auth import NewsApiAuth
33from newsapi import const
44from newsapi .newsapi_exception import NewsAPIException
5+ from sys import version_info
56
67
78class NewsApiClient (object ):
@@ -45,25 +46,25 @@ def get_top_headlines(self, q=None, sources=None, language='en', country=None, c
4546
4647 # Keyword/Phrase
4748 if q is not None :
48- if type (q ) == str :
49+ if is_valid_string (q ):
4950 payload ['q' ] = q
5051 else :
51- raise TypeError ('keyword/phrase q param should be a of type str' )
52+ raise TypeError ('keyword/phrase q param should be of type str' )
5253
5354 # Sources
5455 if (sources is not None ) and ((country is not None ) or (category is not None )):
5556 raise ValueError ('cannot mix country/category param with sources param.' )
5657
5758 # Sources
5859 if sources is not None :
59- if type (sources ) == str :
60+ if is_valid_string (sources ):
6061 payload ['sources' ] = sources
6162 else :
6263 raise TypeError ('sources param should be of type str' )
6364
6465 # Language
6566 if language is not None :
66- if type (language ) == str :
67+ if is_valid_string (language ):
6768 if language in const .languages :
6869 payload ['language' ] = language
6970 else :
@@ -73,7 +74,7 @@ def get_top_headlines(self, q=None, sources=None, language='en', country=None, c
7374
7475 # Country
7576 if country is not None :
76- if type (country ) == str :
77+ if is_valid_string (country ):
7778 if country in const .countries :
7879 payload ['country' ] = country
7980 else :
@@ -83,7 +84,7 @@ def get_top_headlines(self, q=None, sources=None, language='en', country=None, c
8384
8485 # Category
8586 if category is not None :
86- if type (category ) == str :
87+ if is_valid_string (category ):
8788 if category in const .categories :
8889 payload ['category' ] = category
8990 else :
@@ -160,21 +161,21 @@ def get_everything(self, q=None, sources=None, domains=None, exclude_domains=Non
160161
161162 # Keyword/Phrase
162163 if q is not None :
163- if type (q ) == str :
164+ if is_valid_string (q ):
164165 payload ['q' ] = q
165166 else :
166167 raise TypeError ('keyword/phrase q param should be of type str' )
167168
168169 # Sources
169170 if sources is not None :
170- if type (sources ) == str :
171+ if is_valid_string (sources ):
171172 payload ['sources' ] = sources
172173 else :
173174 raise TypeError ('sources param should be of type str' )
174175
175176 # Domains To Search
176177 if domains is not None :
177- if type (domains ) == str :
178+ if is_valid_string (domains ):
178179 payload ['domains' ] = domains
179180 else :
180181 raise TypeError ('domains param should be of type str' )
@@ -187,7 +188,7 @@ def get_everything(self, q=None, sources=None, domains=None, exclude_domains=Non
187188
188189 # Search From This Date ...
189190 if from_param is not None :
190- if type (from_param ) == str :
191+ if is_valid_string (from_param ):
191192 if (len (from_param )) >= 10 :
192193 for i in range (len (from_param )):
193194 if (i == 4 and from_param [i ] != '-' ) or (i == 7 and from_param [i ] != '-' ):
@@ -201,7 +202,7 @@ def get_everything(self, q=None, sources=None, domains=None, exclude_domains=Non
201202
202203 # ... To This Date
203204 if to is not None :
204- if type (to ) == str :
205+ if is_valid_string (to ):
205206 if (len (to )) >= 10 :
206207 for i in range (len (to )):
207208 if (i == 4 and to [i ] != '-' ) or (i == 7 and to [i ] != '-' ):
@@ -215,7 +216,7 @@ def get_everything(self, q=None, sources=None, domains=None, exclude_domains=Non
215216
216217 # Language
217218 if language is not None :
218- if type (language ) == str :
219+ if is_valid_string (language ):
219220 if language not in const .languages :
220221 raise ValueError ('invalid language' )
221222 else :
@@ -225,7 +226,7 @@ def get_everything(self, q=None, sources=None, domains=None, exclude_domains=Non
225226
226227 # Sort Method
227228 if sort_by is not None :
228- if type (sort_by ) == str :
229+ if is_valid_string (sort_by ):
229230 if sort_by in const .sort_method :
230231 payload ['sortBy' ] = sort_by
231232 else :
@@ -288,7 +289,7 @@ def get_sources(self, category=None, language=None, country=None):
288289
289290 # Language
290291 if language is not None :
291- if type (language ) == str :
292+ if is_valid_string (language ):
292293 if language in const .languages :
293294 payload ['language' ] = language
294295 else :
@@ -298,7 +299,7 @@ def get_sources(self, category=None, language=None, country=None):
298299
299300 # Country
300301 if country is not None :
301- if type (country ) == str :
302+ if is_valid_string (country ):
302303 if country in const .countries :
303304 payload ['country' ] = country
304305 else :
@@ -308,7 +309,7 @@ def get_sources(self, category=None, language=None, country=None):
308309
309310 # Category
310311 if category is not None :
311- if type (category ) == str :
312+ if is_valid_string (category ):
312313 if category in const .categories :
313314 payload ['category' ] = category
314315 else :
@@ -324,3 +325,12 @@ def get_sources(self, category=None, language=None, country=None):
324325 raise NewsAPIException (r .json ())
325326
326327 return r .json ()
328+
329+
330+ def is_valid_string (var ):
331+ if version_info [0 ] == 3 :
332+ return isinstance (var , str )
333+ elif version_info [0 ] == 2 :
334+ return isinstance (var , basestring )
335+ else :
336+ raise SystemError ("unsupported version of python detected (supported versions: 2, 3)" )
0 commit comments