Exemplo n.º 1
0
 def __init__(self, api_url='http://api.tineye.com/rest/', public_key=None, private_key=None):
     self.http = urllib3.connection_from_url(api_url)
     self.request = APIRequest(api_url, public_key, private_key)
Exemplo n.º 2
0
 def __init__(self,
              api_url='https://api.tineye.com/rest/',
              public_key=None,
              private_key=None):
     self.http = urllib3.connection_from_url(api_url)
     self.request = APIRequest(api_url, public_key, private_key)
Exemplo n.º 3
0
class TinEyeAPIRequest(object):
    """
    Class to ease communication with the TinEye API server. 

    Establish a connection to the API:

        >>> from pytineye import TinEyeAPIRequest
        >>> api = TinEyeAPIRequest('http://api.tineye.com/rest/', 'your_public_key', 'your_private_key')

    Searching for an image using an image URL:

        >>> api.search_url(url='http://www.tineye.com/images/meloncat.jpg')
        TinEyeResponse(...)

    Searching for an image using image data:

        >>> fp = open('meloncat.jpg', 'rb')
        >>> data = fp.read()
        >>> api.search_data(data=data)
        TinEyeResponse(...)
        >>> fp.close()

    Getting information about your search bundle:

        >>> api.remaining_searches()
        {'expire_date': datetime.datetime(2012, 9, 28, 11, 11, 31),
         'remaining_searches': 854,
         'start_date': datetime.datetime(2011, 9, 29, 11, 11, 31)}

    Getting an image count:

        >>> api.image_count()
        2180913080

    """

    def __init__(self, api_url='http://api.tineye.com/rest/', public_key=None, private_key=None):
        self.http = urllib3.connection_from_url(api_url)
        self.request = APIRequest(api_url, public_key, private_key)

    def _request(self, method, params=None, image_file=None, **kwargs):
        """
        Send request to API and process results.

        - `method`, API method to call.
        - `params`, dictionary of fields to send to the API call.
        - `image_file`, tuple containing info (filename, data) about image to send.

        Returns: a JSON parsed object.
        """

        # Pass in any extra keyword arguments as parameters to the API call
        if not params:
            params = {}
        params.update(kwargs)

        try:
            obj = None
            response = None

            # If an image file was provided, send a POST request, else send a GET request
            if image_file == None:
                request_string = self.request.get_request(method, params)
                response = self.http.request('GET', request_string)
            else:
                filename = image_file[0]
                request_string, boundary = self.request.post_request(method, filename, params)
                response = self.http.request_encode_body('POST', request_string, 
                                                         fields={'image_upload': image_file},
                                                         multipart_boundary=boundary)
            # Parse the JSON into a Python object
            obj = simplejson.loads(response.data)

        except simplejson.decoder.JSONDecodeError, e:
            raise TinEyeAPIError("500", ["Could not decode JSON: %s" % e])
        except Exception, e:
            raise e
Exemplo n.º 4
0
class TinEyeAPIRequest(object):
    """
    Class to ease communication with the TinEye API server.

    Establish a connection to the API:

        >>> from pytineye import TinEyeAPIRequest
        >>> api = TinEyeAPIRequest(
        ...     'https://api.tineye.com/rest/',
        ...     'your_public_key',
        ...     'your_private_key')

    Searching for an image using an image URL:

        >>> api.search_url(url='http://tineye.com/images/meloncat.jpg')
        TinEyeResponse(...)

    Searching for an image using image data:

        >>> fp = open('meloncat.jpg', 'rb')
        >>> data = fp.read()
        >>> api.search_data(data=data)
        TinEyeResponse(...)
        >>> fp.close()

    Getting information about your search bundle:

        >>> api.remaining_searches()
        {'expire_date': datetime.datetime(2012, 9, 28, 11, 11, 31),
         'remaining_searches': 854,
         'start_date': datetime.datetime(2011, 9, 29, 11, 11, 31)}

    Getting an image count:

        >>> api.image_count()
        2180913080

    """
    def __init__(self,
                 api_url='https://api.tineye.com/rest/',
                 public_key=None,
                 private_key=None):
        self.http = urllib3.connection_from_url(api_url)
        self.request = APIRequest(api_url, public_key, private_key)

    def _request(self, method, params=None, image_file=None, **kwargs):
        """
        Send request to API and process results.

        - `method`, API method to call.
        - `params`, dictionary of fields to send to the API call.
        - `image_file`, tuple containing info (filename, data) about image to send.

        Returns: a JSON parsed object.
        """

        # Pass in any extra keyword arguments as parameters to the API call
        if not params:
            params = {}
        params.update(kwargs)

        try:
            obj = None
            response = None

            # If an image file was provided, send a POST request, else send a GET request
            if image_file is None:
                request_string = self.request.get_request(method, params)
                response = self.http.request('GET', request_string)
            else:
                filename = image_file[0]
                request_string, boundary = self.request.post_request(
                    method, filename, params)
                response = self.http.request_encode_body(
                    'POST',
                    request_string,
                    fields={'image_upload': image_file},
                    multipart_boundary=boundary)
            # Parse the JSON into a Python object
            obj = simplejson.loads(response.data)

        except simplejson.decoder.JSONDecodeError, e:
            raise TinEyeAPIError("500", ["Could not decode JSON: %s" % e])
        except Exception, e:
            raise e