Skip to content

Commit 488922f

Browse files
authored
Added helper string identification function
- All string parameters can now be passed as unicode or str in Python 2 - Helper function for above added to `newsapi_client.py`
1 parent 828b359 commit 488922f

1 file changed

Lines changed: 23 additions & 29 deletions

File tree

newsapi/newsapi_client.py

Lines changed: 23 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -46,14 +46,7 @@ def get_top_headlines(self, q=None, sources=None, language='en', country=None, c
4646

4747
# Keyword/Phrase
4848
if q is not None:
49-
if version_info[0] == 3:
50-
q_is_str = isinstance(q, str)
51-
elif version_info[0] == 2:
52-
q_is_str = isinstance(q, basestring)
53-
else:
54-
raise SystemError("unsupported version of python detected (supported versions: 2, 3)")
55-
56-
if q_is_str:
49+
if is_valid_string(q):
5750
payload['q'] = q
5851
else:
5952
raise TypeError('keyword/phrase q param should be of type str')
@@ -64,14 +57,14 @@ def get_top_headlines(self, q=None, sources=None, language='en', country=None, c
6457

6558
# Sources
6659
if sources is not None:
67-
if type(sources) == str:
60+
if is_valid_string(sources):
6861
payload['sources'] = sources
6962
else:
7063
raise TypeError('sources param should be of type str')
7164

7265
# Language
7366
if language is not None:
74-
if type(language) == str:
67+
if is_valid_string(language):
7568
if language in const.languages:
7669
payload['language'] = language
7770
else:
@@ -81,7 +74,7 @@ def get_top_headlines(self, q=None, sources=None, language='en', country=None, c
8174

8275
# Country
8376
if country is not None:
84-
if type(country) == str:
77+
if is_valid_string(country):
8578
if country in const.countries:
8679
payload['country'] = country
8780
else:
@@ -91,7 +84,7 @@ def get_top_headlines(self, q=None, sources=None, language='en', country=None, c
9184

9285
# Category
9386
if category is not None:
94-
if type(category) == str:
87+
if is_valid_string(category):
9588
if category in const.categories:
9689
payload['category'] = category
9790
else:
@@ -168,28 +161,21 @@ def get_everything(self, q=None, sources=None, domains=None, exclude_domains=Non
168161

169162
# Keyword/Phrase
170163
if q is not None:
171-
if version_info[0] == 3:
172-
q_is_str = isinstance(q, str)
173-
elif version_info[0] == 2:
174-
q_is_str = isinstance(q, basestring)
175-
else:
176-
raise SystemError("unsupported version of python detected (supported versions: 2, 3)")
177-
178-
if q_is_str:
164+
if is_valid_string(q):
179165
payload['q'] = q
180166
else:
181167
raise TypeError('keyword/phrase q param should be of type str')
182168

183169
# Sources
184170
if sources is not None:
185-
if type(sources) == str:
171+
if is_valid_string(sources):
186172
payload['sources'] = sources
187173
else:
188174
raise TypeError('sources param should be of type str')
189175

190176
# Domains To Search
191177
if domains is not None:
192-
if type(domains) == str:
178+
if is_valid_string(domains):
193179
payload['domains'] = domains
194180
else:
195181
raise TypeError('domains param should be of type str')
@@ -202,7 +188,7 @@ def get_everything(self, q=None, sources=None, domains=None, exclude_domains=Non
202188

203189
# Search From This Date ...
204190
if from_param is not None:
205-
if type(from_param) == str:
191+
if is_valid_string(from_param):
206192
if (len(from_param)) >= 10:
207193
for i in range(len(from_param)):
208194
if (i == 4 and from_param[i] != '-') or (i == 7 and from_param[i] != '-'):
@@ -216,7 +202,7 @@ def get_everything(self, q=None, sources=None, domains=None, exclude_domains=Non
216202

217203
# ... To This Date
218204
if to is not None:
219-
if type(to) == str:
205+
if is_valid_string(to):
220206
if (len(to)) >= 10:
221207
for i in range(len(to)):
222208
if (i == 4 and to[i] != '-') or (i == 7 and to[i] != '-'):
@@ -230,7 +216,7 @@ def get_everything(self, q=None, sources=None, domains=None, exclude_domains=Non
230216

231217
# Language
232218
if language is not None:
233-
if type(language) == str:
219+
if is_valid_string(language):
234220
if language not in const.languages:
235221
raise ValueError('invalid language')
236222
else:
@@ -240,7 +226,7 @@ def get_everything(self, q=None, sources=None, domains=None, exclude_domains=Non
240226

241227
# Sort Method
242228
if sort_by is not None:
243-
if type(sort_by) == str:
229+
if is_valid_string(sort_by):
244230
if sort_by in const.sort_method:
245231
payload['sortBy'] = sort_by
246232
else:
@@ -303,7 +289,7 @@ def get_sources(self, category=None, language=None, country=None):
303289

304290
# Language
305291
if language is not None:
306-
if type(language) == str:
292+
if is_valid_string(language):
307293
if language in const.languages:
308294
payload['language'] = language
309295
else:
@@ -313,7 +299,7 @@ def get_sources(self, category=None, language=None, country=None):
313299

314300
# Country
315301
if country is not None:
316-
if type(country) == str:
302+
if is_valid_string(country):
317303
if country in const.countries:
318304
payload['country'] = country
319305
else:
@@ -323,7 +309,7 @@ def get_sources(self, category=None, language=None, country=None):
323309

324310
# Category
325311
if category is not None:
326-
if type(category) == str:
312+
if is_valid_string(category):
327313
if category in const.categories:
328314
payload['category'] = category
329315
else:
@@ -339,3 +325,11 @@ def get_sources(self, category=None, language=None, country=None):
339325
raise NewsAPIException(r.json())
340326

341327
return r.json()
328+
329+
def is_valid_string(var):
330+
if version_info[0] == 3:
331+
return isinstance(var, str)
332+
elif version_info[0] == 2:
333+
return isinstance(var, basestring)
334+
else:
335+
raise SystemError("unsupported version of python detected (supported versions: 2, 3)")

0 commit comments

Comments
 (0)