Repustate Clients / python /

Repustate Python API client.

- Python
- A json library (builtin or simplejson works fine)

Want to change it / improve it / share it? Go for it.

Feedback is appreciated at

More documentation available at
import base64
import urllib
import urllib2

    # Different versions of Python call the json library different things.
    import json
except ImportError:
    import simplejson as json

class Repustate(object):

    url_template = (

    ppt_url = ''

    def __init__(self, api_key, version='v2'):
        self.api_key = api_key
        self.version = version

    def _call_api(self, api_function, use_http_get=False, **params):
        params = dict((x, y) for x, y in params.iteritems() if y is not None)

        data = urllib.urlencode(params)

        url_args = dict(

        if api_function == 'powerpoint':
            url = self.ppt_url % url_args
            response = urllib2.urlopen(url, data)
            # Return the ppt file as binary data.
            result =
            url = self.url_template % url_args

            if use_http_get:
                url = '%s?%s' % (url, data)
                response = urllib2.urlopen(url)
                response = urllib2.urlopen(url, data)

            result = json.load(response)

        return result

    def _call_natural_language(self, api_function, cloud=None, text=None,
        Helper function for the NLP calls.
        return self._call_api(api_function, cloud=cloud, text=text, url=url)

    def sentiment(self, text=None, url=None):
        Retrieve the sentiment for a single URl or block of text.
        return self._call_api('score', text=text, url=url)

    def bulk_sentiment(self, items=None):
        Bulk score multiple pieces of text (not urls!).
        items_to_score = {}

        for idx, item in enumerate(items):
            items_to_score['text%d' % idx] = item

        return self._call_api('bulk-score', **items_to_score)

    def clean_html(self, url=None):
        Clean up a web page. It doesn't work well on home pages - it's designed for content pages.
        return self._call_api('clean-html', use_http_get=True, url=url)

    def adjectives(self, cloud=None, text=None, url=None):
        return self._call_natural_language('adj', cloud=cloud, text=text,

    def verbs(self, cloud=None, text=None, url=None):
        return self._call_natural_language('verb', cloud=cloud, text=text,

    def ngrams(self, url=None, text=None, max=None, min=None, freq=None,
        return self._call_api('ngrams', use_http_get=bool(url), text=text,
                              url=url, max=max, min=min, freq=freq,

    def date_extraction(self, text):
        Convert english date indicators like "today", "tomorrow", "next week"
        into date strings like 2011-01-12.
        return self._call_api('extract-dates', text=text)

    def powerpoint(self, report_title, author, images, titles):
        Given a list of images and titles, generate a simple powerpoint presentation.
        kwargs = dict(

        for idx, (image, title) in enumerate(zip(images, titles)):
            # We need to b64 encode the image.
            image_content = base64.b64encode(open(image).read())
            kwargs['slide_%d_image' % idx] = image_content
            kwargs['slide_%d_title' % idx] = title

        return self._call_api('powerpoint', **kwargs)

if __name__ == '__main__':
    Sample usage of the client library. You'll have to change the api_key below
    to yours if you want to actually run this.
    client = Repustate(api_key='demokey', version='v2')

    # Score a single piece of text.
    score = client.sentiment(text='I hate food.')

    # Score multiple pieces of text.
    scores = client.bulk_sentiment(['I love candy', 'I hate fish', 'I want to go watch a movie'])

    # To make this example work, you'll have to have an image called 'test.jpg'
    # in this directory. A new power point presentation called "Test.ppt" will
    # be saved in the current directory.
    data = client.powerpoint('Test Report', 'Martin Ostrovsky', images=['test.jpg'], titles=['A test title'])
    fd = open('Test.ppt', 'w')