Skip to content

Commit f42ad7b

Browse files
authored
Merge pull request #189 from degangliu/master
优化审核接口返回参数的array相关字段
2 parents 96ca0c8 + 7f1926d commit f42ad7b

File tree

2 files changed

+138
-9
lines changed

2 files changed

+138
-9
lines changed

qcloud_cos/cos_client.py

Lines changed: 114 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -511,7 +511,25 @@ def get_object_sensitive_content_recognition(self, Bucket, Key, DetectType, Inte
511511
params=params,
512512
headers=headers)
513513

514+
logging.debug("get object sensitive content recognition rsp:%s", rt.content)
514515
data = xml_to_dict(rt.content)
516+
# format res
517+
if 'PornInfo' in data:
518+
if 'OcrResults' in data['PornInfo']:
519+
format_dict_or_list(data['PornInfo']['OcrResults'], ['Keywords'])
520+
format_dict(data['PornInfo'], ['OcrResults', 'ObjectResults'])
521+
if 'TerroristInfo' in data:
522+
if 'OcrResults' in data['TerroristInfo']:
523+
format_dict_or_list(data['TerroristInfo']['OcrResults'], ['Keywords'])
524+
format_dict(data['TerroristInfo'], ['OcrResults', 'ObjectResults'])
525+
if 'PoliticsInfo' in data:
526+
if 'OcrResults' in data['PoliticsInfo']:
527+
format_dict_or_list(data['PoliticsInfo']['OcrResults'], ['Keywords'])
528+
format_dict(data['PoliticsInfo'], ['OcrResults', 'ObjectResults'])
529+
if 'AdsInfo' in data:
530+
if 'OcrResults' in data['AdsInfo']:
531+
format_dict_or_list(data['AdsInfo']['OcrResults'], ['Keywords'])
532+
format_dict(data['AdsInfo'], ['OcrResults', 'ObjectResults'])
515533

516534
return data
517535

@@ -4434,7 +4452,7 @@ def ci_auditing_submit_common(self, Bucket, Key, DetectType, Type, Url=None, Biz
44344452
44354453
:param Bucket(string): 存储桶名称.
44364454
:param Key(string): COS路径.
4437-
:param DetectType(int): 内容识别标志,位计算 1:porn, 2:terrorist, 4:politics, 8:ads
4455+
:param DetectType(int): 内容识别标志,位计算 1:porn, 2:terrorist, 4:politics, 8:ads, 16: Illegal, 32:Abuse
44384456
:param Type(string): 审核类型,video:视频,text:文本,audio:音频,docment:文档。
44394457
:param Url(string): Url, 支持非cos上的文件
44404458
:param Conf(dic): 审核的个性化配置
@@ -4505,6 +4523,7 @@ def ci_auditing_submit_common(self, Bucket, Key, DetectType, Type, Url=None, Biz
45054523
params=params,
45064524
headers=headers)
45074525

4526+
logging.debug("ci auditing rsp:%s", rt.content)
45084527
data = xml_to_dict(rt.content)
45094528

45104529
return data
@@ -4557,6 +4576,7 @@ def ci_auditing_query_common(self, Bucket, Type, JobID, **kwargs):
45574576
params=params,
45584577
headers=headers)
45594578

4579+
logging.debug("query ci auditing:%s", rt.content)
45604580
data = xml_to_dict(rt.content)
45614581

45624582
return data
@@ -4641,13 +4661,50 @@ def ci_auditing_video_query(self, Bucket, JobID, **kwargs):
46414661
print response
46424662
"""
46434663

4644-
return self.ci_auditing_query_common(
4664+
data = self.ci_auditing_query_common(
46454665
Bucket=Bucket,
46464666
JobID=JobID,
46474667
Type='video',
46484668
**kwargs
46494669
)
46504670

4671+
if 'JobsDetail' in data:
4672+
format_dict(data['JobsDetail'], ['Snapshot', 'AudioSection'])
4673+
if 'Snapshot' in data['JobsDetail']:
4674+
for snapshot in data['JobsDetail']['Snapshot']:
4675+
if 'PornInfo' in snapshot:
4676+
format_dict(snapshot['PornInfo'], ['OcrResults', 'ObjectResults'])
4677+
if 'OcrResults' in snapshot['PornInfo']:
4678+
for ocrResult in snapshot['PornInfo']['OcrResults']:
4679+
format_dict(ocrResult, ['Keywords'])
4680+
if 'TerrorismInfo' in snapshot:
4681+
format_dict(snapshot['TerrorismInfo'], ['OcrResults', 'ObjectResults'])
4682+
if 'OcrResults' in snapshot['TerrorismInfo']:
4683+
for ocrResult in snapshot['TerrorismInfo']['OcrResults']:
4684+
format_dict(ocrResult, ['Keywords'])
4685+
if 'PoliticsInfo' in snapshot:
4686+
format_dict(snapshot['PoliticsInfo'], ['OcrResults', 'ObjectResults'])
4687+
if 'OcrResults' in snapshot['PoliticsInfo']:
4688+
for ocrResult in snapshot['PoliticsInfo']['OcrResults']:
4689+
format_dict(ocrResult, ['Keywords'])
4690+
if 'AdsInfo' in snapshot:
4691+
format_dict(snapshot['AdsInfo'], ['OcrResults', 'ObjectResults'])
4692+
if 'OcrResults' in snapshot['AdsInfo']:
4693+
for ocrResult in snapshot['AdsInfo']['OcrResults']:
4694+
format_dict(ocrResult, ['Keywords'])
4695+
if 'AudioSection' in data['JobsDetail']:
4696+
for audioSection in data['JobsDetail']['AudioSection']:
4697+
if 'PornInfo' in audioSection:
4698+
format_dict(audioSection['PornInfo'], ['Keywords'])
4699+
if 'TerrorismInfo' in audioSection:
4700+
format_dict(audioSection['TerrorismInfo'], ['Keywords'])
4701+
if 'PoliticsInfo' in audioSection:
4702+
format_dict(audioSection['PoliticsInfo'], ['Keywords'])
4703+
if 'AdsInfo' in audioSection:
4704+
format_dict(audioSection['AdsInfo'], ['Keywords'])
4705+
4706+
return data
4707+
46514708
def ci_auditing_audio_submit(self, Bucket, Key, DetectType, Url=None, Callback=None, CallbackVersion='Simple', BizType=None, **kwargs):
46524709
"""提交音频审核任务接口 https://cloud.tencent.com/document/product/460/53395
46534710
@@ -4714,12 +4771,26 @@ def ci_auditing_audio_query(self, Bucket, JobID, **kwargs):
47144771
print response
47154772
"""
47164773

4717-
return self.ci_auditing_query_common(
4774+
data = self.ci_auditing_query_common(
47184775
Bucket=Bucket,
47194776
JobID=JobID,
47204777
Type='audio',
47214778
**kwargs
47224779
)
4780+
if 'JobsDetail' in data:
4781+
format_dict(data['JobsDetail'], ['Section'])
4782+
if 'Section' in data['JobsDetail']:
4783+
for section in data['JobsDetail']['Section']:
4784+
if 'PornInfo' in section:
4785+
format_dict(section['PornInfo'], ['Keywords'])
4786+
if 'TerrorismInfo' in section:
4787+
format_dict(section['TerrorismInfo'], ['Keywords'])
4788+
if 'PoliticsInfo' in section:
4789+
format_dict(section['PoliticsInfo'], ['Keywords'])
4790+
if 'AdsInfo' in section:
4791+
format_dict(section['AdsInfo'], ['Keywords'])
4792+
4793+
return data
47234794

47244795
def ci_auditing_text_submit(self, Bucket, Key, DetectType, Content=None, Callback=None, BizType=None, **kwargs):
47254796
"""提交文本审核任务接口 https://cloud.tencent.com/document/product/460/56285
@@ -4758,7 +4829,7 @@ def ci_auditing_text_submit(self, Bucket, Key, DetectType, Content=None, Callbac
47584829
if Callback:
47594830
conf['Callback'] = Callback
47604831

4761-
return self.ci_auditing_submit_common(
4832+
data = self.ci_auditing_submit_common(
47624833
Bucket=Bucket,
47634834
Key=Key,
47644835
Type='text',
@@ -4769,6 +4840,11 @@ def ci_auditing_text_submit(self, Bucket, Key, DetectType, Content=None, Callbac
47694840
**kwargs
47704841
)
47714842

4843+
if 'JobsDetail' in data:
4844+
format_dict(data['JobsDetail'], ['Section'])
4845+
4846+
return data
4847+
47724848
def ci_auditing_text_query(self, Bucket, JobID, **kwargs):
47734849
"""查询文本审核任务接口 https://cloud.tencent.com/document/product/460/56284
47744850
@@ -4789,19 +4865,23 @@ def ci_auditing_text_query(self, Bucket, JobID, **kwargs):
47894865
print response
47904866
"""
47914867

4792-
return self.ci_auditing_query_common(
4868+
data = self.ci_auditing_query_common(
47934869
Bucket=Bucket,
47944870
JobID=JobID,
47954871
Type='text',
47964872
**kwargs
47974873
)
4874+
if 'JobsDetail' in data:
4875+
format_dict(data['JobsDetail'], ['Section'])
4876+
return data
47984877

4799-
def ci_auditing_document_submit(self, Bucket, Url, DetectType, Type=None, Callback=None, BizType=None, **kwargs):
4878+
def ci_auditing_document_submit(self, Bucket, Url, DetectType, Key=None, Type=None, Callback=None, BizType=None, **kwargs):
48004879
"""提交文档审核任务接口 https://cloud.tencent.com/document/product/460/59380
48014880
48024881
:param Bucket(string): 存储桶名称.
48034882
:param Url(string): 文档文件的链接地址,例如 http://www.example.com/doctest.doc
48044883
:param DetectType(int): 内容识别标志,位计算 1:porn, 2:terrorist, 4:politics, 8:ads
4884+
:param Key(string): 存储在 COS 存储桶中的文件名称,例如在目录 test 中的文件test.doc,则文件名称为 test/test. Key 和 Url 只能选择其中一种。
48054885
:param Type(string): 指定文档文件的类型,如未指定则默认以文件的后缀为类型。
48064886
如果文件没有后缀,该字段必须指定,否则会审核失败。例如:doc、docx、ppt、pptx 等
48074887
:param Callback(string): 回调地址,以http://或者https://开头的地址。
@@ -4822,7 +4902,11 @@ def ci_auditing_document_submit(self, Bucket, Url, DetectType, Type=None, Callba
48224902
print response
48234903
"""
48244904

4825-
Input = {'Url': Url}
4905+
Input = {}
4906+
if Url is not None:
4907+
Input['Url'] = Url
4908+
if Key is not None:
4909+
Input['Object'] = Key
48264910
if Type:
48274911
Input['Type'] = Type
48284912

@@ -4863,13 +4947,35 @@ def ci_auditing_document_query(self, Bucket, JobID, **kwargs):
48634947
print response
48644948
"""
48654949

4866-
return self.ci_auditing_query_common(
4950+
data = self.ci_auditing_query_common(
48674951
Bucket=Bucket,
48684952
JobID=JobID,
48694953
Type='document',
48704954
**kwargs
48714955
)
48724956

4957+
if 'JobsDetail' in data and 'PageSegment' in data['JobsDetail'] and 'Results' in data['JobsDetail']['PageSegment']:
4958+
format_dict(data['JobsDetail']['PageSegment'], ['Results'])
4959+
for resultsItem in data['JobsDetail']['PageSegment']['Results']:
4960+
if 'PornInfo' in resultsItem:
4961+
format_dict(resultsItem['PornInfo'], ['OcrResults', 'ObjectResults'])
4962+
if 'OcrResults' in resultsItem['PornInfo']:
4963+
format_dict_or_list(resultsItem['PornInfo']['OcrResults'], ['Keywords'])
4964+
if 'TerrorismInfo' in resultsItem:
4965+
format_dict(resultsItem['TerrorismInfo'], ['OcrResults', 'ObjectResults'])
4966+
if 'OcrResults' in resultsItem['TerrorismInfo']:
4967+
format_dict_or_list(resultsItem['TerrorismInfo']['OcrResults'], ['Keywords'])
4968+
if 'PoliticsInfo' in resultsItem:
4969+
format_dict(resultsItem['PoliticsInfo'], ['OcrResults', 'ObjectResults'])
4970+
if 'OcrResults' in resultsItem['PoliticsInfo']:
4971+
format_dict_or_list(resultsItem['PoliticsInfo']['OcrResults'], ['Keywords'])
4972+
if 'AdsInfo' in resultsItem:
4973+
format_dict(resultsItem['AdsInfo'], ['OcrResults', 'ObjectResults'])
4974+
if 'OcrResults' in resultsItem['AdsInfo']:
4975+
format_dict_or_list(resultsItem['AdsInfo']['OcrResults'], ['Keywords'])
4976+
4977+
return data
4978+
48734979
def ci_get_media_queue(self, Bucket, **kwargs):
48744980
"""查询媒体处理队列接口 https://cloud.tencent.com/document/product/436/54045
48754981

qcloud_cos/cos_comm.py

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -396,13 +396,26 @@ def format_dict(data, key_lst):
396396
return data
397397
for key in key_lst:
398398
# 将dict转为list,保持一致
399-
if key in data and (isinstance(data[key], dict) or isinstance(data[key], str)):
399+
if key in data and (isinstance(data[key], dict) or isinstance(data[key], string_types)):
400400
lst = []
401401
lst.append(data[key])
402402
data[key] = lst
403403
return data
404404

405405

406+
def format_dict_or_list(data, key_lst):
407+
"""转换返回dict或list中的可重复字段为list"""
408+
if not ((isinstance(data, list) or isinstance(data, dict)) and isinstance(key_lst, list)):
409+
return data
410+
if isinstance(data, dict):
411+
return format_dict(data, key_lst)
412+
413+
for data_item in data:
414+
format_dict(data_item, key_lst)
415+
416+
return data
417+
418+
406419
def decode_result(data, key_lst, multi_key_list):
407420
"""decode结果中的字段"""
408421
for key in key_lst:
@@ -471,6 +484,8 @@ class CiDetectType():
471484
TERRORIST = 2
472485
POLITICS = 4
473486
ADS = 8
487+
ILLEGAL = 16
488+
ABUSE = 32
474489

475490
@staticmethod
476491
def get_detect_type_str(DetectType):
@@ -490,6 +505,14 @@ def get_detect_type_str(DetectType):
490505
if len(detect_type) > 0:
491506
detect_type += ','
492507
detect_type += 'Ads'
508+
if DetectType & CiDetectType.ILLEGAL > 0:
509+
if len(detect_type) > 0:
510+
detect_type += ','
511+
detect_type += 'Illegal'
512+
if DetectType & CiDetectType.ABUSE > 0:
513+
if len(detect_type) > 0:
514+
detect_type += ','
515+
detect_type += 'Abuse'
493516

494517
return detect_type
495518

0 commit comments

Comments
 (0)