2017-07-12 4 views
1

prestashopの製品から既存の画像を更新しようとしています。私はPythonと要求し、次のコードを使用しています:PythonがPrestashopの画像を更新するように要求します

import requests 
import io 
import mimetypes 
from PIL import Image 
from StringIO import StringIO 

api_key = 'test' 

url = "https://.../api/images/products/249/445" 

file_name = 't3_6kxvzv.jpg' 
fd = io.open(file_name, "rb") 
content = fd.read() 
fd.close() 

def encode_multipart_formdata(): 
    """Encode files to an http multipart/form-data. 
    :param files: a sequence of (type, filename, value) 
     elements for data to be uploaded as files. 
    :return: headers and body. 
    """ 
    BOUNDARY = '----------ThIs_Is_tHe_bouNdaRY_$' 
    CRLF = '\r\n' 
    L = [] 
    L.append('--' + BOUNDARY) 
    L.append(
     'Content-Disposition: form-data; \ 
      name="%s"; filename="%s"' % ("image", file_name)) 
    L.append('Content-Type: %s' % get_content_type(file_name)) 
    L.append('') 
    L.append(content) 
    L.append('--' + BOUNDARY + '--') 
    L.append('') 
    body = CRLF.join(L) 
    headers = { 
     'Content-Type': 'multipart/form-data; boundary=%s' % BOUNDARY 
    } 
    return headers, body 

def get_content_type(file_name): 
    """Retrieve filename mimetype. 
    :param filename: file name. 
    :return: mimetype. 
    """ 
    return mimetypes.guess_type(file_name)[0] or 'application/octet- stream' 

header, body = encode_multipart_formdata() 

r = requests.put(url, data=body, auth=(api_key,""), headers= header) 
# also tried data = content 

r = requests.get(url, auth=(api_key,"")) 
i = Image.open(StringIO(r.content)) 
i.show() 

は私が

data = content 

だけで400のステータスコードを取得して、さまざまなPUTやPOSTリクエストを試してみました。

次に、既存のイメージを取得しようとしましたが正常に動作します。

api_keyには、PUTとPOSTを許可するために必要な設定がすべてあります。

prestashop.add("https://...api/images/products/249/445", files[('image',file_name,content)]) 

は生成します:

KeyError: ('image', 't3_6kxvzv.jpg', '\xff\xd8\xff\xe0\x00\x10JFI... 

私は、私が使用して製品に画像を追加するために自分のドキュメントに従わない可能性がprestapytをインポートした後、prestapytはこの問題を解決する方法に読み込むしようとしました

は私がとget_content_type機能が同様の溶液を生成するためにencode_multipart_formdataを修正するために、次にしようとしたが、400 STAを乗り越えることができませんtusコード。

私は非常にリクエストを使用し、prestapytとターンキーソリューションを使用して画像を更新する方法を理解しようとします。

ありがとうございました!私が使用し

ドキュメント: PrestaShopのhttp://doc.prestashop.com/display/PS16/Chapter+9+-+Image+management

prestapyt https://github.com/prestapyt/prestapyt

UPDATE:

url_2 = "https:/.../api/images/products/249" 
r = requests.post(url_2, data=body, auth=(api_key,""), headers=header) 

私は経由して製品に画像を追加するために要求とPOSTを使用することができました

PUTを使用して画像を変更または更新することはできません。

答えて

0

は仕事にPUT取得できませんでしたので、代わりに

DELETEとPOST

import requests 
import io 
import mimetypes 
import xml.etree.ElementTree as ET 
import sys 

api = '' 

urls =["https://.../api/images/products/22", 
     "https://.../api/images/products/31", 
     "https://.../api/images/products/37", 
     "https://.../api/images/products/46", 
     "https://.../api/images/products/212"] 


def encode_multipart_formdata(file_name,content): 
    """Encode files to an http multipart/form-data. 
    :param files: a sequence of (type, filename, value) 
     elements for data to be uploaded as files. 
    :return: headers and body. 
    """ 
    BOUNDARY = '----------ThIs_Is_tHe_bouNdaRY_$' 
    CRLF = '\r\n' 
    L = [] 
    L.append('--' + BOUNDARY) 
    L.append(
     'Content-Disposition: form-data; \ 
      name="%s"; filename="%s"' % ("image", file_name)) 
    L.append('Content-Type: %s' % get_content_type(file_name)) 
    L.append('') 
    L.append(content) 
    L.append('--' + BOUNDARY + '--') 
    L.append('') 
    body = CRLF.join(L) 
    headers = { 
     'Content-Type': 'multipart/form-data; boundary=%s' % BOUNDARY 
    } 
    return headers, body 


def get_content_type(file_name): 
    """Retrieve filename mimetype. 
    :param filename: file name. 
    :return: mimetype. 
    """ 
    return mimetypes.guess_type(file_name)[0] or 'application/octet-stream' 


def get_image_url(url): 
    """get from a given url the image url""" 
    r = requests.get(url, auth=(api,"")) 
    tree = ET.fromstring(r.content) 
    return tree.find("image").find("declination").get("{http://www.w3.org/1999/xlink}href") 


def delete_image(url): 
    """deletes the image on prestashop given by url""" 
    url2 = get_image_url(url) 
    requests.delete(url2, auth=(api,"")) 


def load_image(file_name): 
    """loads image to upload"""  
    fd = io.open(file_name, "rb") 
    content = fd.read() 
    fd.close() 
    return content, file_name 


def upload_image(url, file_name): 
    """uploads new image to a given url""" 
    content, file_name = load_image(file_name) 
    header, body = encode_multipart_formdata(file_name, content) 
    requests.post(url, data=body, auth=(api,""), headers=header) 

if __name__ == "__main__": 
    file_name = sys.argv[1] 
    content, file_name = load_image(file_name) 
    for i in urls: 
     delete_image(i) 
     upload_image(i,file_name) 
を使用回避策が正常に動作し、このような複雑な方法と理由PUTは動作しませんが存在しなければならない理由はまだ理解していません。

関連する問題