def orcid_user_info(affiliation='', orcid_id='', access_token=''): if affiliation: info = {} info.setdefault('orcid', orcid_id) sandbox = orcid_secrets.orcid_app_data.get(affiliation).get('sandbox') client_id = orcid_secrets.orcid_app_data.get(affiliation).get('sandbox_client_id') client_secret = orcid_secrets.orcid_app_data.get(affiliation).get('sandbox_client_secret') redirect_uri = orcid_secrets.orcid_app_data.get(affiliation).get('redirect_uri') if not sandbox: client_id = orcid_secrets.orcid_app_data.get(affiliation).get('client_id') client_secret = orcid_secrets.orcid_app_data.get(affiliation).get('client_secret') api = orcid.MemberAPI(client_id, client_secret, sandbox=sandbox) try: # get public_info from orcid public_info = api.read_record_public(orcid_id=orcid_id, request_type='person', token=access_token) return public_info except RequestException as e: logging.error('ORCID-ERROR: %s' % e.response.text) else: logging.error('Bad request: affiliation has no value!')
def orcid_read_works(affiliation='', orcid_id='', access_token=''): works = [] if affiliation: sandbox = orcid_secrets.orcid_app_data.get(affiliation).get('sandbox') client_id = orcid_secrets.orcid_app_data.get(affiliation).get('sandbox_client_id') client_secret = orcid_secrets.orcid_app_data.get(affiliation).get('sandbox_client_secret') if not sandbox: client_id = orcid_secrets.orcid_app_data.get(affiliation).get('client_id') client_secret = orcid_secrets.orcid_app_data.get(affiliation).get('client_secret') api = orcid.MemberAPI(client_id, client_secret, sandbox=sandbox) try: info = api.read_record_member(orcid_id=orcid_id, request_type='activities', token=access_token) # logging.info('info: %s' % info) works = info.get('works').get('group') except RequestException as e: logging.error('ORCID-ERROR: %s' % e.response.text) else: logging.error('Bad request: affiliation has no value!') return works
def orcid_add_external_id(affiliation='', orcid_id='', access_token='', external_id=None): put_code = '' if affiliation: sandbox = orcid_secrets.orcid_app_data.get(affiliation).get('sandbox') client_id = orcid_secrets.orcid_app_data.get(affiliation).get('sandbox_client_id') client_secret = orcid_secrets.orcid_app_data.get(affiliation).get('sandbox_client_secret') if not sandbox: client_id = orcid_secrets.orcid_app_data.get(affiliation).get('client_id') client_secret = orcid_secrets.orcid_app_data.get(affiliation).get('client_secret') api = orcid.MemberAPI(client_id, client_secret, sandbox=sandbox) try: logging.info('external_id: %s' % external_id) put_code = api.add_record(orcid_id=orcid_id, token=access_token, request_type='external-identifiers', data=external_id) # get info from orcid again info = api.read_record_member(orcid_id=orcid_id, request_type='external-identifiers', token=access_token) logging.info('info: %s' % info) except RequestException as e: logging.error('ORCID-ERROR: %s' % e.response.text) else: logging.error('Bad request: affiliation has no value!') return put_code
def orcid_update_records(affiliation='', orcid_id='', access_token='', works=None): if works is None: works = {} if works: if affiliation: sandbox = orcid_secrets.orcid_app_data.get(affiliation).get('sandbox') client_id = orcid_secrets.orcid_app_data.get(affiliation).get('sandbox_client_id') client_secret = orcid_secrets.orcid_app_data.get(affiliation).get('sandbox_client_secret') if not sandbox: client_id = orcid_secrets.orcid_app_data.get(affiliation).get('client_id') client_secret = orcid_secrets.orcid_app_data.get(affiliation).get('client_secret') api = orcid.MemberAPI(client_id, client_secret, sandbox=sandbox) for record_id in works.keys(): # logging.info('work: %s' % work) work = works.get(record_id)[0] print(json.dumps(work, indent=4)) try: put_code = int(record_id) api.update_record(orcid_id=orcid_id, token=access_token, request_type='work', put_code=put_code, data=work) except RequestException as e: logging.error('ORCID-ERROR: %s' % e.response.text) break else: logging.error('Bad request: affiliation has no value!')
def orcid_add_records(affiliation='', orcid_id='', access_token='', works=None): if works is None: works = {} if works: if affiliation: sandbox = orcid_secrets.orcid_app_data.get(affiliation).get('sandbox') client_id = orcid_secrets.orcid_app_data.get(affiliation).get('sandbox_client_id') client_secret = orcid_secrets.orcid_app_data.get(affiliation).get('sandbox_client_secret') if not sandbox: client_id = orcid_secrets.orcid_app_data.get(affiliation).get('client_id') client_secret = orcid_secrets.orcid_app_data.get(affiliation).get('client_secret') api = orcid.MemberAPI(client_id, client_secret, sandbox=sandbox) for record_id in works.keys(): # logging.info('work: %s' % work) work = works.get(record_id)[0] print(work) try: put_code = api.add_record(orcid_id=orcid_id, token=access_token, request_type='work', data=work) if put_code: update_json = {} orcid_sync = {'orcid_id': orcid_id, 'orcid_put_code': str(put_code)} update_json['orcid_sync'] = [orcid_sync] logger.info('PUT /work/%s' % record_id) # TODO PUT request logger.info(json.dumps(update_json, indent=4)) try: # put data response = requests.put( '%s/%s/%s' % (orcid_secrets.API, 'work', record_id), headers={'Content-Type': 'application/json', 'Authorization': 'Bearer %s' % orcid_secrets.TOKEN}, data=json.dumps(update_json) ) status = response.status_code logger.info('STATUS: %s' % status) if status == 200: response_json = json.loads(response.content.decode("utf-8")) logger.info(response_json.get('work')) if response_json.get('message'): logger.info(response_json.get('message')) else: logger.error('ERROR: %s: %s' % (status, response.content.decode("utf-8"))) except requests.exceptions.ConnectionError as e: logging.error(e) except RequestException as e: logging.error('ORCID-ERROR: %s' % e.response.text) break else: logging.error('Bad request: affiliation has no value!')
def get_dois_from_orcid_api_v1(orcid_id, get_titles=False): """Get dois in case of a known ORCID. @param scope: orcid_id @type scope: string @param get_titles: whether to add titles to the results or not @type get_titles: boolean @return: dois of a person with ORCID indicated by orcid_id @rtype: list """ ap = orcid.MemberAPI(CFG_OAUTH2_CONFIGURATIONS['orcid']['consumer_key'], CFG_OAUTH2_CONFIGURATIONS['orcid']['consumer_secret'], 'sandbox' in CFG_OAUTH2_CONFIGURATIONS['orcid']['member_url']) orcid_profile = None if orcid_id: try: orcid_profile = ap.read_record_public(orcid_id, 'activities') except RequestException: register_exception(alert_admin=True) dois = list() if orcid_profile is None or orcid_profile['works'] is None: return dois try: for work in orcid_profile['works']['group']: try: if work['identifiers'] and \ work['identifiers']['identifier'] is not None: for identifier in work['identifiers']['identifier']: identifier_type = \ identifier['external-identifier-type'].lower() value = identifier['external-identifier-id'] if identifier_type == "doi": doi = get_doi(value) if doi: current_dois = doi.split(",") if not get_titles: dois.append(current_dois[0]) else: title = work['work-summary'][0]['title'] if title: title = title['title']['value'] dois.append((doi, title)) except KeyError: # No identifiers on this work. pass except KeyError: register_exception(alert_admin=True) return dois
def member(self): orcid_base_url = self.app.config['OAUTHCLIENT_REMOTE_APPS']['orcid'][ 'params']['base_url'] orcid_consumer_secret = self.app.config[ 'OAUTHCLIENT_ORCID_CREDENTIALS']['consumer_secret'] orcid_consumer_key = self.app.config['OAUTHCLIENT_ORCID_CREDENTIALS'][ 'consumer_key'] sandbox = (orcid_base_url == 'https://pub.sandbox.orcid.org/') return orcid.MemberAPI(orcid_consumer_secret, orcid_consumer_key, sandbox=sandbox)
def _get_extids_from_orcid(orcid_id): """Get all external ids from ORCID database for a given person. @param orcid_id: ORCID in format xxxx-xxxx-xxxx-xxxx @type orcid_id: str @return: a dictionary which contains all external identifiers for given person. Identifiers are stored in sets under identifiers names keys. @rtype: dictionary """ ap = orcid.MemberAPI(CFG_OAUTH2_CONFIGURATIONS['orcid']['consumer_key'], CFG_OAUTH2_CONFIGURATIONS['orcid']['consumer_secret'], 'sandbox' in CFG_OAUTH2_CONFIGURATIONS['orcid']['member_url']) ext_ids_dict = { 'doi': set(), 'arxiv': set(), 'isbn': set(), 'other-id': set() } orcid_profile = None if orcid_id: try: orcid_profile = ap.read_record_public(orcid_id, 'activities') except RequestException: register_exception(alert_admin=True) if not orcid_profile: return ext_ids_dict try: for work in orcid_profile.get('works', {}).get('group', []): try: for identifier in work['identifiers']['identifier']: identifier_type = identifier[ 'external-identifier-type'].lower() value = identifier['external-identifier-id'] if identifier_type in ext_ids_dict: if identifier_type == "doi": ext_ids_dict[identifier_type].add(get_doi(value)) ext_ids_dict[identifier_type].add(value) except (KeyError, AttributeError, TypeError): # No identifiers on this work. pass except (KeyError, AttributeError): # Very likely there are no works in this profile. pass return ext_ids_dict
def ORCIDValidator(form, field): """Validate that the given ORCID exists.""" msg = u"The ORCID iD was not found in <a href='http://orcid.org' target='_blank'>orcid.org</a>. Please, make sure it is valid." orcid_id = field.data if current_app.config.get("ORCID_APP_CREDENTIALS"): api = orcid.MemberAPI( current_app.config["ORCID_APP_CREDENTIALS"]["consumer_key"], current_app.config["ORCID_APP_CREDENTIALS"]["consumer_secret"]) try: result = api.search_member("orcid:" + orcid_id) if result['orcid-search-results']["num-found"] == 0: raise StopValidation(msg) except RequestException: return
def init_app(self, app, **kwargs): """Initialize application object.""" app.extensions['inspire-orcid'] = self orcid_base_url = app.config['OAUTHCLIENT_REMOTE_APPS']['orcid'][ 'params']['base_url'] orcid_consumer_secret = app.config['OAUTHCLIENT_ORCID_CREDENTIALS'][ 'consumer_secret'] orcid_consumer_key = app.config['OAUTHCLIENT_ORCID_CREDENTIALS'][ 'consumer_key'] self.sandbox = (orcid_base_url == 'https://pub.sandbox.orcid.org/') self.orcid_api = orcid.MemberAPI(orcid_consumer_secret, orcid_consumer_key, sandbox=self.sandbox)