def delete(self, fileid, userid="0"): if not fileid: return {"httpcode": 0, "code": self.IMAGE_PARAMS_ERROR, "message": "params error", "data": {}} expired = int(time.time()) + self.EXPIRED_SECONDS url = self.generate_res_url(userid, fileid, "del") auth = Auth(self._secret_id, self._secret_key) sign = auth.app_sign(url, expired) headers = {"Authorization": sign, "User-Agent": conf.get_ua()} r = {} try: r = requests.post(url, headers=headers) ret = r.json() except Exception as e: if r: return {"httpcode": r.status_code, "code": self.IMAGE_NETWORK_ERROR, "message": str(e), "data": {}} else: return {"httpcode": 0, "code": self.IMAGE_NETWORK_ERROR, "message": str(e), "data": {}} if "code" in ret: if 0 == ret["code"]: return {"httpcode": r.status_code, "code": ret["code"], "message": ret["message"], "data": {}} else: return {"httpcode": r.status_code, "code": ret["code"], "message": ret["message"], "data": {}} else: return {"httpcode": r.status_code, "code": self.IMAGE_NETWORK_ERROR, "message": str(r.raw), "data": {}}
def upload_impl(self, fileobj, filetype, bucket, fileid, userid, magic_context, params): expired = int(time.time()) + self.EXPIRED_SECONDS url = self.generate_res_url_v2(bucket, userid, fileid) auth = Auth(self._secret_id, self._secret_key) sign = auth.get_app_sign_v2(bucket, fileid, expired) data = {} if magic_context: data['MagicContext'] = magic_context headers = { 'Authorization':'QCloud '+sign, 'User-Agent':conf.get_ua(), } if filetype == 0: files = {'FileContent': open(fileobj.decode("utf-8"), 'rb')} elif filetype == 1: files = { 'FileContent': fileobj } if params.has_key('get'): query_str = urllib.urlencode(params['get']); url = url + '?' + query_str r = {} try: r = requests.post(url, data=data, headers=headers, files=files) ret = r.json() except Exception as e: if r: return {'httpcode':r.status_code, 'code':self.IMAGE_NETWORK_ERROR, 'message':str(e), 'data':{}} else: return {'httpcode':0, 'code':self.IMAGE_NETWORK_ERROR, 'message':str(e), 'data':{}} if 'code' in ret: if 0 == ret['code']: data = { 'url':ret['data']['url'], 'download_url':ret['data']['download_url'], 'fileid':ret['data']['fileid'], } if ret['data'].has_key('is_fuzzy'): data['is_fuzzy'] = ret['data']['is_fuzzy'] if ret['data'].has_key('is_food'): data['is_food'] = ret['data']['is_food'] return { 'httpcode':r.status_code, 'code':ret['code'], 'message':ret['message'], 'data':data } else: return { 'httpcode':r.status_code, 'code':ret['code'], 'message':ret['message'], 'data':{} } else: return {'httpcode':r.status_code, 'code':self.IMAGE_NETWORK_ERROR, 'message':str(r.raw), 'data':{}}
def upload_impl(self, file_obj, bucket, fileid, userid, magic_context, params): expired = int(time.time()) + self.EXPIRED_SECONDS url = self.generate_res_url_v2(bucket, userid, fileid) auth = Auth(self._secret_id, self._secret_key) sign = auth.get_app_sign_v2(bucket, fileid, expired) # It seems size is never used # size = os.path.getsize(filepath) data = {} if magic_context: data['MagicContext'] = magic_context headers = { 'Authorization': 'QCloud '+sign, 'User-Agent': conf.get_ua(), } files = {'FileContent': file_obj} if 'get' in params: query_str = urllib.parse.urlencode(params['get']); url = url + '?' + query_str r = {} try: r = requests.post(url, data=data, headers=headers, files=files) ret = r.json() except Exception as e: if r: return {'httpcode': r.status_code, 'code': self.IMAGE_NETWORK_ERROR, 'message': str(e), 'data': {}} else: return {'httpcode': 0, 'code': self.IMAGE_NETWORK_ERROR, 'message': str(e), 'data': {}} if 'code' in ret: if 0 == ret['code']: data = { 'url': ret['data']['url'], 'download_url': ret['data']['download_url'], 'fileid': ret['data']['fileid'], 'info':ret['data']['info'] } if 'is_fuzzy' in ret['data']: data['is_fuzzy'] = ret['data']['is_fuzzy'] if 'is_food' in ret['data']: data['is_food'] = ret['data']['is_food'] return { 'httpcode': r.status_code, 'code': ret['code'], 'message': ret['message'], 'data': data } else: return { 'httpcode': r.status_code, 'code': ret['code'], 'message': ret['message'], 'data': {} } else: return {'httpcode': r.status_code, 'code': self.IMAGE_NETWORK_ERROR, 'message': str(r.raw), 'data': {}}
def upload(self, filepath, userid='0',title='',desc='',magic_context=''): filepath = os.path.abspath(filepath); if os.path.exists(filepath): expired = int(time.time()) + self.EXPIRED_SECONDS url = self.generate_res_url(userid) auth = Auth(self._secret_id, self._secret_key) sign = auth.app_sign(url, expired) size = os.path.getsize(filepath) sha1 = hashlib.sha1(); fp = open(filepath, 'rb') sha1.update(fp.read()) fp.close() headers = { 'Authorization':'QCloud '+sign, 'User-Agent':conf.get_ua(), } files = {'FileContent': open(filepath, 'rb'),'Sha':sha1.hexdigest(),'Title':title,'Desc':desc,'MagicContext':magic_context} r = {} try: r = requests.post(url, headers=headers, files=files) ret = r.json() except Exception as e: if r: return {'httpcode':r.status_code, 'code':self.VIDEO_NETWORK_ERROR, 'message':str(e), 'data':{}} else: return {'httpcode':0, 'code':self.VIDEO_NETWORK_ERROR, 'message':str(e), 'data':{}} if 'code' in ret: if 0 == ret['code']: return { 'httpcode':r.status_code, 'code':ret['code'], 'message':ret['message'], 'data':{ 'url':ret['data']['url'], 'download_url':ret['data']['download_url'], 'fileid':ret['data']['fileid'], 'cover_url': 'cover_url' in ret['data'] and ret['data']['cover_url'] or '', } } else: return { 'httpcode':r.status_code, 'code':ret['code'], 'message':ret['message'], 'data':{} } else: return {'httpcode':r.status_code, 'code':self.VIDEO_NETWORK_ERROR, 'message':str(r.raw), 'data':{}} else: return {'httpcode':0, 'code':self.VIDEO_FILE_NOT_EXISTS, 'message':'file not exists', 'data':{}}
def upload(self, filepath, userid=0, magic_context=''): filepath = os.path.abspath(filepath); if os.path.exists(filepath): expired = int(time.time()) + self.EXPIRED_SECONDS url = self.generate_res_url(userid) auth = Auth(self._secret_id, self._secret_key) sign = auth.app_sign(url, expired) size = os.path.getsize(filepath) data = {} if magic_context: data['MagicContext'] = magic_context headers = { 'Authorization':'QCloud '+sign, 'User-Agent':conf.get_ua(), } files = {'FileContent': open(filepath, 'rb')} r = {} try: r = requests.post(url, data=data, headers=headers, files=files) ret = r.json() except Exception as e: if r: return {'httpcode':r.status_code, 'code':self.IMAGE_NETWORK_ERROR, 'message':str(e), 'data':{}} else: return {'httpcode':0, 'code':self.IMAGE_NETWORK_ERROR, 'message':str(e), 'data':{}} if 'code' in ret: if 0 == ret['code']: return { 'httpcode':r.status_code, 'code':ret['code'], 'message':ret['message'], 'data':{ 'url':ret['data']['url'], 'download_url':ret['data']['download_url'], 'fileid':ret['data']['fileid'], } } else: return { 'httpcode':r.status_code, 'code':ret['code'], 'message':ret['message'], 'data':{} } else: return {'httpcode':r.status_code, 'code':self.IMAGE_NETWORK_ERROR, 'message':str(r.raw), 'data':{}} else: return {'httpcode':0, 'code':self.IMAGE_FILE_NOT_EXISTS, 'message':'file not exists', 'data':{}}
def stat(self, fileid, userid='0'): if not fileid: return {'httpcode':0, 'code':self.VIDEO_PARAMS_ERROR, 'message':'params error', 'data':{}} expired = int(time.time()) + self.EXPIRED_SECONDS url = self.generate_res_url(userid, fileid) auth = Auth(self._secret_id, self._secret_key) sign = auth.app_sign(url, expired) headers = { 'Authorization':'QCloud '+sign, 'User-Agent':conf.get_ua(), } r = {} try: r = requests.get(url, headers=headers) ret = r.json() except Exception as e: if r: return {'httpcode':r.status_code, 'code':self.VIDEO_NETWORK_ERROR, 'message':str(e), 'data':{}} else: return {'httpcode':0, 'code':self.VIDEO_NETWORK_ERROR, 'message':str(e), 'data':{}} if 'code' in ret: if 0 == ret['code']: return { 'httpcode':r.status_code, 'code':ret['code'], 'message':ret['message'], 'data':{ 'download_url':ret['data']['file_url'], 'fileid': 'file_fileid' in ret['data'] and ret['data']['file_fileid'] or '', 'upload_time':ret['data']['file_upload_time'], 'size':ret['data']['file_size'], 'sha':ret['data']['file_sha'], 'video_status':ret['data']['video_status'], 'video_status_msg':ret['data']['video_status_msg'], 'video_play_time': 'video_play_time' in ret['data'] and ret['data']['video_play_time'] or 0, 'video_title': 'video_title' in ret['data'] and ret['data']['video_title'] or '', 'video_desc': 'video_desc' in ret['data'] and ret['data']['video_desc'] or '', 'video_cover_url': 'video_cover_url' in ret['data'] and ret['data']['video_cover_url'] or '', }, } else: return { 'httpcode':r.status_code, 'code':ret['code'], 'message':ret['message'], 'data':{} } else: return {'httpcode':r.status_code, 'code':self.VIDEO_NETWORK_ERROR, 'message':str(r.raw), 'data':{}}
def stat(self, bucket, fileid, userid='0'): if not fileid: return {'httpcode':0, 'code':self.IMAGE_PARAMS_ERROR, 'message':'params error', 'data':{}} if isinstance(fileid, unicode): fileid = fileid.encode('utf-8') expired = int(time.time()) + self.EXPIRED_SECONDS url = self.generate_res_url_v2(bucket, userid, fileid) auth = Auth(self._secret_id, self._secret_key) sign = auth.get_app_sign_v2(bucket, fileid, expired) headers = { 'Authorization':'QCloud '+sign, 'User-Agent':conf.get_ua(), } r = {} try: r = requests.get(url, headers=headers) ret = r.json() except Exception as e: if r: return {'httpcode':r.status_code, 'code':self.IMAGE_NETWORK_ERROR, 'message':str(e), 'data':{}} else: return {'httpcode':0, 'code':self.IMAGE_NETWORK_ERROR, 'message':str(e), 'data':{}} if 'code' in ret: if 0 == ret['code']: return { 'httpcode':r.status_code, 'code':ret['code'], 'message':ret['message'], 'data':{ 'download_url':ret['data']['file_url'], 'fileid':ret['data']['file_fileid'], 'upload_time':ret['data']['file_upload_time'], 'size':ret['data']['file_size'], 'md5':ret['data']['file_md5'], 'width':ret['data']['photo_width'], 'height':ret['data']['photo_height'], } } else: return { 'httpcode':r.status_code, 'code':ret['code'], 'message':ret['message'], 'data':{} } else: return {'httpcode':r.status_code, 'code':self.IMAGE_NETWORK_ERROR, 'message':str(r.raw), 'data':{}}
def upload_impl(self, fileobj, filetype, userid, magic_context, params): expired = int(time.time()) + self.EXPIRED_SECONDS url = self.generate_res_url(userid) auth = Auth(self._secret_id, self._secret_key) sign = auth.app_sign(url, expired) data = {} if magic_context: data["MagicContext"] = magic_context headers = {"Authorization": "QCloud " + sign, "User-Agent": conf.get_ua()} if filetype == 0: files = {"FileContent": open(fileobj, "rb")} elif filetype == 1: files = {"FileContent": fileobj} if params.has_key("get"): query_str = urllib.urlencode(params["get"]) url = url + "?" + query_str r = {} try: r = requests.post(url, data=data, headers=headers, files=files) ret = r.json() except Exception as e: if r: return {"httpcode": r.status_code, "code": self.IMAGE_NETWORK_ERROR, "message": str(e), "data": {}} else: return {"httpcode": 0, "code": self.IMAGE_NETWORK_ERROR, "message": str(e), "data": {}} if "code" in ret: if 0 == ret["code"]: data = { "url": ret["data"]["url"], "download_url": ret["data"]["download_url"], "fileid": ret["data"]["fileid"], } if ret["data"].has_key("is_fuzzy"): data["is_fuzzy"] = ret["data"]["is_fuzzy"] if ret["data"].has_key("is_food"): data["is_food"] = ret["data"]["is_food"] return {"httpcode": r.status_code, "code": ret["code"], "message": ret["message"], "data": data} else: return {"httpcode": r.status_code, "code": ret["code"], "message": ret["message"], "data": {}} else: return {"httpcode": r.status_code, "code": self.IMAGE_NETWORK_ERROR, "message": str(r.raw), "data": {}}
def copy(self, bucket, fileid, userid='0'): if not fileid: return {'httpcode':0, 'code':self.IMAGE_PARAMS_ERROR, 'message':'params error', 'data':{}} if isinstance(fileid, unicode): fileid = fileid.encode('utf-8') expired = 0 url = self.generate_res_url_v2(bucket, userid, fileid, 'copy') auth = Auth(self._secret_id, self._secret_key) sign = auth.get_app_sign_v2(bucket, fileid, expired) headers = { 'Authorization':'QCloud '+sign, 'User-Agent':conf.get_ua(), } r = {} try: r = requests.post(url, headers=headers) ret = r.json() except Exception as e: if r: return {'httpcode':r.status_code, 'code':self.IMAGE_NETWORK_ERROR, 'message':str(e), 'data':{}} else: return {'httpcode':0, 'code':self.IMAGE_NETWORK_ERROR, 'message':str(e), 'data':{}} if 'code' in ret: if 0 == ret['code']: return { 'httpcode':r.status_code, 'code':ret['code'], 'message':ret['message'], 'data':{ 'url':ret['data']['url'], 'download_url':ret['data']['download_url'], }, } else: return { 'httpcode':r.status_code, 'code':ret['code'], 'message':ret['message'], 'data':{}, } else: return {'httpcode':r.status_code, 'code':self.IMAGE_NETWORK_ERROR, 'message':str(r.raw), 'data':{}}
def upload_data(self,userid,data,session,offset): url = self.generate_res_url(userid) expired = int(time.time()) + self.EXPIRED_SECONDS auth = Auth(self._secret_id, self._secret_key) sign = auth.app_sign(url, expired) sha1 = hashlib.sha1(); sha1.update(data) headers = { 'Authorization':sign, 'User-Agent':conf.get_ua(), } files = {'op': ('upload_slice'),'filecontent': data,'session':session,'offset':str(offset)} r = {} try: r = requests.post(url, headers=headers,files=files) ret = r.json() except Exception as e: if r: return {'httpcode':r.status_code, 'code':self.VIDEO_NETWORK_ERROR, 'message':str(e), 'data':{}} else: return {'httpcode':0, 'code':self.VIDEO_NETWORK_ERROR, 'message':str(e), 'data':{}} if 'code' in ret: if 0 == ret['code']: return { 'httpcode':r.status_code, 'code':ret['code'], 'message':ret['message'], 'data':ret['data'], } else: return { 'httpcode':r.status_code, 'code':ret['code'], 'message':ret['message'], 'data':{} } else: return {'httpcode':r.status_code, 'code':self.VIDEO_NETWORK_ERROR, 'message':str(r.raw), 'data':{}}
def copy(self, fileid, userid=0): if not fileid: return {'httpcode':0, 'code':self.IMAGE_PARAMS_ERROR, 'message':'params error', 'data':{}} expired = int(time.time()) + self.EXPIRED_SECONDS url = self.generate_res_url(userid, fileid, 'copy') auth = Auth(self._secret_id, self._secret_key) sign = auth.app_sign(url, expired) headers = { 'Authorization':'QCloud '+sign, 'User-Agent':conf.get_ua(), } r = {} try: r = requests.post(url, headers=headers) ret = r.json() except Exception as e: if r: return {'httpcode':r.status_code, 'code':self.IMAGE_NETWORK_ERROR, 'message':str(e), 'data':{}} else: return {'httpcode':0, 'code':self.IMAGE_NETWORK_ERROR, 'message':str(e), 'data':{}} if 'code' in ret: if 0 == ret['code']: return { 'httpcode':r.status_code, 'code':ret['code'], 'message':ret['message'], 'data':{ 'url':ret['data']['url'], 'download_url':ret['data']['download_url'], }, } else: return { 'httpcode':r.status_code, 'code':ret['code'], 'message':ret['message'], 'data':{}, } else: return {'httpcode':r.status_code, 'code':self.IMAGE_NETWORK_ERROR, 'message':str(r.raw), 'data':{}}
def stat(self, fileid, userid="0"): if not fileid: return {"httpcode": 0, "code": self.IMAGE_PARAMS_ERROR, "message": "params error", "data": {}} expired = int(time.time()) + self.EXPIRED_SECONDS url = self.generate_res_url(userid, fileid) auth = Auth(self._secret_id, self._secret_key) sign = auth.app_sign(url, expired) headers = {"Authorization": "QCloud " + sign, "User-Agent": conf.get_ua()} r = {} try: r = requests.get(url, headers=headers) ret = r.json() except Exception as e: if r: return {"httpcode": r.status_code, "code": self.IMAGE_NETWORK_ERROR, "message": str(e), "data": {}} else: return {"httpcode": 0, "code": self.IMAGE_NETWORK_ERROR, "message": str(e), "data": {}} if "code" in ret: if 0 == ret["code"]: return { "httpcode": r.status_code, "code": ret["code"], "message": ret["message"], "data": { "download_url": ret["data"]["file_url"], "fileid": ret["data"]["file_fileid"], "upload_time": ret["data"]["file_upload_time"], "size": ret["data"]["file_size"], "md5": ret["data"]["file_md5"], "width": ret["data"]["photo_width"], "height": ret["data"]["photo_height"], }, } else: return {"httpcode": r.status_code, "code": ret["code"], "message": ret["message"], "data": {}} else: return {"httpcode": r.status_code, "code": self.IMAGE_NETWORK_ERROR, "message": str(r.raw), "data": {}}
def upload_data(self, userid, data, session, offset): url = self.generate_res_url(userid) expired = int(time.time()) + self.EXPIRED_SECONDS auth = Auth(self._secret_id, self._secret_key) sign = auth.app_sign(url, expired) sha1 = hashlib.sha1() sha1.update(data) headers = { 'Authorization': sign, 'User-Agent': conf.get_ua(), } files = { 'op': ('upload_slice'), 'filecontent': data, 'session': session, 'offset': str(offset) } r = {} try: r = requests.post(url, headers=headers, files=files) ret = r.json() except Exception as e: if r: return { 'httpcode': r.status_code, 'code': self.VIDEO_NETWORK_ERROR, 'message': str(e), 'data': {} } else: return { 'httpcode': 0, 'code': self.VIDEO_NETWORK_ERROR, 'message': str(e), 'data': {} } if 'code' in ret: if 0 == ret['code']: return { 'httpcode': r.status_code, 'code': ret['code'], 'message': ret['message'], 'data': ret['data'], } else: return { 'httpcode': r.status_code, 'code': ret['code'], 'message': ret['message'], 'data': {} } else: return { 'httpcode': r.status_code, 'code': self.VIDEO_NETWORK_ERROR, 'message': str(r.raw), 'data': {} }
def delete(self, fileid, userid='0'): if not fileid: return { 'httpcode': 0, 'code': self.IMAGE_PARAMS_ERROR, 'message': 'params error', 'data': {} } expired = int(time.time()) + self.EXPIRED_SECONDS url = self.generate_res_url(userid, fileid, 'del') auth = Auth(self._secret_id, self._secret_key) sign = auth.app_sign(url, expired) headers = { 'Authorization': sign, 'User-Agent': conf.get_ua(), } r = {} try: r = requests.post(url, headers=headers) ret = r.json() except Exception as e: if r: return { 'httpcode': r.status_code, 'code': self.IMAGE_NETWORK_ERROR, 'message': str(e), 'data': {} } else: return { 'httpcode': 0, 'code': self.IMAGE_NETWORK_ERROR, 'message': str(e), 'data': {} } if 'code' in ret: if 0 == ret['code']: return { 'httpcode': r.status_code, 'code': ret['code'], 'message': ret['message'], 'data': {}, } else: return { 'httpcode': r.status_code, 'code': ret['code'], 'message': ret['message'], 'data': {}, } else: return { 'httpcode': r.status_code, 'code': self.IMAGE_NETWORK_ERROR, 'message': str(r.raw), 'data': {} }
def upload_impl(self, file_obj, userid, magic_context, params): expired = int(time.time()) + self.EXPIRED_SECONDS url = self.generate_res_url(userid) auth = Auth(self._secret_id, self._secret_key) sign = auth.app_sign(url, expired) # It seems file size is never uesed # size = os.path.getsize(filepath) data = {} if magic_context: data['MagicContext'] = magic_context headers = { 'Authorization': 'QCloud ' + sign, 'User-Agent': conf.get_ua(), } files = {'FileContent': file_obj} if 'get' in params: query_str = urllib.parse.urlencode(params['get']) url = url + '?' + query_str r = {} try: r = requests.post(url, data=data, headers=headers, files=files) ret = r.json() except Exception as e: if r: return { 'httpcode': r.status_code, 'code': self.IMAGE_NETWORK_ERROR, 'message': str(e), 'data': {} } else: return { 'httpcode': 0, 'code': self.IMAGE_NETWORK_ERROR, 'message': str(e), 'data': {} } if 'code' in ret: if 0 == ret['code']: data = { 'url': ret['data']['url'], 'download_url': ret['data']['download_url'], 'fileid': ret['data']['fileid'], } if 'is_fuzzy' in ret['data']: data['is_fuzzy'] = ret['data']['is_fuzzy'] if 'is_food' in ret['data']: data['is_food'] = ret['data']['is_food'] return { 'httpcode': r.status_code, 'code': ret['code'], 'message': ret['message'], 'data': data } else: return { 'httpcode': r.status_code, 'code': ret['code'], 'message': ret['message'], 'data': {} } else: return { 'httpcode': r.status_code, 'code': self.IMAGE_NETWORK_ERROR, 'message': str(r.raw), 'data': {} }
def stat(self, fileid, userid='0'): if not fileid: return { 'httpcode': 0, 'code': self.IMAGE_PARAMS_ERROR, 'message': 'params error', 'data': {} } expired = int(time.time()) + self.EXPIRED_SECONDS url = self.generate_res_url(userid, fileid) auth = Auth(self._secret_id, self._secret_key) sign = auth.app_sign(url, expired) headers = { 'Authorization': 'QCloud ' + sign, 'User-Agent': conf.get_ua(), } r = {} try: r = requests.get(url, headers=headers) ret = r.json() except Exception as e: if r: return { 'httpcode': r.status_code, 'code': self.IMAGE_NETWORK_ERROR, 'message': str(e), 'data': {} } else: return { 'httpcode': 0, 'code': self.IMAGE_NETWORK_ERROR, 'message': str(e), 'data': {} } if 'code' in ret: if 0 == ret['code']: return { 'httpcode': r.status_code, 'code': ret['code'], 'message': ret['message'], 'data': { 'download_url': ret['data']['file_url'], 'fileid': ret['data']['file_fileid'], 'upload_time': ret['data']['file_upload_time'], 'size': ret['data']['file_size'], 'md5': ret['data']['file_md5'], 'width': ret['data']['photo_width'], 'height': ret['data']['photo_height'], } } else: return { 'httpcode': r.status_code, 'code': ret['code'], 'message': ret['message'], 'data': {} } else: return { 'httpcode': r.status_code, 'code': self.IMAGE_NETWORK_ERROR, 'message': str(r.raw), 'data': {} }
def copy(self, bucket, fileid, userid='0'): if not fileid: return { 'httpcode': 0, 'code': self.IMAGE_PARAMS_ERROR, 'message': 'params error', 'data': {} } expired = 0 url = self.generate_res_url_v2(bucket, userid, fileid, 'copy') auth = Auth(self._secret_id, self._secret_key) sign = auth.get_app_sign_v2(bucket, fileid, expired) headers = { 'Authorization': 'QCloud ' + sign, 'User-Agent': conf.get_ua(), } r = {} try: r = requests.post(url, headers=headers) ret = r.json() except Exception as e: if r: return { 'httpcode': r.status_code, 'code': self.IMAGE_NETWORK_ERROR, 'message': str(e), 'data': {} } else: return { 'httpcode': 0, 'code': self.IMAGE_NETWORK_ERROR, 'message': str(e), 'data': {} } if 'code' in ret: if 0 == ret['code']: return { 'httpcode': r.status_code, 'code': ret['code'], 'message': ret['message'], 'data': { 'url': ret['data']['url'], 'download_url': ret['data']['download_url'], }, } else: return { 'httpcode': r.status_code, 'code': ret['code'], 'message': ret['message'], 'data': {}, } else: return { 'httpcode': r.status_code, 'code': self.IMAGE_NETWORK_ERROR, 'message': str(r.raw), 'data': {} }
def upload_impl(self, fileobj, filetype, bucket, fileid, userid, magic_context, params): expired = int(time.time()) + self.EXPIRED_SECONDS url = self.generate_res_url_v2(bucket, userid, fileid) auth = Auth(self._secret_id, self._secret_key) sign = auth.get_app_sign_v2(bucket, fileid, expired) data = {} if magic_context: data['MagicContext'] = magic_context headers = { 'Authorization': 'QCloud ' + sign, 'User-Agent': conf.get_ua(), } if filetype == 0: files = {'FileContent': open(fileobj, 'rb')} elif filetype == 1: files = {'FileContent': fileobj} if params.has_key('get'): query_str = urllib.urlencode(params['get']) url = url + '?' + query_str r = {} try: r = requests.post(url, data=data, headers=headers, files=files) ret = r.json() except Exception as e: if r: return { 'httpcode': r.status_code, 'code': self.IMAGE_NETWORK_ERROR, 'message': str(e), 'data': {} } else: return { 'httpcode': 0, 'code': self.IMAGE_NETWORK_ERROR, 'message': str(e), 'data': {} } if 'code' in ret: if 0 == ret['code']: data = { 'url': ret['data']['url'], 'download_url': ret['data']['download_url'], 'fileid': ret['data']['fileid'], } if ret['data'].has_key('is_fuzzy'): data['is_fuzzy'] = ret['data']['is_fuzzy'] if ret['data'].has_key('is_food'): data['is_food'] = ret['data']['is_food'] return { 'httpcode': r.status_code, 'code': ret['code'], 'message': ret['message'], 'data': data } else: return { 'httpcode': r.status_code, 'code': ret['code'], 'message': ret['message'], 'data': {} } else: return { 'httpcode': r.status_code, 'code': self.IMAGE_NETWORK_ERROR, 'message': str(r.raw), 'data': {} }
def upload(self, filepath, userid='0', title='', desc='', magic_context=''): filepath = os.path.abspath(filepath) if os.path.exists(filepath): expired = int(time.time()) + self.EXPIRED_SECONDS url = self.generate_res_url(userid) auth = Auth(self._secret_id, self._secret_key) sign = auth.app_sign(url, expired) size = os.path.getsize(filepath) sha1 = hashlib.sha1() fp = open(filepath, 'rb') sha1.update(fp.read()) fp.close() headers = { 'Authorization': 'QCloud ' + sign, 'User-Agent': conf.get_ua(), } files = { 'FileContent': open(filepath, 'rb'), 'Sha': sha1.hexdigest(), 'Title': title, 'Desc': desc, 'MagicContext': magic_context } r = {} try: r = requests.post(url, headers=headers, files=files) ret = r.json() except Exception as e: if r: return { 'httpcode': r.status_code, 'code': self.VIDEO_NETWORK_ERROR, 'message': str(e), 'data': {} } else: return { 'httpcode': 0, 'code': self.VIDEO_NETWORK_ERROR, 'message': str(e), 'data': {} } if 'code' in ret: if 0 == ret['code']: return { 'httpcode': r.status_code, 'code': ret['code'], 'message': ret['message'], 'data': { 'url': ret['data']['url'], 'download_url': ret['data']['download_url'], 'fileid': ret['data']['fileid'], 'cover_url': ret['data'].has_key('cover_url') and ret['data']['cover_url'] or '', } } else: return { 'httpcode': r.status_code, 'code': ret['code'], 'message': ret['message'], 'data': {} } else: return { 'httpcode': r.status_code, 'code': self.VIDEO_NETWORK_ERROR, 'message': str(r.raw), 'data': {} } else: return { 'httpcode': 0, 'code': self.VIDEO_FILE_NOT_EXISTS, 'message': 'file not exists', 'data': {} }
def stat(self, fileid, userid='0'): if not fileid: return { 'httpcode': 0, 'code': self.VIDEO_PARAMS_ERROR, 'message': 'params error', 'data': {} } expired = int(time.time()) + self.EXPIRED_SECONDS url = self.generate_res_url(userid, fileid) auth = Auth(self._secret_id, self._secret_key) sign = auth.app_sign(url, expired) headers = { 'Authorization': 'QCloud ' + sign, 'User-Agent': conf.get_ua(), } r = {} try: r = requests.get(url, headers=headers) ret = r.json() except Exception as e: if r: return { 'httpcode': r.status_code, 'code': self.VIDEO_NETWORK_ERROR, 'message': str(e), 'data': {} } else: return { 'httpcode': 0, 'code': self.VIDEO_NETWORK_ERROR, 'message': str(e), 'data': {} } if 'code' in ret: if 0 == ret['code']: return { 'httpcode': r.status_code, 'code': ret['code'], 'message': ret['message'], 'data': { 'download_url': ret['data']['file_url'], 'fileid': ret['data'].has_key('file_fileid') and ret['data']['file_fileid'] or '', 'upload_time': ret['data']['file_upload_time'], 'size': ret['data']['file_size'], 'sha': ret['data']['file_sha'], 'video_status': ret['data']['video_status'], 'video_status_msg': ret['data']['video_status_msg'], 'video_play_time': ret['data'].has_key('video_play_time') and ret['data']['video_play_time'] or 0, 'video_title': ret['data'].has_key('video_title') and ret['data']['video_title'] or '', 'video_desc': ret['data'].has_key('video_desc') and ret['data']['video_desc'] or '', 'video_cover_url': ret['data'].has_key('video_cover_url') and ret['data']['video_cover_url'] or '', }, } else: return { 'httpcode': r.status_code, 'code': ret['code'], 'message': ret['message'], 'data': {} } else: return { 'httpcode': r.status_code, 'code': self.VIDEO_NETWORK_ERROR, 'message': str(r.raw), 'data': {} }