def get_affiliation(affl_id): logger.info(f'Retrieving affiliation {affl_id}') api_key = key_manager.get_key('affiliation') client = elsclient.ElsClient(api_key) affl = ElsAffil(affil_id=affl_id) if not affl.read(client): logger.error("[!]Read affiliation failed: %s", affl_id) affl.data['_id'] = affl.data['coredata']['dc:identifier'].split(':')[1] yield affl.data
def readElsevierAffiliation(self, affilID='60101411'): ## Affiliation example # Initialize affiliation with ID as string my_aff = ElsAffil(affil_id=affilID) if my_aff.read(self, self.client): print("my_aff.name: ", my_aff.name) my_aff.write() else: print("Read affiliation failed.")
def readAllDocsFromAffiliation(self, affilID='60101411'): ## Read all documents for example affiliation, then write to disk my_aff = ElsAffil(affil_id=affilID) if my_aff.read_docs(client): print("my_aff.doc_list has " + str(len(my_aff.doc_list)) + " items.") my_aff.write_docs() else: print("Read docs for affiliation failed.")
class TestElsAffil: """Test affiliation functionality""" ## Test data aff_uri = "https://api.elsevier.com/content/affiliation/affiliation_id/60101411" aff_id_int = 60101411 aff_id_str = "60101411" ## Test initialization def test_init_uri(self): """ Test case: uri is set correctly during initialization with uri""" myAff = ElsAffil(uri = self.aff_uri) assert myAff.uri == self.aff_uri def test_init_aff_id_int(self): """ Test case: uri is set correctly during initialization with affiliation id as integer""" myAff = ElsAffil(affil_id = self.aff_id_int) assert myAff.uri == self.aff_uri def test_init_aff_id_str(self): """ Test case: uri is set correctly during initialization with affiliation id as string""" myAff = ElsAffil(affil_id = self.aff_id_str) assert myAff.uri == self.aff_uri ## Test reading/writing author profile data bad_client = ElsClient("dummy") good_client = ElsClient(config['apikey'], inst_token = config['insttoken']) good_client.local_dir = str(test_path) myAff = ElsAffil(uri = aff_uri) def test_read_good_bad_client(self): """Test case: using a well-configured client leads to successful read and using a badly-configured client does not.""" assert self.myAff.read(self.bad_client) == False assert self.myAff.read(self.good_client) == True def test_json_to_dict(self): """Test case: the JSON read by the author object from the API is parsed into a Python dictionary""" assert type(self.myAff.data) == dict def test_name_getter(self): """Test case: the name attribute is returned as a non-empty string""" assert (type(self.myAff.name) == str and self.myAff.name != '') def test_write(self): """Test case: the author object's data is written to a file with the author ID in the filename""" self.myAff.write() assert util.file_exist_with_id(self.myAff.data['coredata']['dc:identifier'].split(':')[1]) def test_read_docs(self): self.myAff.read_docs() assert len(self.myAff.doc_list) == int(self.myAff.data['coredata']['document-count'])
def test_init_aff_id_str(self): """ Test case: uri is set correctly during initialization with affiliation id as string""" myAff = ElsAffil(affil_id = self.aff_id_str) assert myAff.uri == self.aff_uri
def test_init_uri(self): """ Test case: uri is set correctly during initialization with uri""" myAff = ElsAffil(uri = self.aff_uri) assert myAff.uri == self.aff_uri
#h = fishbase.host ## Author example # Initialize author with uri my_auth = ElsAuthor( uri = 'https://api.elsevier.com/content/author/author_id/7004367821') # Read author data, then write to disk if my_auth.read(client): print ("my_auth.full_name: ", my_auth.full_name) my_auth.write() else: print ("Read author failed.") ## Affiliation example # Initialize affiliation with ID as string my_aff = ElsAffil(affil_id = '60101411') if my_aff.read(client): print ("my_aff.name: ", my_aff.name) my_aff.write() else: print ("Read affiliation failed.") ## Scopus (Abtract) document example # Initialize document with ID as integer scp_doc = AbsDoc(scp_id = 84872135457) if scp_doc.read(client): print ("scp_doc.title: ", scp_doc.title) scp_doc.write() else: print ("Read document failed.")