Skip to content

Adding the authentication to be picked from a json format file in home folder. #1

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
.pyc
33 changes: 33 additions & 0 deletions auth.py
Original file line number Diff line number Diff line change
@@ -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()

55 changes: 28 additions & 27 deletions lymbix.py
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand All @@ -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)
Expand All @@ -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)
Expand All @@ -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()
return response.read()