Skip to content

Instantly share code, notes, and snippets.

@gronpipmaster gronpipmaster/client_photolab.py Secret
Last active Mar 17, 2019

Embed
What would you like to do?
import requests
import logging
import json
logging.basicConfig(level=logging.ERROR, format='%(asctime)s - %(filename)s:%(lineno)d - %(message)s')
class ClientPhotolab(object):
api_endpoint = 'http://api-soft.photolab.me'
api_upload_endpoint = 'http://upload-soft.photolab.me/upload.php?no_resize=1'
api_endpoint_proxy = 'http://api-proxy-soft.photolab.me'
def image_upload(self, image):
image_blob = None
if type(image) is str:
image_blob = open(image, 'rb')
elif type(image) is file:
image_blob = file
else:
raise Exception('image not file and not filename')
files = {'file1': image_blob}
response = requests.post(self.api_upload_endpoint, files=files)
resp_body = response.text
logging.info('response: {}'.format(resp_body))
return resp_body
def template_upload(self, resources):
resources_blob = None
if type(resources) is str:
resources_blob = open(resources, 'rb')
elif type(resources) is file:
resources_blob = file
else:
raise Exception('resources not file and not filename')
files = {'resources': resources_blob}
endpoint = '{}/template_upload.php'.format(self.api_endpoint_proxy)
response = requests.post(endpoint, files=files)
resp_body = response.text
logging.info('response: {}'.format(resp_body))
return resp_body
def template_process(self, template_name, contents):
form = {
'template_name' : template_name
}
for i in range(0, len(contents)):
content = contents[i]
form['image_url[' + str(i+1) + ']'] = content['url']
if 'crop' in content:
form['crop[' + str(i+1) + ']'] = content['crop']
if 'flip' in content:
form['flip[' + str(i+1) + ']'] = content['flip']
if 'rotate' in content:
form['rotate[' + str(i+1) + ']'] = content['rotate']
endpoint = '{}/template_process.php'.format(self.api_endpoint)
return self._query(endpoint, data=form)
def photolab_process(self, template_name, contents):
form = {
'template_name' : template_name
}
for i in range(0, len(contents)):
content = contents[i]
form['image_url[' + str(i+1) + ']'] = content['url']
if 'crop' in content:
form['crop[' + str(i+1) + ']'] = content['crop']
if 'flip' in content:
form['flip[' + str(i+1) + ']'] = content['flip']
if 'rotate' in content:
form['rotate[' + str(i+1) + ']'] = content['rotate']
endpoint = '{}/template_process.php'.format(self.api_endpoint)
return self._query(endpoint, data=form)
def photolab_steps(self, combo_id):
form = {
'combo_id' : combo_id
}
endpoint = '{}/photolab_steps.php'.format(self.api_endpoint)
return json.loads(self._query(endpoint, data=form))
def photolab_steps_advanced(self, combo_id):
form = {
'combo_id' : combo_id
}
endpoint = '{}/photolab_steps_advanced.php'.format(self.api_endpoint)
return json.loads(self._query(endpoint, data=form))
def _query(self, endpoint, data=None, files=None):
response = requests.post(endpoint, data=data, files=files)
resp_body = response.text
logging.info('response: {}'.format(resp_body))
if response.status_code != 200:
raise Exception('_query: {}, error: {}'.format(endpoint, resp_body))
return resp_body
from client_photolab import ClientPhotolab
content_filename = 'girl.jpg'
resourses_filename = 'resources.zip'
combo_id = '5635874'
combo_id_advanced = '3124589'
api = ClientPhotolab()
content_url = api.image_upload(content_filename)
print('content_url: {}'.format(content_url))
template_name = api.template_upload(resourses_filename)
print('template_name: {}'.format(template_name))
result_url = api.template_process(template_name, [{
'url' : content_url,
'rotate' : 0,
'flip' : 0,
'crop' : '0,0,1,1'
}])
print('result_url: {}'.format(result_url))
result_url = api.template_process(template_name, [{
'url' : content_url
}])
print('result_url: {}'.format(result_url))
print('===')
print('start process combo_id: {}'.format(combo_id))
i = 0
for template_name in api.photolab_steps(combo_id):
result_url = api.photolab_process(str(template_name), [{
'url' : content_url,
'rotate' : 0,
'flip' : 0,
'crop' : '0,0,1,1'
}])
i = i + 1
if i != 0:
content_url = result_url
print('---for template_name: {}, result_url: {}'.format(template_name, result_url))
print('result_url: {}'.format(result_url))
print('===')
print('start process combo_id: {}'.format(combo_id_advanced))
i = 0
for step in api.photolab_steps_advanced(combo_id_advanced)['steps']:
template_name = str(step['id'])
contents = [{
'url' : content_url,
'rotate' : 0,
'flip' : 0,
'crop' : '0,0,1,1'
}]
for i in range(0, len(step['image_urls'])):
contents.append({
'url' : step['image_urls'][i],
'rotate' : 0,
'flip' : 0,
'crop' : '0,0,1,1'
})
result_url = api.photolab_process(template_name, contents)
i = i + 1
if i != 0:
content_url = result_url
print('---for template_name: {}, result_url: {}'.format(template_name, result_url))
print('result_url: {}'.format(result_url))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.