Jump to content

Welcome! You're currently a Guest.

If you'd like to join in the Discussion, or access additional features in our forums, please sign in with your Evernote Account here. Have an Evernote Account but forgot your password? Reset it! Don't have an account yet? Create One! You'll need to set your Display Name before your first post.

Photo

Node.js code snippet for syncing with Evernote


  • Please log in to reply
1 reply to this topic

#1 berryboy

berryboy

  • Pip
  • Title: Member
  • Group: Members
  • 42 posts

Posted 27 July 2012 - 09:11 AM

About two week ago, I shared Evernote example in Node.js. And, this week, I received an email asking about how to sync with Evernote. So, I would like to share the code snippet here and hope it would be useful.

This snippet is based on Everest.js and Evernote document in Synchronization spec.

I hope it would be useful.
Any comment is very welcome :)

evercalendar.syncNotes = function (user, needFullSync, callback){

	 //IF needFullSync OR a new user
	 if(needFullSync || !user.lastSyncTime || !user.fullSynchronized) return fullSync();

	 evernote.getSyncState(user, function(err, syncState) {

		 if(err) return callback(err);
		
		 //No need to sync
		 if( syncState.updateCount == user.lastUpdateCount ) return finishSync();
		
		 //Require fullSync from Evernote Server
		 if( syncState.fullSyncBefore > user.lastSyncTime ) return fullSync();
		 return incrementalSync(syncState);
	 });

	 function fullSync(){

		 user.fullSynchronized = false;
		
		 //================ CLEAR OLD DATA HERE =================
         // Database.deleteAllNotes(user)
						
		 evernote.getSyncChunk(user, 0, 256, true, gotSyncChunk);

		 function gotSyncChunk(err, syncChunk){

			 if(err) return callback(err);

			 syncChunk.notes = syncChunk.notes || [];
			 syncChunk.notes.forEach(function(note,index) {
				
				 //Don't save note in the trash
				 if(!note.active) return;
					
				 //================ SAVE NOTE HERE ================
                 // Database.saveNote(user, note)
			 });

			 //Update User's status
			 user.lastUpdateCount = syncChunk.chunkHighUSN;
			 user.lastSyncTime = syncChunk.currentTime;

			 //IF have more chunk -> Recursive Loop
			 if(syncChunk.chunkHighUSN < syncChunk.updateCount)
				 return evernote.getSyncChunk(user, syncChunk.chunkHighUSN, 256, true, gotSyncChunk);

			 user.fullSynchronized = true;
			 return finishSync();
		 }
	 }

	 function incrementalSync(syncState){

		 evernote.getSyncChunk(user, user.lastUpdateCount, 256, false, gotSyncChunk);

		 function gotSyncChunk(err, syncChunk){

			 if(err) return callback(err);

			 syncChunk.notes = syncChunk.notes || [];
			 syncChunk.notes.forEach(function(note,index) {

				 if(note.active) {
						 //================ SAVE NOTE HERE ================
                         // Database.saveNote(user, note)
				 } else {
					 //================ DELETE NOTE HERE ================
                     // Database.deleteNote(user, note)
				 }
			 });

			 syncChunk.expungedNotes = syncChunk.expungedNotes || [];
			 syncChunk.expungedNotes.forEach(function(guid, index) {
				 //================ DELETE NOTE HERE ================
                 // Database.deleteNote(user, note)
			 });
			
			 //Update User's status
			 user.lastUpdateCount = syncChunk.chunkHighUSN;
			 user.lastSyncTime = syncChunk.currentTime;

			 //Load more chunk...
			 if(syncChunk.chunkHighUSN < syncChunk.updateCount)
				 return evernote.getSyncChunk(user, syncChunk.chunkHighUSN, 256, false, gotSyncChunk);

			 return finishSync();
		 }
	 }

	 function finishSync(){
		 //====== DONT FORGET TO SAVE USER STATUS HERE ==========
         // Database.saveStatus(user)
		 return callback(null,true);
	 }
}


#2 dipanshu

dipanshu

  • PipPip
  • Title: Alliance Lackey
  • Group: Members
  • 72 posts

Posted 21 August 2012 - 07:19 AM

can anyone post coding for update and delete notes in evernote to sync with external application in php




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users

Clip to Evernote