Skip to content
Toggle navigation
Toggle navigation
This project
Loading...
Sign in
周伟奇
/
bmw-ocr
Go to a project
Toggle navigation
Toggle navigation pinning
Projects
Groups
Snippets
Help
Project
Activity
Repository
Graphs
Network
Create a new issue
Commits
Issue Boards
Files
Commits
Network
Compare
Branches
Tags
50b15762
authored
2021-10-22 14:02:36 +0800
by
周伟奇
Browse Files
Options
Browse Files
Tag
Download
Email Patches
Plain Diff
ecm download
1 parent
5a19df38
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
94 additions
and
0 deletions
src/apps/doc/exceptions.py
src/apps/doc/ocr/ecm.py
src/common/redis_cache/handler.py
src/apps/doc/exceptions.py
View file @
50b1576
...
...
@@ -24,3 +24,7 @@ class GCAPException(Exception):
class
CMSException
(
Exception
):
pass
class
ECMException
(
Exception
):
pass
...
...
src/apps/doc/ocr/ecm.py
0 → 100644
View file @
50b1576
import
base64
import
requests
from
common.redis_cache
import
redis_handler
as
rh
from
settings
import
conf
from
apps.doc.exceptions
import
ECMException
class
ECM
:
def
__init__
(
self
):
self
.
oauth_token
=
None
self
.
username
=
conf
.
ECM_USER
self
.
pwd
=
conf
.
ECM_PWD
self
.
oauth_url
=
conf
.
ECM_OAUTH_URL
self
.
download_url
=
conf
.
ECM_DOWNLOAD_URL
self
.
oauth_headers
=
{
'Content-Type'
:
'application/x-www-form-urlencoded'
}
self
.
oauth_payload
=
{
'grant_type'
:
'client_credentials'
,
'client_id'
:
conf
.
ECM_OAUTH_ID
,
'client_secret'
:
conf
.
ECM_OAUTH_SECRET
,
}
self
.
token_key
=
'access_token'
self
.
token_type
=
'Bearer'
self
.
token_type_key
=
'token_type'
self
.
expires_key
=
'expires_in'
def
update_oauth_token
(
self
):
response
=
requests
.
post
(
self
.
oauth_url
,
headers
=
self
.
oauth_headers
,
data
=
self
.
oauth_payload
,
verify
=
False
)
if
response
.
status_code
!=
200
:
raise
ECMException
(
'ECM Oauth response with code: {0}'
.
format
(
response
.
status_code
))
token
=
response
.
json
()
.
get
(
self
.
token_key
)
if
not
isinstance
(
token
,
str
):
raise
ECMException
(
'ECM Oauth can not get token: {0}'
.
format
(
response
.
json
()))
self
.
oauth_token
=
token
self
.
token_type
=
response
.
json
()
.
get
(
self
.
token_type_key
,
self
.
token_type
)
expires
=
response
.
json
()
.
get
(
self
.
expires_key
,
3600
)
rh
.
set_ecm_token
(
self
.
oauth_token
,
expires
)
def
get_oauth_token
(
self
):
if
self
.
oauth_token
is
None
:
# redis获取token
self
.
oauth_token
=
rh
.
get_ecm_token
()
if
self
.
oauth_token
is
None
:
self
.
update_oauth_token
()
return
self
.
oauth_token
def
download
(
self
,
save_path
,
doc_base
,
doc_type
,
object_id
):
download_headers
=
{
'Authorization'
:
'{0} {1}'
.
format
(
self
.
token_type
,
self
.
get_oauth_token
())
}
download_json
=
{
"userName"
:
self
.
username
,
"password"
:
self
.
pwd
,
"docbase"
:
doc_base
,
"documentType"
:
doc_type
,
"objectId"
:
object_id
,
}
response
=
requests
.
post
(
self
.
download_url
,
headers
=
download_headers
,
json
=
download_json
,
verify
=
False
)
if
response
.
status_code
!=
200
:
raise
ECMException
(
'ECM download response with code: {0}'
.
format
(
response
.
status_code
))
base64_data
=
response
.
json
()
.
get
(
'Envelope'
,
{})
.
get
(
'Body'
,
{})
.
get
(
'getResponse'
,
{})
.
get
(
'return'
,
{})
.
get
(
'DataObjects'
,
{})
.
get
(
'Contents'
,
{})
.
get
(
'Value'
)
if
not
isinstance
(
base64_data
,
str
):
raise
ECMException
(
'ECM download failed: {0}'
.
format
(
response
.
json
()))
with
open
(
save_path
,
"wb"
)
as
fh
:
fh
.
write
(
base64
.
b64decode
(
base64_data
.
encode
()))
def
upload
(
self
):
{
"username"
:
"fanliubing"
,
"password"
:
"Bmwecm123456&api1"
,
"docbase"
:
"SF5_CN"
,
"documentType"
:
"acceptance"
,
"object_name"
:
"JoeMyPDF202109061529.pdf"
,
"folder"
:
"/Wholesale/Operations/Audits"
,
"format"
:
"pdf"
,
"b_application_no"
:
""
,
"file_base64_content"
:
""
,
}
src/common/redis_cache/handler.py
View file @
50b1576
...
...
@@ -37,6 +37,7 @@ class RedisHandler:
self
.
priority_queue_key
=
'{0}:priority_queue'
.
format
(
self
.
prefix
)
self
.
session_id_key
=
'{0}:session_id'
.
format
(
self
.
prefix
)
self
.
cms_token_key
=
'{0}:cms_token'
.
format
(
self
.
prefix
)
self
.
ecm_token_key
=
'{0}:ecm_token'
.
format
(
self
.
prefix
)
def
enqueue
(
self
,
tasks
,
is_priority
=
False
):
# 1
...
...
@@ -64,3 +65,9 @@ class RedisHandler:
def
set_cms_token
(
self
,
token
,
expires
=
None
):
return
self
.
redis
.
set
(
self
.
cms_token_key
,
token
,
expires
)
def
get_ecm_token
(
self
):
return
self
.
redis
.
get
(
self
.
ecm_token_key
)
def
set_ecm_token
(
self
,
token
,
expires
=
None
):
return
self
.
redis
.
set
(
self
.
ecm_token_key
,
token
,
expires
)
...
...
Write
Preview
Styling with
Markdown
is supported
Attach a file
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to post a comment