def __init__(self, pid, client=None): """Initialize provider. To use the default client, just configure the following variables: * `PIDSTORE_DATACITE_USERNAME` as username. * `PIDSTORE_DATACITE_PASSWORD` as password. * `PIDSTORE_DATACITE_DOI_PREFIX` as DOI prefix. * `PIDSTORE_DATACITE_TESTMODE` to `True` if it configured in test mode. * `PIDSTORE_DATACITE_URL` as DataCite URL. :param pid: A :class:`invenio_pidstore.models.PersistentIdentifier` instance. :param client: A client to access to DataCite. (Default: :class:`datacite.DataCiteMDSClient` instance) """ super(DataCiteProvider, self).__init__(pid) if client is not None: self.api = client else: self.api = DataCiteMDSClient( username=current_app.config.get('PIDSTORE_DATACITE_USERNAME'), password=current_app.config.get('PIDSTORE_DATACITE_PASSWORD'), prefix=current_app.config.get('PIDSTORE_DATACITE_DOI_PREFIX'), test_mode=current_app.config.get( 'PIDSTORE_DATACITE_TESTMODE', False), url=current_app.config.get('PIDSTORE_DATACITE_URL'))
def update_record(idv, username, password, datacite_password): url = 'https://' + username + ':' + password + '@authors.library.caltech.edu/rest/eprint/' record_url = url + str(idv) + '.xml' record = subprocess.check_output(["eputil", record_url], universal_newlines=True) eprint = xmltodict.parse(record)['eprints']['eprint'] metadata = caltech_thesis.epxml_to_datacite(eprint) assert schema40.validate(metadata) #Debugging if this fails #v = schema40.validator.validate(metadata) #errors = sorted(v.iter_errors(instance), key=lambda e: e.path) #for error in errors: # print(error.message) # Initialize the MDS client. d = DataCiteMDSClient( username='******', password=datacite_password, prefix='10.7907', ) xml = schema40.tostring(metadata) result = d.metadata_post(xml) print(result)
async def write_datacite_metadata(guid, temp_dir, metadata): try: doi = next(( identifier["attributes"]["value"] for identifier in metadata["data"]["embeds"]["identifiers"]["data"] if identifier["attributes"]["category"] == "doi")) except StopIteration: raise DataCiteNotFoundError( f"Datacite DOI not found for registration {guid} on OSF server.") client = DataCiteMDSClient( url=settings.DATACITE_URL, username=settings.DATACITE_USERNAME, password=settings.DATACITE_PASSWORD, prefix=settings.DATACITE_PREFIX, ) try: xml_metadata = client.metadata_get(doi) except DataCiteNotFoundError: raise DataCiteNotFoundError( f"Datacite DOI {doi} not found for registration {guid} on Datacite server." ) with open(os.path.join(temp_dir, "datacite.xml"), "w") as fp: fp.write(xml_metadata) return xml_metadata
async def write_datacite_metadata(guid, temp_dir): data = get_with_retry( f"{settings.OSF_API_URL}v2/registrations/{guid}/identifiers/" ).json()["data"] doi = [ identifier["attributes"]["value"] for identifier in data if identifier["attributes"]["category"] == "doi" ] if not doi: raise DataCiteNotFoundError( f"Datacite DOI not found for registration {guid} on OSF server." ) else: doi = doi[0] client = DataCiteMDSClient( url=settings.DATACITE_URL, username=settings.DATACITE_USERNAME, password=settings.DATACITE_PASSWORD, prefix=settings.DATACITE_PREFIX, ) try: xml_metadata = client.metadata_get(doi) except DataCiteNotFoundError: raise DataCiteNotFoundError( f"Datacite DOI not found for registration {guid} on Datacite server." ) with open(os.path.join(temp_dir, "datacite.xml"), "w") as fp: fp.write(xml_metadata) return xml_metadata
def create_doi(metadata, url): password = os.environ['DATACITE'] prefix = '10.33569' # Initialize the MDS client. d = DataCiteMDSClient(username='******', password=password, prefix=prefix, url='https://mds.test.datacite.org' #test_mode=True ) result = schema40.validate(metadata) #Debugging if this fails if result == False: v = schema40.validator.validate(metadata) errors = sorted(v.iter_errors(instance), key=lambda e: e.path) for error in errors: print(error.message) exit() #Provide only prefix in identifier field to let DataCite generate DOI metadata['identifier'] = {'identifier': prefix, 'identifierType': 'DOI'} xml = schema40.tostring(metadata) response = d.metadata_post(xml) print(response) identifier = response.split('(')[1].split(')')[0] response = d.doi_post(identifier, url) print(response)
def update_doi(doi,metadata,url=''): password = os.environ['DATACITE'] prefix = doi.split('/')[0] #Ensure metadata identifier matches that given in function metadata['identifier'] = {'identifier':doi,'identifierType':'DOI'} # Initialize the MDS client. d = DataCiteMDSClient( username='******', password=password, prefix=prefix, url='https://mds.datacite.org' #test_mode=True ) result = schema40.validate(metadata) #Debugging if this fails if result == False: v = schema40.validator.validate(metadata) errors = sorted(v.iter_errors(instance), key=lambda e: e.path) for error in errors: print(error.message) exit() xml = schema40.tostring(metadata) response = d.metadata_post(xml) print(response) if url != '': response = d.doi_post(doi,url) print(response)
def getDataCiteClient(): config = getConfig() client = DataCiteMDSClient(username=config['DATACITE']['user'], password=config['DATACITE']['password'], prefix=config['DATACITE']['prefix'], test_mode=False) return client
def __init__(self, base_url, prefix, client=None): self.base_url = base_url self.prefix = prefix self._client = client or DataCiteMDSClient( url=self.base_url, username=settings.DATACITE_USERNAME, password=settings.DATACITE_PASSWORD, prefix=self.prefix)
def get_datacite_metadata(doi): client = DataCiteMDSClient( url=settings.DATACITE_URL, username=settings.DATACITE_USERNAME, password=settings.DATACITE_PASSWORD, prefix=settings.DATACITE_PREFIX, ) return client.metadata_get(doi)
def __init__(self): """Initialize provider.""" self.api = DataCiteMDSClient(username=cfg.get('CFG_DATACITE_USERNAME'), password=cfg.get('CFG_DATACITE_PASSWORD'), prefix=cfg.get('CFG_DATACITE_DOI_PREFIX'), test_mode=cfg.get('CFG_DATACITE_TESTMODE', False), url=cfg.get('CFG_DATACITE_URL'))
def test_api_url(): """Test client init.""" c = DataCiteMDSClient( username="******", prefix="10.1234", url="https://mds.example.org", # without slash test_mode=True, ) assert c.api_url == "https://mds.example.org/" # with slash assert c.prefix == "10.1234" assert c.test_mode is True assert c.__repr__() == "<DataCiteMDSClient: TEST>"
def get_client(username="******", password="******", prefix='10.1234', with_fake_url=True): """Create a API client.""" client = DataCiteMDSClient( username=username, password=password, prefix=prefix, test_mode=True, ) if with_fake_url: # change URL for tests client.api_url = APIURL return client
def update_doi_url(doi, url): password = os.environ['DATACITE'] prefix = doi.split('/')[0] # Initialize the MDS client. d = DataCiteMDSClient(username='******', password=password, prefix=prefix, url='https://mds.datacite.org') response = d.doi_post(doi, url) print(response)
def update_datacite_metadata(collection, token, access): """Access contains username, password, and prefix for DataCite""" keys = dataset.keys(collection) for a in access: username = a["username"] password = a["password"] prefix = a["prefix"] # Initialize the MDS client. d = DataCiteMDSClient( username=username, password=password, prefix=prefix, url="https://mds.datacite.org", ) for k in keys: print(k) metadata, err = dataset.read(collection, k) if err != "": print(err) exit() # Get rid of Key from dataset metadata.pop("_Key") if "identifier" in metadata: record_doi = metadata["identifier"]["identifier"] # Handle records with 4.3 metadata elements if "schemaVersion" in metadata: metadata.pop("schemaVersion") if "types" in metadata: metadata.pop("types") if record_doi.split("/")[0] == prefix: result = schema40.validate(metadata) # Debugging if this fails if result == False: print(metadata) v = schema40.validator.validate(metadata) errors = sorted(v.iter_errors(instance), key=lambda e: e.path) for error in errors: print(error.message) exit() xml = schema40.tostring(metadata) response = d.metadata_post(xml) print(response)
def __init__(self, node): try: assert settings.DATACITE_URL and (getattr( node.provider, 'doi_prefix', None) or settings.DATACITE_PREFIX) except AssertionError: raise ImproperlyConfigured( 'OSF\'Datacite client\'s settings are not configured') self._client = DataCiteMDSClient( url=settings.DATACITE_URL, username=settings.DATACITE_USERNAME, password=settings.DATACITE_PASSWORD, prefix=getattr(node.provider, 'doi_prefix', None) or settings.DATACITE_PREFIX)
def get_client(username="******", password="******", prefix='10.5072', test_mode=False, timeout=None): """Create a API client.""" return DataCiteMDSClient( username=username, password=password, prefix=prefix, url=APIURL, test_mode=test_mode, timeout=timeout, )
def get_datacite_metadata(doi, datacite_username, datacite_password, datacite_prefix): assert isinstance(datacite_password, str), 'Datacite password not passed to pigeon' assert isinstance(datacite_username, str), 'Datacite username not passed to pigeon' assert isinstance(datacite_prefix, str), 'Datacite prefix not passed to pigeon' client = DataCiteMDSClient( url=settings.DATACITE_URL, username=datacite_username, password=datacite_password, prefix=datacite_prefix, ) return client.metadata_get(doi)
def update_doi(doi, xml, url=''): password = os.environ['DATACITE'] prefix = doi.split('/')[0] # Initialize the MDS client. d = DataCiteMDSClient(username='******', password=password, prefix=prefix, url='https://mds.datacite.org' #test_mode=True ) response = d.metadata_post(xml) print(response) if url != '': response = d.doi_post(doi, url) print(response)
def generate_new_doi(*args): # If you want to generate XML for earlier versions, you need to use either the # schema31, schema40 or schema41 instead. data = { "identifier": { "identifier": prefix + "/RDS." + str(args[0]), "identifierType": "DOI" }, "creators": [{ "creatorName": organization }], "titles": [{ "title": args[1] }], "publisher": "ICIMOD", "publicationYear": args[2], "language": "en-us", "resourceType": { "resourceTypeGeneral": "Dataset" }, "rightsList": [{ "rights": "Creative Commons Attribution 4.0 International (CC-BY-4.0)", "rightsURI": "https://creativecommons.org/licenses/by/4.0", "lang": "en-us" }], "descriptions": [{ "descriptionType": "Abstract", "description": args[3] }], } # Validate dictionary # assert schema41.validate(data) # Generate DataCite XML from dictionary. doc = schema41.tostring(data) # print(doc) # Initialize the MDS client. d = DataCiteMDSClient( username=login_user, password=login_pass, url=url, prefix=prefix, ) # Set metadata for DOI d.metadata_post(doc) # Mint new DOI d.doi_post(prefix + '/RDS.' + str(args[0]), args[4]) logging.basicConfig(filename='logs/DOI_generation_Log_' + date_time + '.log', filemode='w', format='%(asctime)s - %(message)s', level=logging.INFO) logging.info("{} DOI Registration for id={} and title={}".format( args[5], args[0], args[1])) print(args[5], prefix + '/RDS.' + str(args[0]), args[4])
} metadata['publicationYear'] = str(now.year) metadata['publisher'] = 'Caltech Library' #Get the prefix to use prefix = inputv['prefix'].split('(')[1].split(')')[0] #prefix = '10.5072' #Get our DataCite password infile = open('pw', 'r') password = infile.readline().strip() # Initialize the MDS client. d = DataCiteMDSClient( username='******', password=password, prefix=prefix, ) identifier = str(prefix) metadata['identifier'] = { 'identifier': identifier, 'identifierType': 'DOI' } #assert schema40.validate(metadata) #Debugging if this fails #v = schema40.validator.validate(metadata) #errors = sorted(v.iter_errors(instance), key=lambda e: e.path) #for error in errors:
'resourceType': 'Dataset', 'resourceTypeGeneral': 'Dataset' }, 'schemaVersion': 'http://datacite.org/schema/kernel-4', } # Validate dictionary assert schema42.validate(data) # Generate DataCite XML from dictionary. doc = schema42.tostring(data) # Initialize the MDS client. d = DataCiteMDSClient( username='******', password='******', prefix='10.5072', ) # Set metadata for DOI d.metadata_post(doc) # Mint new DOI d.doi_post('10.5072/test-doi', 'http://example.org/test-doi') # Get DOI location location = d.doi_get("10.5072/test-doi") # Set alternate URL for content type (available through content negotiation) d.media_post( "10.5072/test-doi",
metadata['publisher'] = 'Caltech Library' metadata['resourceType'] = { "resourceTypeGeneral": 'Other', 'resourceType': 'Website' } #Get our DataCite password infile = open('data/pw', 'r') password = infile.readline().strip() # Initialize the MDS client. d = DataCiteMDSClient( username='******', password=password, prefix=prefix, #test_mode=True ) doi_end = subprocess.check_output(['./gen-cool-doi'], universal_newlines=True) identifier = str(prefix) + '/' + str(doi_end) metadata['identifier'] = {'identifier': identifier, 'identifierType': 'DOI'} assert schema40.validate(metadata) #Debugging if this fails #v = schema40.validator.validate(metadata) #errors = sorted(v.iter_errors(instance), key=lambda e: e.path) #for error in errors: # print(error.message)
'resourceType': 'Dataset', 'resourceTypeGeneral': 'Dataset' }, 'schemaVersion': 'http://datacite.org/schema/kernel-4', } # Validate dictionary assert schema42.validate(data) # Generate DataCite XML from dictionary. doc = schema42.tostring(data) # Initialize the MDS client. d = DataCiteMDSClient( username='******', password='******', prefix='10.1234', test_mode=True, ) # Set metadata for DOI d.metadata_post(doc) # Mint new DOI d.doi_post('10.1234/test-doi', 'http://example.org/test-doi') # Get DOI location location = d.doi_get("10.1234/test-doi") # Set alternate URL for content type (available through content negotiation) d.media_post( "10.1234/test-doi", {