From b3ab7091f89eedf26a9937a5a3cbea9af580bc5f Mon Sep 17 00:00:00 2001 From: Rahul Ravindran Date: Thu, 24 Mar 2016 09:51:57 -0700 Subject: [PATCH] fix: adding timeout variable for disconnection due to long intervals. --- alchemyapi.py | 17 +++++++++++++---- 1 file changed, 13 insertions(+), 4 deletions(-) diff --git a/alchemyapi.py b/alchemyapi.py index 34ded5b..3abc0fb 100644 --- a/alchemyapi.py +++ b/alchemyapi.py @@ -1,6 +1,6 @@ #!/usr/bin/env python -# Copyright 2013 AlchemyAPI +# Copyright 2013 AlchemyAPI # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. @@ -136,8 +136,11 @@ class AlchemyAPI: s = requests.Session() + # Timeout for the request sent to api + timeout = None + def __init__(self): - """ + """ Initializes the SDK so it can send requests to AlchemyAPI for analysis. It loads the API key from api_key.txt and configures the endpoints. """ @@ -179,6 +182,12 @@ def __init__(self): except Exception as e: print(e) + def setTimeout(self,interval): + """ + Sets the timeout for the request parameter. + """ + self.timeout = interval + def entities(self, flavor, data, options={}): """ Extracts the entities for text, a URL or HTML. @@ -302,7 +311,7 @@ def sentiment_targeted(self, flavor, data, target, options={}): options -> various parameters that can be used to adjust how the API works, see below for more info on the available options. Available Options: - showSourceText -> 0: disabled, 1: enabled + showSourceText -> 0: disabled, 1: enabled OUTPUT: The response, already converted from JSON to a Python object. @@ -767,7 +776,7 @@ def __analyze(self, endpoint, params, post_data=bytearray()): results = "" try: - results = self.s.post(url=post_url, data=post_data) + results = self.s.post(url=post_url, data=post_data, timeout = self.timeout) except Exception as e: print(e) return {'status': 'ERROR', 'statusInfo': 'network-error'}