From 8a3c9033d1c42e9f96e693b2aa4493a52c19c59b Mon Sep 17 00:00:00 2001 From: WuXianChaoPin <1029559041@qq.com> Date: Thu, 26 Apr 2018 18:44:16 +0800 Subject: [PATCH] =?UTF-8?q?=E9=98=BF=E9=87=8C=E4=BA=91=E7=9B=B8=E5=86=8C?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- PixivSearch/aliyun/photo/AliyunPhoto.py | 75 ++++++++++++++----------- 1 file changed, 42 insertions(+), 33 deletions(-) diff --git a/PixivSearch/aliyun/photo/AliyunPhoto.py b/PixivSearch/aliyun/photo/AliyunPhoto.py index 51381aa..6d8a534 100644 --- a/PixivSearch/aliyun/photo/AliyunPhoto.py +++ b/PixivSearch/aliyun/photo/AliyunPhoto.py @@ -17,8 +17,8 @@ class UploadPhoto(AcsClient): super().__init__(ak, secret, region_id, auto_retry, max_retry_time, user_agent, port, timeout, public_key_id, private_key, session_period, credential, debug) aliyunsdkcore.request.set_default_protocol_type("https") - self.domain='cloudphoto.cn-shanghai.aliyuncs.com' - self.version='2017-07-11' + self.domain = 'cloudphoto.cn-shanghai.aliyuncs.com' + self.version = '2017-07-11' def get_md5_01(self, file_path): md5 = None @@ -40,40 +40,55 @@ class UploadPhoto(AcsClient): response = json.loads(self.do_action_with_exception(request).decode()) print(response['Credentials']['SecurityToken']) - def createAction(self,action): + def createAction(self, action): return CommonRequest(domain=self.domain, version=self.version, - action_name=action) + action_name=action) + + def do_action_with_exception(self, acs_request): + return json.loads(super().do_action_with_exception(acs_request).decode()) + + def showOption(self, options, key, desc): + choose = [] + for index, option in enumerate(options): + choose.append(option[key]) + print('{index}:{name}'.format(index=index + 1, name=option[key])) + return choose[(int)(input('输入{desc}\n'.format(desc=desc))) - 1] + if __name__ == '__main__': - # createTransaction("GET", "C:/Users/10295/Desktop/灵梦.png") client = UploadPhoto() request = client.createAction('ListPhotoStores') - response = json.loads(client.do_action_with_exception(request).decode()) - print(response) - + response = client.do_action_with_exception(request) print('PhotoStores:') photoStores = response['PhotoStores'] - for index, photoStore in enumerate(photoStores): - print('{index}:{name}'.format(index=index + 1, name=photoStore['Name'])) - StoreName = input('输入StoreName\n') + StoreName = client.showOption(photoStores, 'Name', 'StoreName') + + request = client.createAction('GetPhotoStore') + request.add_query_param('StoreName', StoreName) + response = client.do_action_with_exception(request) + print(response) + + request = client.createAction('FetchLibraries') + request.add_query_param('StoreName', StoreName) + response = client.do_action_with_exception(request) + Libraries = response['Libraries'] + LibraryId = client.showOption(Libraries, 'LibraryId', 'LibraryId') + + request = client.createAction('GetLibrary') + request.add_body_params('StoreName', StoreName) + request.add_body_params('LibraryId', LibraryId) + response = client.do_action_with_exception(request) + print(response) + + request = client.createAction('CreateTransaction') + filePath = input('输入上传文件路径') + fileName = filePath.split('/')[-1] + request.add_query_param('Size', os.path.getsize(filePath)) + request.add_query_param('Ext', fileName[-fileName[::-1].index('.'):]) + request.add_query_param('Md5', client.get_md5_01(filePath)) + request.add_query_param('StoreName', StoreName) - # request = client.createAction('GetPhotoStore') - # request.set_accept_format('json') - # request.add_query_param('StoreName', StoreName) - # response = json.loads(client.do_action_with_exception(request).decode()) - # print(response) - # buckets = response['PhotoStore']['Buckets'] - # - # request = client.createAction('CreateTransaction') - # filePath = 'C:/Users/10295/Desktop/灵梦.png' - # fileName = filePath.split('/')[-1] - # request.add_query_param('Size', os.path.getsize(filePath)) - # request.add_query_param('Ext', fileName[-fileName[::-1].index('.'):]) - # request.add_query_param('Md5', client.get_md5_01(filePath)) - # request.add_query_param('StoreName', StoreName) - # - # liberaryId = input('输入liberaryId\n') # request.add_query_param('LibraryId', liberaryId) # response = json.loads(client.do_action_with_exception(request)) # print(response) @@ -100,9 +115,3 @@ if __name__ == '__main__': # request.add_query_param('LibraryId', liberaryId) # response = json.loads(client.do_action_with_exception(request)) # print(response) - - request=client.createAction('FetchLibraries') - request.add_query_param('StoreName', StoreName) - request.add_query_param('LibraryId', '') - response = json.loads(client.do_action_with_exception(request)) - print(response) \ No newline at end of file