Skip to content

Commit 2627499

Browse files
author
Steve Herschleb
committed
fix python 2 vs. 2 compatability issues
1 parent a3960b9 commit 2627499

1 file changed

Lines changed: 29 additions & 20 deletions

File tree

alchemyapi.py

Lines changed: 29 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,15 @@
11
#!/usr/bin/env python
22
from __future__ import print_function
3-
import urllib2
3+
4+
try:
5+
from urllib.request import urlopen
6+
from urllib.parse import urlparse
7+
from urllib.quote import urlquote
8+
except ImportError:
9+
from urlparse import urlparse
10+
from urllib2 import urlopen
11+
from urllib2 import quote as urlquote
12+
413
try:
514
import json
615
except ImportError:
@@ -22,7 +31,7 @@
2231
"""
2332

2433
import sys
25-
if sys.argv[1]:
34+
if len(sys.argv) == 2 and sys.argv[1]:
2635
if len(sys.argv[1]) == 40:
2736
#write the key to the file
2837
f = open('api_key.txt','w')
@@ -154,7 +163,7 @@ def sentiment(self, flavor, data, options={}):
154163
return { u'status':'ERROR', u'statusInfo':u'sentiment analysis for ' + flavor + ' not available' }
155164

156165
#add the URL encoded data to the options and analyze
157-
options[flavor] = urllib2.quote(data)
166+
options[flavor] = urlquote(data)
158167
return self.__analyze(AlchemyAPI.ENDPOINTS['sentiment'][flavor], options)
159168

160169

@@ -187,8 +196,8 @@ def sentiment_targeted(self, flavor, data, target, options={}):
187196
return { u'status':'ERROR', u'statusInfo':u'targeted sentiment analysis for ' + flavor + ' not available' }
188197

189198
#add the URL encoded data and target to the options and analyze
190-
options[flavor] = urllib2.quote(data)
191-
options['target'] = urllib2.quote(target)
199+
options[flavor] = urlquote(data)
200+
options['target'] = urlquote(target)
192201
return self.__analyze(AlchemyAPI.ENDPOINTS['sentiment_targeted'][flavor], options)
193202

194203

@@ -216,7 +225,7 @@ def author(self, flavor, data, options={}):
216225
return { u'status':'ERROR', u'statusInfo':u'author extraction for ' + flavor + ' not available' }
217226

218227
#add the URL encoded data to the options and analyze
219-
options[flavor] = urllib2.quote(data)
228+
options[flavor] = urlquote(data)
220229
return self.__analyze(AlchemyAPI.ENDPOINTS['author'][flavor], options)
221230

222231

@@ -246,7 +255,7 @@ def keywords(self, flavor, data, options={}):
246255
return { u'status':'ERROR', u'statusInfo':u'keyword extraction for ' + flavor + ' not available' }
247256

248257
#add the URL encoded data to the options and analyze
249-
options[flavor] = urllib2.quote(data)
258+
options[flavor] = urlquote(data)
250259
return self.__analyze(AlchemyAPI.ENDPOINTS['keywords'][flavor], options)
251260

252261

@@ -270,7 +279,7 @@ def concepts(self, flavor, data, options={}):
270279
return { u'status':'ERROR', u'statusInfo':u'concept tagging for ' + flavor + ' not available' }
271280

272281
#add the URL encoded data to the options and analyze
273-
options[flavor] = urllib2.quote(data)
282+
options[flavor] = urlquote(data)
274283
return self.__analyze(AlchemyAPI.ENDPOINTS['concepts'][flavor], options)
275284

276285

@@ -303,7 +312,7 @@ def entities(self, flavor, data, options={}):
303312
return { u'status':'ERROR', u'statusInfo':u'entity extraction for ' + flavor + ' not available' }
304313

305314
#add the URL encoded data to the options and analyze
306-
options[flavor] = urllib2.quote(data)
315+
options[flavor] = urlquote(data)
307316
return self.__analyze(AlchemyAPI.ENDPOINTS['entities'][flavor], options)
308317

309318

@@ -330,7 +339,7 @@ def category(self, flavor, data, options={}):
330339
return { u'status':'ERROR', u'statusInfo':u'text categorization for ' + flavor + ' not available' }
331340

332341
#add the URL encoded data to the options and analyze
333-
options[flavor] = urllib2.quote(data)
342+
options[flavor] = urlquote(data)
334343

335344
return self.__analyze(AlchemyAPI.ENDPOINTS['category'][flavor], options)
336345

@@ -367,7 +376,7 @@ def relations(self, flavor, data, options={}):
367376
return { u'status':'ERROR', u'statusInfo':u'relation extraction for ' + flavor + ' not available' }
368377

369378
#add the URL encoded data to the options and analyze
370-
options[flavor] = urllib2.quote(data)
379+
options[flavor] = urlquote(data)
371380
return self.__analyze(AlchemyAPI.ENDPOINTS['relations'][flavor], options)
372381

373382

@@ -394,7 +403,7 @@ def language(self, flavor, data, options={}):
394403
return { u'status':'ERROR', u'statusInfo':u'language detection for ' + flavor + ' not available' }
395404

396405
#add the URL encoded data to the options and analyze
397-
options[flavor] = urllib2.quote(data)
406+
options[flavor] = urlquote(data)
398407
return self.__analyze(AlchemyAPI.ENDPOINTS['language'][flavor], options)
399408

400409

@@ -422,7 +431,7 @@ def text_clean(self, flavor, data, options={}):
422431
return { u'status':'ERROR', u'statusInfo':u'clean text extraction for ' + flavor + ' not available' }
423432

424433
#add the URL encoded data to the options and analyze
425-
options[flavor] = urllib2.quote(data)
434+
options[flavor] = urlquote(data)
426435
return self.__analyze(AlchemyAPI.ENDPOINTS['text_clean'][flavor], options)
427436

428437

@@ -449,7 +458,7 @@ def text_raw(self, flavor, data, options={}):
449458
return { u'status':'ERROR', u'statusInfo':u'raw text extraction for ' + flavor + ' not available' }
450459

451460
#add the URL encoded data to the options and analyze
452-
options[flavor] = urllib2.quote(data)
461+
options[flavor] = urlquote(data)
453462
return self.__analyze(AlchemyAPI.ENDPOINTS['text_raw'][flavor], options)
454463

455464

@@ -477,7 +486,7 @@ def text_title(self, flavor, data, options={}):
477486
return { u'status':'ERROR', u'statusInfo':u'title extraction for ' + flavor + ' not available' }
478487

479488
#add the URL encoded data to the options and analyze
480-
options[flavor] = urllib2.quote(data)
489+
options[flavor] = urlquote(data)
481490
return self.__analyze(AlchemyAPI.ENDPOINTS['text_title'][flavor], options)
482491

483492

@@ -505,7 +514,7 @@ def microformats(self, flavor, data, options={}):
505514
return { u'status':'ERROR', u'statusInfo':u'microformat extraction for ' + flavor + ' not available' }
506515

507516
#add the URL encoded data to the options and analyze
508-
options[flavor] = urllib2.quote(data)
517+
options[flavor] = urlquote(data)
509518
return self.__analyze(AlchemyAPI.ENDPOINTS['microformats'][flavor], options)
510519

511520

@@ -533,7 +542,7 @@ def feeds(self, flavor, data, options={}):
533542
return { u'status':'ERROR', u'statusInfo':u'feed detection for ' + flavor + ' not available' }
534543

535544
#add the URL encoded data to the options and analyze
536-
options[flavor] = urllib2.quote(data)
545+
options[flavor] = urlquote(data)
537546
return self.__analyze(AlchemyAPI.ENDPOINTS['feeds'][flavor], options)
538547

539548

@@ -562,9 +571,9 @@ def __analyze(self, url, options):
562571

563572
try:
564573
#build the request with uri encoding
565-
req = urllib2.Request(url)
566-
f = urllib2.urlopen(req)
567-
return json.load(f)
574+
req = urlparse(url)
575+
page = urlopen(req.geturl())
576+
return json.load(page)
568577
except Exception as e:
569578
print('error for url: ', url)
570579
print(e)

0 commit comments

Comments
 (0)