Skip to content

Commit 2bf30ba

Browse files
committed
Fixed POST data being passed in URL and broke out JSON parsing from POST
request in order to get errors passed through correctly.
1 parent c212b7d commit 2bf30ba

File tree

2 files changed

+21
-18
lines changed

2 files changed

+21
-18
lines changed

alchemyapi.py

Lines changed: 20 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -202,7 +202,7 @@ def entities(self, flavor, data, options={}):
202202

203203
#add the data to the options and analyze
204204
options[flavor] = data
205-
return self.__analyze(AlchemyAPI.ENDPOINTS['entities'][flavor], options)
205+
return self.__analyze(AlchemyAPI.ENDPOINTS['entities'][flavor], {}, options)
206206

207207

208208

@@ -233,7 +233,7 @@ def keywords(self, flavor, data, options={}):
233233

234234
#add the data to the options and analyze
235235
options[flavor] = data
236-
return self.__analyze(AlchemyAPI.ENDPOINTS['keywords'][flavor], options)
236+
return self.__analyze(AlchemyAPI.ENDPOINTS['keywords'][flavor], {}, options)
237237

238238

239239

@@ -258,7 +258,7 @@ def concepts(self, flavor, data, options={}):
258258

259259
#add the data to the options and analyze
260260
options[flavor] = data
261-
return self.__analyze(AlchemyAPI.ENDPOINTS['concepts'][flavor], options)
261+
return self.__analyze(AlchemyAPI.ENDPOINTS['concepts'][flavor], {}, options)
262262

263263

264264

@@ -286,7 +286,7 @@ def sentiment(self, flavor, data, options={}):
286286

287287
#add the data to the options and analyze
288288
options[flavor] = data
289-
return self.__analyze(AlchemyAPI.ENDPOINTS['sentiment'][flavor], options)
289+
return self.__analyze(AlchemyAPI.ENDPOINTS['sentiment'][flavor], {}, options)
290290

291291

292292

@@ -320,7 +320,7 @@ def sentiment_targeted(self, flavor, data, target, options={}):
320320
#add the URL encoded data and target to the options and analyze
321321
options[flavor] = data
322322
options['target'] = target
323-
return self.__analyze(AlchemyAPI.ENDPOINTS['sentiment_targeted'][flavor], options)
323+
return self.__analyze(AlchemyAPI.ENDPOINTS['sentiment_targeted'][flavor], {}, options)
324324

325325

326326

@@ -377,7 +377,7 @@ def text_raw(self, flavor, data, options={}):
377377

378378
#add the data to the options and analyze
379379
options[flavor] = data
380-
return self.__analyze(AlchemyAPI.ENDPOINTS['text_raw'][flavor], options)
380+
return self.__analyze(AlchemyAPI.ENDPOINTS['text_raw'][flavor], {}, options)
381381

382382

383383

@@ -405,7 +405,7 @@ def author(self, flavor, data, options={}):
405405

406406
#add the data to the options and analyze
407407
options[flavor] = data
408-
return self.__analyze(AlchemyAPI.ENDPOINTS['author'][flavor], options)
408+
return self.__analyze(AlchemyAPI.ENDPOINTS['author'][flavor], {}, options)
409409

410410

411411

@@ -433,7 +433,7 @@ def language(self, flavor, data, options={}):
433433

434434
#add the data to the options and analyze
435435
options[flavor] = data
436-
return self.__analyze(AlchemyAPI.ENDPOINTS['language'][flavor], options)
436+
return self.__analyze(AlchemyAPI.ENDPOINTS['language'][flavor], {}, options)
437437

438438

439439

@@ -461,7 +461,7 @@ def title(self, flavor, data, options={}):
461461

462462
#add the data to the options and analyze
463463
options[flavor] = data
464-
return self.__analyze(AlchemyAPI.ENDPOINTS['title'][flavor], options)
464+
return self.__analyze(AlchemyAPI.ENDPOINTS['title'][flavor], {}, options)
465465

466466

467467

@@ -498,7 +498,7 @@ def relations(self, flavor, data, options={}):
498498

499499
#add the data to the options and analyze
500500
options[flavor] = data
501-
return self.__analyze(AlchemyAPI.ENDPOINTS['relations'][flavor], options)
501+
return self.__analyze(AlchemyAPI.ENDPOINTS['relations'][flavor], {}, options)
502502

503503

504504

@@ -527,7 +527,7 @@ def category(self, flavor, data, options={}):
527527
#add the data to the options and analyze
528528
options[flavor] = data
529529

530-
return self.__analyze(AlchemyAPI.ENDPOINTS['category'][flavor], options)
530+
return self.__analyze(AlchemyAPI.ENDPOINTS['category'][flavor], {}, options)
531531

532532

533533

@@ -555,7 +555,7 @@ def feeds(self, flavor, data, options={}):
555555

556556
#add the data to the options and analyze
557557
options[flavor] = data
558-
return self.__analyze(AlchemyAPI.ENDPOINTS['feeds'][flavor], options)
558+
return self.__analyze(AlchemyAPI.ENDPOINTS['feeds'][flavor], {}, options)
559559

560560

561561

@@ -583,7 +583,7 @@ def microformats(self, flavor, data, options={}):
583583

584584
#add the data to the options and analyze
585585
options[flavor] = data
586-
return self.__analyze(AlchemyAPI.ENDPOINTS['microformats'][flavor], options)
586+
return self.__analyze(AlchemyAPI.ENDPOINTS['microformats'][flavor], {}, options)
587587

588588
def imageExtraction(self, flavor, data, options={}):
589589
"""
@@ -605,7 +605,7 @@ def imageExtraction(self, flavor, data, options={}):
605605
if flavor not in AlchemyAPI.ENDPOINTS['image']:
606606
return { 'status':'ERROR', 'statusInfo':'image extraction for ' + flavor + ' not available' }
607607
options[flavor] = data
608-
return self.__analyze(AlchemyAPI.ENDPOINTS['image'][flavor], options)
608+
return self.__analyze(AlchemyAPI.ENDPOINTS['image'][flavor], {}, options)
609609

610610
def taxonomy(self, flavor, data, options={}):
611611
"""
@@ -656,7 +656,7 @@ def taxonomy(self, flavor, data, options={}):
656656
if flavor not in AlchemyAPI.ENDPOINTS['taxonomy']:
657657
return { 'status':'ERROR', 'statusInfo':'taxonomy for ' + flavor + ' not available' }
658658
options[flavor] = data
659-
return self.__analyze(AlchemyAPI.ENDPOINTS['taxonomy'][flavor], options)
659+
return self.__analyze(AlchemyAPI.ENDPOINTS['taxonomy'][flavor], {}, options)
660660

661661
def combined(self, flavor, data, options={}):
662662
"""
@@ -722,7 +722,7 @@ def combined(self, flavor, data, options={}):
722722
if flavor not in AlchemyAPI.ENDPOINTS['combined']:
723723
return { 'status':'ERROR', 'statusInfo':'combined for ' + flavor + ' not available' }
724724
options[flavor] = data
725-
return self.__analyze(AlchemyAPI.ENDPOINTS['combined'][flavor], options)
725+
return self.__analyze(AlchemyAPI.ENDPOINTS['combined'][flavor], {}, options)
726726

727727
def imageTagging(self, flavor, data, options={}):
728728
"""
@@ -740,7 +740,7 @@ def imageTagging(self, flavor, data, options={}):
740740
return self.__analyze(AlchemyAPI.ENDPOINTS['imagetagging'][flavor], options, image)
741741

742742
options[flavor] = data
743-
return self.__analyze(AlchemyAPI.ENDPOINTS['imagetagging'][flavor], options)
743+
return self.__analyze(AlchemyAPI.ENDPOINTS['imagetagging'][flavor], {}, options)
744744

745745

746746
def __analyze(self, endpoint, params, post_data=bytearray()):
@@ -763,7 +763,9 @@ def __analyze(self, endpoint, params, post_data=bytearray()):
763763
post_url = AlchemyAPI.BASE_URL + endpoint + '?' + urlencode(params).encode('utf-8');
764764

765765
try:
766-
return self.s.post(url=post_url, data=post_data).json()
766+
results = self.s.post(url=post_url, data=post_data)
767+
return results.json()
767768
except Exception as e:
768769
print(e)
769770
return { 'status':'ERROR', 'statusInfo':'network-error' }
771+

example.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121

2222

2323
demo_text = 'Yesterday dumb Bob destroyed my fancy iPhone in beautiful Denver, Colorado. I guess I will have to head over to the Apple Store and buy a new one.'
24+
2425
demo_url = 'http://www.npr.org/2013/11/26/247336038/dont-stuff-the-turkey-and-other-tips-from-americas-test-kitchen'
2526
demo_html = '<html><head><title>Python Demo | AlchemyAPI</title></head><body><h1>Did you know that AlchemyAPI works on HTML?</h1><p>Well, you do now.</p></body></html>'
2627
image_url = 'http://demo1.alchemyapi.com/images/vision/football.jpg'

0 commit comments

Comments
 (0)