class MendeleyImporter: #parameters are OAuth keys def __init__(self, consumer_key, secret_key): self.mendeley = MendeleyClient(consumer_key, secret_key) #fetches URL for authenticating tokens def get_auth_url(self): self.mendeley.request_token = self.mendeley.mendeley.request_token() return(self.mendeley.mendeley.authorize(self.mendeley.request_token)) #loads authenticated OAuth tokens def load_keys(self, api_keys_pkl_dir): #dirty hack tmp_cwd = os.getcwd() os.chdir(api_keys_pkl_dir) self.mendeley.load_keys() os.chdir(tmp_cwd) #dumps authenticated OAuth tokens to file def save_keys(self, api_keys_pkl_dir): #same dirty hack as before tmp_cwd = os.getcwd() os.chdir(api_keys_pkl_dir) self.mendeley.save_keys() os.chdir(tmp_cwd) #get dictionary with folder IDs and names def get_folders(self): return self.mendeley.folders() #get list of dictionarys with document details from given folder, or all documents if folder_id is 0 def get_documents(self, folder_id): list_result = [] #get document_ids for docs in given folder if folder_id == 0: #all documents fold = self.mendeley.library() else: fold = self.mendeley.folder_documents(folder_id) #get details for document_ids for doc in fold['document_ids']: doc_details = self.mendeley.document_details(doc) if not 'citation_key' in doc_details: #awkward, mendeley did not give citation_key #let's be creative and generate one doc_details['citation_key'] = '' if doc_details['authors']!=[]: doc_details['citation_key'] += doc_details['authors'][0]['surname'] if 'year' in doc_details: doc_details['citation_key'] += doc_details['year']+'_' doc_details['citation_key'] += doc_details['id'] list_result.append(doc_details) return list_result # parses serialized request token and the verifier that was given by user def set_verified_token(self, token_string, verifier): self.mendeley.request_token = oauth2.Token.from_string(token_string) self.mendeley.mendeley.authorize(self.mendeley.request_token) self.mendeley.request_token.set_verifier(verifier) self.mendeley.access_token = self.mendeley.mendeley.access_token(self.mendeley.request_token)
def get_mendeley_authored_documents(): '''This function gets all of the authored documents in the authorized library. It will return a dictionary named documents with keys documentId and several fields from the Mendeley API. ''' mendeley = MendeleyClient(settings.MENDELEY_CONSUMER_KEY, settings.MENDELEY_SECRET_KEY) try: mendeley.load_keys() except IOError: mendeley.get_required_keys() mendeley.save_keys() authored_document_list = {} response = mendeley.documents_authored() #this get a list of all documents authored by the authorized user for document in response['document_ids']: #this generates a list of all the details for each of these documents details = mendeley.document_details(document) authored_document_list[document] = details return authored_document_list
def get_mendeley_authored_documents(): '''This function gets all of the authored documents in the authorized library. It will return a dictionary named documents with keys documentId and several fields from the Mendeley API. ''' mendeley = MendeleyClient(settings.MENDELEY_CONSUMER_KEY, settings.MENDELEY_SECRET_KEY) try: mendeley.load_keys() except IOError: mendeley.get_required_keys() mendeley.save_keys() authored_document_list = {} response = mendeley.documents_authored( ) #this get a list of all documents authored by the authorized user for document in response[ 'document_ids']: #this generates a list of all the details for each of these documents details = mendeley.document_details(document) authored_document_list[document] = details return authored_document_list
# pprint(response) print "Created Review Child Folder" reviewchildfolderid = response['folder_id'] docs = mendeley.folder_documents(folderid) pprint(docs) print "Retrieving documents from selected folder" pub_list = [] pprint(pub_list) # from wikiradar.py # Dictionary mapping uuid's to a dictionary with keys: authors, title, year, count related_doc_dict = dict() details = mendeley.document_details(documents['document_ids'][0]) print "Looking up suggestions for related docs." print "" for pub_item in pub_list: pprint(pub_item) related_docs = mendeley.related(pub_item.uuid, items=10) for related_doc in related_docs['documents']: uuid = related_doc['uuid'] rel_doc_info = related_doc_dict.get(uuid, None) if rel_doc_info: rel_doc_info['count'] += 1 else: rel_doc_info = dict() rel_doc_info['authors'] = related_doc['authors'] rel_doc_info['title'] = related_doc['title']
print """ ----------------------------------------------------- Create a new library document -----------------------------------------------------""" response = mendeley.create_document(document=json.dumps({'type' : 'Book','title': 'Document creation test', 'year': 2008})) pprint(response) documentId = response['document_id'] print """ ----------------------------------------------------- Document details -----------------------------------------------------""" response = mendeley.document_details(documentId) pprint(response) print """ ----------------------------------------------------- Delete library document -----------------------------------------------------""" response = mendeley.delete_library_document(documentId) pprint(response) print """ ----------------------------------------------------- Documents authored -----------------------------------------------------"""
sys.exit(1) # create a client and load tokens from the pkl file mendeley = MendeleyClient(config.api_key, config.api_secret) tokens_store = MendeleyTokensStore() # configure the client to use a specific token # if no tokens are available, prompt the user to authenticate access_token = tokens_store.get_access_token("test_account") if not access_token: mendeley.interactive_auth() tokens_store.add_account("test_account",mendeley.get_access_token()) else: mendeley.set_access_token(access_token) documents = mendeley.folder_documents(34240081)['document_ids'] for document_id in documents: document = mendeley.document_details(document_id) theFile = config.reading_list + '/' + document['title'] + '.pdf' theMendeleyFile = config.mendeley_library + '/' + document['year'] + '/' + document['title'] + '.pdf' if os.path.exists(theFile): if (os.path.getmtime(theFile) < os.path.getmtime(theMendeleyFile)): shutil.copy2(theMendeleyFile, theFile) elif (os.path.getmtime(theFile) > os.path.getmtime(theMendeleyFile)): shutil.copy2(theFile, theMendeleyFile) else: shutil.copy2(theMendeleyFile, theFile)