Skip to content

Commit

Permalink
Rewrites abraia cli using click
Browse files Browse the repository at this point in the history
  • Loading branch information
jrrodri committed Oct 16, 2020
1 parent 35264f2 commit 97b243a
Show file tree
Hide file tree
Showing 10 changed files with 156 additions and 175 deletions.
1 change: 1 addition & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
language: python
python:
- 2.7
- 3.6
install:
- pip install -r requirements.txt
Expand Down
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

# Abraia API client for Python

Python client for the [Abraia API](https://abraia.me/api/). Batch optimize images for web with no quality damage based on perception-driven
Python client for the [Abraia API](https://abraia.me/docs/api/). Batch optimize images for web with no quality damage based on perception-driven
technology.

* Optimal image compression with our perceptual adjustment to preserve the quality and maximize the compression.
Expand Down Expand Up @@ -53,7 +53,7 @@ abraia.from_file('images/lion.jpg').to_file('images/lion_o.jpg')
abraia.from_file('images/jaguar.png').to_file('images/jaguar_o.png')
```

You can also compress and image directly from an URL
You can also compress and image directly from an url:

```python
abraia.from_url('https://api.abraia.me/files/demo/birds.jpg').to_file('images/birds_o.jpg')
Expand Down
13 changes: 2 additions & 11 deletions abraia/abraia.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,20 +6,11 @@
class Abraia(Client):
def __init__(self, folder=''):
super(Abraia, self).__init__()
self.userid = self.userid()
self.userid = self.load_user()['id']
self.folder = folder
self.params = {}
self.path = ''

def userid(self):
try:
return self.user()['id']
except Exception:
return None

def user(self):
return self.load_user()['user']

def list(self, folder=''):
length = len(self.userid) + 1
files, folders = self.list_files(path=self.userid + '/' + folder)
Expand Down Expand Up @@ -57,7 +48,7 @@ def to_file(self, filename):
self.params['format'] = ext.lower()[1:]
buffer = self.transform_image(self.path, self.params)
with open(filename, 'wb') as f:
f.write(buffer.getbuffer())
f.write(buffer.getvalue())
return self

def resize(self, width=None, height=None, mode=None):
Expand Down
19 changes: 15 additions & 4 deletions abraia/client.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import os
import base64
import requests
import mimetypes
from datetime import datetime
from io import BytesIO
from . import config
Expand All @@ -23,7 +24,7 @@ def load_user(self):
resp = requests.get(url, auth=self.auth)
if resp.status_code != 200:
raise APIError(resp.text, resp.status_code)
return resp.json()
return resp.json()['user']
raise APIError('Unauthorized', 401)

def list_files(self, path=''):
Expand All @@ -45,11 +46,13 @@ def upload_remote(self, url, path):
resp = resp.json()
return resp['file']

def upload_file(self, file, path, type=''):
def upload_file(self, file, path):
source = path + os.path.basename(file) if path.endswith('/') else path
name = os.path.basename(source)
url = '{}/files/{}'.format(config.API_URL, source)
type = mimetypes.guess_type(name)[0] or 'binary/octet-stream'
# json = {'name': name, 'type': type, 'md5': md5} if md5 else {'name': name, 'type': type}
json = {'name': name, 'type': type}
url = '{}/files/{}'.format(config.API_URL, source)
resp = requests.post(url, json=json, auth=self.auth)
if resp.status_code != 201:
raise APIError(resp.text, resp.status_code)
Expand All @@ -68,7 +71,8 @@ def move_file(self, old_path, new_path):
resp = resp.json()
return resp['file']

def download_file(self, path):
def download_file(self, path, dest=''):
# T0D0 Add dest ptina paramater to save file
url = '{}/files/{}'.format(config.API_URL, path)
resp = requests.get(url, stream=True, auth=self.auth)
if resp.status_code != 200:
Expand Down Expand Up @@ -100,6 +104,13 @@ def analyze_image(self, path, params={}):
resp['salmap'] = BytesIO(base64.b64decode(resp['salmap'][23:]))
return resp

def detect_labels(self, path, params={}):
url = '{}/rekognition/{}'.format(config.API_URL, path)
resp = requests.get(url, auth=self.auth)
if resp.status_code != 200:
raise APIError(resp.text, resp.status_code)
return resp.json()

def transform_image(self, path, params={}):
if params.get('action'):
params['background'] = '{}/images/{}'.format(config.API_URL, path)
Expand Down
2 changes: 1 addition & 1 deletion abraia/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
import sys
import base64

API_URL = 'https://api.abraia.me'
API_URL = 'https://api2.abraia.me'
CONFIG_FILE = os.path.join(os.path.expanduser('~'), '.abraia')

IMAGE_EXTS = ['.jpg', '.jpeg', '.png', '.gif', '.webp', '.svg', '.psd']
Expand Down
Binary file removed images/lion_o.jpg
Binary file not shown.
1 change: 1 addition & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
requests==2.24.0
click==7.1.2
tqdm==4.50.1

0 comments on commit 97b243a

Please sign in to comment.