Skip to content

Commit

Permalink
Change api key configuration
Browse files Browse the repository at this point in the history
  • Loading branch information
jrrodri committed Feb 22, 2019
1 parent 1ed671a commit a7ae2c8
Show file tree
Hide file tree
Showing 10 changed files with 41 additions and 52 deletions.
15 changes: 11 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ quality and maximize the compression.
* Smart crop and resize images with our saliency and aesthetic based model
which balances between content and aesthetics.

```
```sh
abraia optimize --width 800 --height 400 https://images.pexels.com/photos/700948/pexels-photo-700948.jpeg images/skater.jpg
```

Expand Down Expand Up @@ -44,13 +44,20 @@ Verify that the abraia CLI is correctly installed:
abraia --version
```

Finally, configure your API Keys using the command bellow:
If you get a message of command not found try to uninstall the package and
install it again globally:

```sh
abraia configure
pip uninstall abraia
sudo pip install abraia
```

You can [create a free account](https://abraia.me/login) to get your API Keys.
Finally, configure your [free API Key](https://abraia.me/docs/getting-started)
using the command bellow:

```sh
abraia configure
```

## Command line interface

Expand Down
4 changes: 2 additions & 2 deletions abraia/__init__.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
from . import config
from .client import Client, APIError
from .abraia import from_file, from_url, list
from .abraia import from_file, from_url, from_store, list

__all__ = ['config', 'Client', 'APIError', 'from_file', 'from_url', 'list']
__all__ = ['config', 'Client', 'APIError', 'from_file', 'from_url', 'from_store', 'list']
5 changes: 3 additions & 2 deletions abraia/abraia.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@


def list(path=''):
return Abraia().list_files(path=path)
abraia = Abraia()
return abraia.list_files(path=abraia.userid+'/'+path)


def from_file(filename):
Expand All @@ -22,7 +23,7 @@ def from_store(path):
class Abraia(Client):
def __init__(self):
super(Abraia, self).__init__()
self.userid = self.check()
self.userid = self.load_user()['user']['id']
self.path = ''
self.params = {}

Expand Down
4 changes: 0 additions & 4 deletions abraia/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,6 @@ class Client(object):
def __init__(self):
self.auth = config.load_auth()

def check(self):
files, folders = self.list_files()
return folders[0]['name']

def load_user(self):
url = '{}/users'.format(config.API_URL)
resp = requests.get(url, auth=self.auth)
Expand Down
18 changes: 12 additions & 6 deletions abraia/config.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import os
import base64

API_URL = 'https://api.abraia.me'
CONFIG_FILE = os.path.join(os.path.expanduser('~'), '.abraia')
Expand All @@ -7,21 +8,26 @@
'jpeg': 'image/jpeg',
'png': 'image/png',
'gif': 'image/gif',
'svg': 'image/svg+xml',
'webp': 'image/webp',
'bmp': 'image/bmp',
'pdf': 'application/pdf'}
'pdf': 'application/pdf',
'psd': 'image/vnd.adobe.photoshop'}


def load_auth():
api_key = os.environ.get('ABRAIA_API_KEY')
api_secret = os.environ.get('ABRAIA_API_SECRET')
config = {'abraia_api_key': api_key, 'abraia_api_secret': api_secret}
if os.path.isfile(CONFIG_FILE) and (api_key is None or api_secret is None):
abraia_key = os.environ.get('ABRAIA_KEY')
if os.path.isfile(CONFIG_FILE) and (abraia_key is None):
config = {}
with open(CONFIG_FILE, 'r') as f:
for line in f:
key, value = list(map(lambda v: v.strip(), line.split(':')))
config[key] = value
return config['abraia_api_key'], config['abraia_api_secret']
return config['abraia_api_key'], config['abraia_api_secret']
elif abraia_key:
api_key, api_secret = base64.b64decode(abraia_key).split(':')
return api_key, api_secret
return '', ''


def save_auth(api_key, api_secret):
Expand Down
Binary file modified images/skater.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
39 changes: 9 additions & 30 deletions scripts/abraia
Original file line number Diff line number Diff line change
Expand Up @@ -5,36 +5,29 @@ from builtins import input
import os
import sys
import shutil
import base64
import argparse
from tqdm import tqdm
from datetime import datetime

import abraia

IMAGE_EXTS = ['.jpg', '.jpeg', '.png', '.gif', '.webp', '.svg']
IMAGE_EXTS = ['.jpg', '.jpeg', '.png', '.gif', '.webp', '.svg', '.psd']


def process_info():
print('Abraia CLI v0.4.1')
print('Abraia CLI v0.4.2')


def process_configure():
api_key, api_secret = abraia.config.load_auth()
key = input('Abraia Api Key [{}]: '.format(api_key))
api_key = api_key if key == '' else key
secret = input('Abraia Api Secret [{}]: '.format(api_secret))
api_secret = api_secret if secret == '' else secret
abraia_key = '{}:{}'.format(api_key, api_secret).encode('base64') if api_key and api_secret else ''
key = input('Abraia Key [{}]: '.format(abraia_key))
abraia_key = abraia_key if key == '' else key
api_key, api_secret = base64.b64decode(abraia_key).split(':')
abraia.config.save_auth(api_key, api_secret)


# def patterns_to_paths(input_pattern):
# input_files = []
# for pat in input_pattern:
# # Expand ~ to $HOME, then pass to glob
# input_files += glob.glob(os.path.expanduser(pat))
# return input_files


def input_files(path):
filenames = []
if os.path.isfile(path):
Expand All @@ -49,10 +42,8 @@ def process_optimize(args):
path = args['path']
dest = args.get('dest')
format = args.get('format')

filenames = input_files(path)
dirname = path.rstrip('/').rstrip('\\') if os.path.isdir(path) else None

if len(filenames):
for filename in tqdm(filenames, unit='file'):
path, name = os.path.split(filename)
Expand Down Expand Up @@ -96,16 +87,6 @@ def process_url(args):
print('Error to processing:', path)


def process_analyze(args):
result = abraia.from_file(args['path']).analyze()
print(result)


def process_aesthetics(args):
result = abraia.from_file(args['path']).aesthetics()
print(result)


def process_list(path):
files, folders = abraia.list(path=path)
txt = '\n'.join(['{:>28} {}/'.format(
Expand All @@ -117,7 +98,7 @@ def process_list(path):


def process_remove(path):
print(abraia.remove_file(path))
print(abraia.from_store(path).remove())


def add_parser_info(subparser):
Expand All @@ -141,15 +122,14 @@ def add_parser_optimize(subparser):
return parser_optimize


# TODO: Rename store to storage
def add_parser_store(subparser):
parser_store = subparser.add_parser('store', help='work with the cloud stored files')
return parser_store


def parse_input():
parser = argparse.ArgumentParser(description='Abraia image optimization tool')
parser.add_argument('-V', '--version', action='version', version='0.4.1')
parser.add_argument('-V', '--version', action='version', version='0.4.2')
subparser = parser.add_subparsers(dest='command')
parser_info = add_parser_info(subparser)
parser_configure = add_parser_configure(subparser)
Expand All @@ -161,7 +141,6 @@ def parse_input():
parser_remove = subparser_store.add_parser('rm', help='remove a stored file')
parser_remove.add_argument('path', nargs='?', help='image path to remove')
args = vars(parser.parse_args())

if args['command'] is None:
parser.print_help()
sys.exit()
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

setup(
name='abraia',
version='0.4.1',
version='0.4.2',
description='Abraia Python SDK',
long_description=long_description,
long_description_content_type='text/markdown',
Expand Down
4 changes: 2 additions & 2 deletions tests/test_abraia.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,10 @@ def test_from_file():

def test_from_url():
"""Test an API call to upload a remote file"""
url = 'https://abraia.me/images/random.jpg'
url = 'https://upload.wikimedia.org/wikipedia/commons/f/f1/100_m_final_Berlin_2009.JPG'
source = abraia.from_url(url)
assert isinstance(source, abraia.Client)
assert source.path.endswith('random.jpg')
assert source.path.endswith('100_m_final_Berlin_2009.JPG')


def test_optimize_image_from_url():
Expand Down
2 changes: 1 addition & 1 deletion tests/test_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
from abraia import Client, APIError

client = Client()
userid = client.check()
userid = client.load_user()['user']['id']
filename = 'tiger.jpg'


Expand Down

0 comments on commit a7ae2c8

Please sign in to comment.