diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..15584ca --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +.pyc diff --git a/auth.py b/auth.py new file mode 100644 index 0000000..734da4c --- /dev/null +++ b/auth.py @@ -0,0 +1,33 @@ +# -*- coding: utf-8 -*- +# vi:si:et:sw=4:sts=4:ts=4 +# GPL 2009 +# Happily copied from https://launchpad.net/python-ox +# +import os + +def get(key): + user_auth = os.environ.get('oxAUTH', os.path.expanduser('~/.ox/auth.json')) + auth = {} + if os.path.exists(user_auth): + f = open(user_auth, "r") + data = f.read() + f.close() + auth = json.loads(data) + if key in auth: + return auth[key] + print "please add key %s to json file '%s'" % (key, user_auth) + raise Exception,"no key %s found" % key + +def update(key, value): + user_auth = os.environ.get('oxAUTH', os.path.expanduser('~/.ox/auth.json')) + auth = {} + if os.path.exists(user_auth): + f = open(user_auth, "r") + data = f.read() + f.close() + auth = json.loads(data) + auth[key] = value + f = open(user_auth, "w") + f.write(json.dumps(auth, indent=2)) + f.close() + diff --git a/lymbix.py b/lymbix.py index deb4da8..a689f78 100644 --- a/lymbix.py +++ b/lymbix.py @@ -2,8 +2,9 @@ import urllib2 import json +import auth class Lymbix: - + API_BASE = 'https://gyrus.lymbix.com/'; TONALIZE_MULTIPLE = 'tonalize_multiple'; TONALIZE_DETAILED = 'tonalize_detailed'; @@ -16,41 +17,41 @@ def __init__(self, authentication_key): -authentication_key: your Lymbix authentication key ''' if authentication_key == None or len(authentication_key) == 0: - raise Exception('You must include your authentication key') - + #raise Exception('You must include your authentication key') + self.authentication_key = auth.get('lymbix_api_key') self.authentication_key = authentication_key - + ''' utility functions ''' - + def _get_headers(self): headers = { 'Authentication': self.authentication_key, 'Accept': 'application/json', 'Version': '2.2'} return headers - + ''' api methods ''' - + def tonalize_multiple(self, articles, options = None): ''' tonalize multiple articles - + Args: -articles: articles to tonalize -options: additional parameters (reference_ids and return_fields) - + Returns: -see the api documentation for the format of this object ''' if articles == None or len(articles) == 0: raise Exception('You must include articles to tonalize') - + url = self.API_BASE + self.TONALIZE_MULTIPLE data = {'articles': articles} if options != None: data.update(options) for key, value in data.iteritems(): data[key] = json.dumps(value) data = urllib.urlencode(data) - + headers = self._get_headers() request = urllib2.Request(url, data, headers) response = urllib2.urlopen(request) @@ -59,48 +60,48 @@ def tonalize_multiple(self, articles, options = None): def tonalize_detailed(self, article, options = None): ''' tonalize an article - + Args: -article: article to tonalize -options: additional parameters (reference_id and return_fields) - + Returns: -see the api documentation for the format of this object ''' if article == None or len(article) == 0: raise Exception('You must include an article to tonalize') - + url = self.API_BASE + self.TONALIZE_DETAILED data = {'article': article} if options != None: data.update(options) for key, value in data.iteritems(): data[key] = json.dumps(value) data = urllib.urlencode(data) - + headers = self._get_headers() request = urllib2.Request(url, data, headers) response = urllib2.urlopen(request) return json.loads(response.read()) - + def tonalize(self, article, options = None): ''' tonalize an article - + Args: -article: article to tonalize -options: additional parameters (reference_id and return_fields) - + Returns: -see the api documentation for the format of this object ''' if article == None or len(article) == 0: raise Exception('You must include an article to tonalize') - + url = self.API_BASE + self.TONALIZE data = {'article': article} if options != None: data.update(options) for key, value in data.iteritems(): data[key] = json.dumps(value) data = urllib.urlencode(data) - + headers = self._get_headers() request = urllib2.Request(url, data, headers) response = urllib2.urlopen(request) @@ -109,32 +110,32 @@ def tonalize(self, article, options = None): def flag_response(self, phrase, api_method = None, api_version = '2.2', callback_url = None, options = None): ''' flag a response as inaccurate - + Args: -phrase: the phrase that returns an inaccurate response -api_method: the method that returns an inaccurate response -api_version: the version that returns an inaccurate response -callback_url: a url to call when the phrase has been re-rated -options: additional parameters (reference_id) - + Returns: -see the api documentation for the format of this object ''' if phrase == None or len(phrase) == 0: raise Exception('You must include a phrase to flag') - + url = self.API_BASE + self.FLAG_RESPONSE data = {'phrase': phrase} - + if (api_method != None): data['apiMethod'] = api_method if (api_version != None): data['apiVersion'] = api_version if (callback_url != None): data['callbackUrl'] = callback_url if (options != None): data.update(options) - + for key, value in data.iteritems(): data[key] = json.dumps(value) data = urllib.urlencode(data) - + headers = self._get_headers() request = urllib2.Request(url, data, headers) response = urllib2.urlopen(request) - return response.read() \ No newline at end of file + return response.read()