-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathoapenuploader.py
66 lines (56 loc) · 2.09 KB
/
oapenuploader.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
#!/usr/bin/env python3
"""
Retrieve and disseminate files and metadata to OAPEN
"""
import logging
import sys
from ftplib import FTP, error_perm
from io import BytesIO
from errors import DisseminationError
from uploader import Uploader
class OAPENUploader(Uploader):
"""Dissemination logic for OAPEN"""
def upload_to_platform(self):
"""
Upload work in required format to OAPEN.
Only the OAPEN ONIX file is required, as OAPEN can retrieve
content files from the links contained within it.
"""
# Fast-fail if credentials for upload are missing
try:
user = self.get_variable_from_env('oapen_ftp_user', 'OAPEN')
passwd = self.get_variable_from_env('oapen_ftp_pw', 'OAPEN')
except DisseminationError as error:
logging.error(error)
sys.exit(1)
metadata_bytes = self.get_formatted_metadata('onix_3.0::oapen')
# Filename TBD: use work ID for now
filename = self.work_id
try:
with FTP(
host='oapen-ftpserver.org',
user=user,
passwd=passwd,
) as ftp:
try:
ftp.cwd('/OAPEN')
except FileNotFoundError:
logging.error(
'Could not find folder "OAPEN" on OAPEN FTP server')
sys.exit(1)
try:
ftp.storbinary('STOR {}.xml'.format(
filename), BytesIO(metadata_bytes))
except error_perm as error:
logging.error(
'Error uploading to OAPEN FTP server: {}'.format(error))
sys.exit(1)
except error_perm as error:
logging.error(
'Could not connect to OAPEN FTP server: {}'.format(error))
sys.exit(1)
logging.info('Successfully uploaded to OAPEN FTP server')
def parse_metadata(self):
"""Convert work metadata into OAPEN format"""
# Not required for OAPEN - only the ONIX file is required
pass