Commits

Anonymous committed f97f182

Updated python client to only use the json version of the api.

  • Participants
  • Parent commits 95ab632

Comments (0)

Files changed (2)

 Requirements:
 - Python
 - A json library (builtin or simplejson works fine)
-- lxml (http://lxml.de/)
 
 Want to change it / improve it / share it? Go for it.
 
 More documentation available at http://www.repustate.com/docs
 """
 import base64
-from lxml import etree
 import urllib
 import urllib2
 
 class Repustate(object):
 
     url_template = (
-        'http://api.repustate.com/%(version)s/%(key)s/%(function)s.%(response_type)s'
+        'http://api.repustate.com/%(version)s/%(key)s/%(function)s.json'
         )
 
     ppt_url = 'http://api.repustate.com/%(version)s/%(key)s/powerpoint/'
         self.api_key = api_key
         self.version = version
 
-    def _call_api(self, api_function, use_http_get=False, response_type='json', **params):
-
-        assert response_type in ('json', 'xml'), "%s is not a valid response type" % response_type
-
+    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(
-            response_type=response_type,
             function=api_function,
             key=self.api_key,
             version=self.version,
             else:
                 response = urllib2.urlopen(url, data)
 
-            if response_type == 'json':
-                result = json.load(response)
-            else:
-                result = etree.fromstring(response.read())
+            result = json.load(response)
 
         return result
 
     def _call_natural_language(self, api_function, cloud=None, text=None,
-                               url=None, response_type=None):
+                               url=None):
         """
         Helper function for the NLP calls.
         """
-        return self._call_api(api_function, cloud=cloud, text=text, url=url,
-                              response_type=response_type)
+        return self._call_api(api_function, cloud=cloud, text=text, url=url)
 
-    def sentiment(self, text=None, url=None, response_type='json'):
+    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,
-                              response_type=response_type)
+        return self._call_api('score', text=text, url=url)
 
-    def bulk_sentiment(self, items=None, response_type='json'):
+    def bulk_sentiment(self, items=None):
         """
         Bulk score multiple pieces of text (not urls!).
         """
         for idx, item in enumerate(items):
             items_to_score['text%d' % idx] = item
 
-        return self._call_api('bulk-score', response_type=response_type,
-                              **items_to_score)
+        return self._call_api('bulk-score', **items_to_score)
 
-    def clean_html(self, url=None, response_type='json'):
+    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,
-                              response_type=response_type)
+        return self._call_api('clean-html', use_http_get=True, url=url)
 
-    def adjectives(self, cloud=None, text=None, url=None, response_type='json'):
+    def adjectives(self, cloud=None, text=None, url=None):
         return self._call_natural_language('adj', cloud=cloud, text=text,
-                                           url=url, response_type=response_type)
+                                           url=url)
 
-    def verbs(self, cloud=None, text=None, url=None, response_type='json'):
+    def verbs(self, cloud=None, text=None, url=None):
         return self._call_natural_language('verb', cloud=cloud, text=text,
-                                           url=url, response_type=response_type)
+                                           url=url)
 
     def ngrams(self, url=None, text=None, max=None, min=None, freq=None,
-               stopwords=None, response_type='json'):
+               stopwords=None):
         return self._call_api('ngrams', use_http_get=bool(url), text=text,
                               url=url, max=max, min=min, freq=freq,
-                              stopwords=stopwords, response_type=response_type)
+                              stopwords=stopwords)
 
-    def date_extraction(self, text, response_type='json'):
+    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,
-                              response_type=response_type)
+        return self._call_api('extract-dates', text=text)
 
     def powerpoint(self, report_title, author, images, titles):
         """
     client = Repustate(api_key='demokey', version='v2')
 
     # Score a single piece of text.
-    score = client.sentiment(text='I hate food.', response_type='txt')
+    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'], response_type='xml')
+    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

python/test_repustate.py

     def get_accessor(self):
         return Repustate(self.key, 'v2')
 
-    def check_status(self, result, response_type):
-        if response_type == 'json':
-            expected_type = dict
-            status = result.get('status')
-        else:
-            expected_type = etree._Element
-            status = result.findtext('status')
+    def check_status(self, result):
+        expected_type = dict
+        status = result.get('status')
         self.assertIsInstance(result, expected_type, 'Invalid result')
         self.assertEqual(status, 'OK')
 
         result = func(**kwargs)
         self.assertIsNotNone(result, 'Invalid result')
         if check_status:
-            self.check_status(result, kwargs.get('response_type', 'json'))
-
-    def test_xml_response_type(self):
-        self.check_call('sentiment', text=self.text, response_type='xml')
+            self.check_status(result)
 
     def test_sentiment_for_text(self):
         self.check_call('sentiment', text=self.text)