1 package com.yuji.ef.utility;
3 import java.net.ConnectException;
4 import java.util.ArrayList;
7 import org.apache.thrift.TException;
8 import org.apache.thrift.protocol.TBinaryProtocol;
9 import org.apache.thrift.transport.THttpClient;
10 import org.apache.thrift.transport.TTransportException;
12 import com.evernote.edam.error.EDAMErrorCode;
13 import com.evernote.edam.error.EDAMNotFoundException;
14 import com.evernote.edam.error.EDAMSystemException;
15 import com.evernote.edam.error.EDAMUserException;
16 import com.evernote.edam.notestore.NoteFilter;
17 import com.evernote.edam.notestore.NoteList;
18 import com.evernote.edam.notestore.NoteStore;
19 import com.evernote.edam.type.Note;
20 import com.evernote.edam.type.NoteSortOrder;
21 import com.evernote.edam.type.Notebook;
22 import com.evernote.edam.type.Tag;
23 import com.evernote.edam.type.User;
24 import com.evernote.edam.userstore.AuthenticationResult;
25 import com.evernote.edam.userstore.UserStore;
27 public class EvernoteUtil {
28 private static final int N = 50;
29 private String username;
30 private String password;
33 NONE, CONNECT, INVALID_AUTH, NOT_FOUND, OTHER
36 private Error errorCode;
38 public static EvernoteUtil getInstance() {
39 if (instance == null) {
40 instance = new EvernoteUtil();
46 private static EvernoteUtil instance = null;
48 private EvernoteUtil() {
49 errorCode = Error.NONE;
52 private String authenticationToken = null;
53 private NoteStore.Client noteStore = null;
55 public void setConfig(String username, String password) {
56 this.username = username;
57 this.password = password;
58 authenticationToken = null;
66 public Note getNote(String guid) throws EDAMNotFoundException {
67 boolean withContent = true;
68 boolean withResourcesData = false;
69 boolean withResourcesRecognition = false;
70 boolean withResourcesAlternateData = false;
72 errorCode = Error.OTHER;
74 String token = getAuthenticationToken();
78 note = noteStore.getNote(token, guid, withContent,
79 withResourcesData, withResourcesRecognition,
80 withResourcesAlternateData);
81 errorCode = Error.NONE;
82 } catch (EDAMUserException e) {
83 Debug.d(this, null, e);
84 } catch (EDAMSystemException e) {
85 Debug.d(this, null, e);
86 } catch (EDAMNotFoundException e) {
87 Debug.d(this, null, e);
89 } catch (TException e) {
90 Debug.d(this, null, e);
92 if (e.getCause() instanceof ConnectException) {
93 errorCode = Error.CONNECT;
99 private NoteFilter getDefaultFilter(){
100 // 検索条件として、検索語なし、更新日順ソートを指定
101 NoteFilter filter = new NoteFilter();
102 filter.setOrder(NoteSortOrder.UPDATED.getValue());
103 filter.setAscending(false);
108 public List<NoteList> getNoteList() {
109 NoteFilter filter = getDefaultFilter();
110 return getNoteList(filter);
113 public List<NoteList> getNoteListByNotebook(String notebookGuid) {
114 NoteFilter filter = getDefaultFilter();
115 filter.setNotebookGuid(notebookGuid);
116 return getNoteList(filter);
119 public List<NoteList> getNoteListByTag(String tagGuid) {
120 NoteFilter filter = getDefaultFilter();
121 List<String> tagGuids = new ArrayList<String>();
122 tagGuids.add(tagGuid);
123 filter.setTagGuids(tagGuids);
124 return getNoteList(filter);
127 public List<NoteList> getNoteList(NoteFilter filter) {
128 errorCode = Error.OTHER;
130 List<NoteList> list = new ArrayList<NoteList>();
132 String token = getAuthenticationToken();
138 NoteList noteList = noteStore.findNotes(token, filter, index, N);
139 int size = noteList.getNotesSize();
151 errorCode = Error.NONE;
152 } catch (EDAMUserException e) {
153 Debug.d(this, null, e);
155 } catch (EDAMSystemException e) {
156 Debug.d(this, null, e);
158 } catch (EDAMNotFoundException e) {
159 Debug.d(this, null, e);
161 } catch (TException e) {
162 Debug.d(this, null, e);
164 if (e.getCause() instanceof ConnectException) {
165 errorCode = Error.CONNECT;
172 public List<Notebook> getNoteBookList() {
173 errorCode = Error.OTHER;
175 List<Notebook> noteBookList = null;
177 String token = getAuthenticationToken();
181 noteBookList = noteStore.listNotebooks(token);
182 errorCode = Error.NONE;
183 } catch (EDAMUserException e) {
184 Debug.d(this, null, e);
185 } catch (EDAMSystemException e) {
186 Debug.d(this, null, e);
187 } catch (TException e) {
188 Debug.d(this, null, e);
190 if (e.getCause() instanceof ConnectException) {
191 errorCode = Error.CONNECT;
197 public List<Tag> getTagList() {
198 errorCode = Error.OTHER;
200 List<Tag> tagList = null;
202 String token = getAuthenticationToken();
206 tagList = noteStore.listTags(token);
207 errorCode = Error.NONE;
208 } catch (EDAMUserException e) {
209 Debug.d(this, null, e);
210 } catch (EDAMSystemException e) {
211 Debug.d(this, null, e);
212 } catch (TException e) {
213 Debug.d(this, null, e);
215 if (e.getCause() instanceof ConnectException) {
216 errorCode = Error.CONNECT;
222 public Note updateNoteContext(String guid, String title, String text) {
223 errorCode = Error.OTHER;
227 note = getNote(guid);
228 } catch (EDAMNotFoundException e) {
229 errorCode = Error.NOT_FOUND;
234 String content = note.getContent();
238 String endTag = "</en-note>";
239 int pos = content.indexOf(endTag);
243 StringBuffer sb = new StringBuffer();
244 sb.append(content.substring(0, pos));
251 sb.append(content.substring(pos));
252 content = sb.toString();
253 note.setContent(content);
255 Note n = updateNote(note);
257 errorCode = Error.NONE;
261 public Note updateNote(Note note) {
262 errorCode = Error.OTHER;
265 String token = getAuthenticationToken();
269 n = noteStore.updateNote(token, note);
270 errorCode = Error.NONE;
271 } catch (EDAMUserException e) {
272 Debug.d(this, null, e);
273 } catch (EDAMSystemException e) {
274 Debug.d(this, null, e);
275 } catch (EDAMNotFoundException e) {
277 } catch (TException e) {
278 Debug.d(this, null, e);
280 if (e.getCause() instanceof ConnectException) {
281 errorCode = Error.CONNECT;
287 private String getAuthenticationToken() {
289 if (authenticationToken == null) {
290 // String userStoreUrl =
291 // "https://sandbox.evernote.com/edam/user";
292 String userStoreUrl = "https://www.evernote.com/edam/user";
293 THttpClient userStoreTrans = new THttpClient(userStoreUrl);
294 TBinaryProtocol userStoreProt = new TBinaryProtocol(
296 UserStore.Client userStore = new UserStore.Client(userStoreProt);
299 // http://www.antun.net/tips/api/evernote.html
300 // expiredTime=time.time()+(authResult.expiration/1000.0-authResult.currentTime/1000.0)
301 // # expiredTime<time.time() なら認証し直して、authenticationToken
304 String consumerKey = "yuji_k64613";
305 String consumerSecret = "d5528b4fdb3a7fee";
306 AuthenticationResult authResult = userStore.authenticate(
307 username, password, consumerKey, consumerSecret);
308 authenticationToken = authResult.getAuthenticationToken();
310 // String noteStoreUrlBase =
311 // "https://sandbox.evernote.com/edam/note/";
312 String noteStoreUrlBase = "https://www.evernote.com/edam/note/";
313 User user = authResult.getUser();
314 String noteStoreUrl = noteStoreUrlBase + user.getShardId();
315 THttpClient noteStoreTrans = new THttpClient(noteStoreUrl);
316 TBinaryProtocol noteStoreProt = new TBinaryProtocol(
318 noteStore = new NoteStore.Client(noteStoreProt);
320 } catch (TTransportException e) {
321 authenticationToken = null;
323 Debug.d(this, null, e);
325 errorCode = Error.CONNECT;
326 } catch (EDAMUserException e) {
327 Debug.d(this, null, e);
329 EDAMErrorCode code = e.getErrorCode();
330 if (code.equals(EDAMErrorCode.INVALID_AUTH)) {
331 errorCode = Error.INVALID_AUTH;
333 } catch (EDAMSystemException e) {
334 Debug.d(this, null, e);
335 } catch (TException e) {
336 Debug.d(this, null, e);
338 if (e.getCause() instanceof ConnectException) {
339 errorCode = Error.CONNECT;
343 return authenticationToken;
346 public Error getErrorCode() {
350 // public String getErrorMessage(Context context, Error code) {
351 // if (code.equals(Error.NONE)) {
354 // if (code.equals(Error.CONNECT)) {
355 // return context.getString(R.string.toastConnect);
357 // if (code.equals(Error.INVALID_AUTH)) {
358 // return context.getString(R.string.toastInvalidAuth);
360 // if (code.equals(Error.NOT_FOUND)) {
361 // return context.getString(R.string.toastNoteDoesNotExist);
363 // return context.getString(R.string.toastEvernoteSystem);
366 // public static com.yuji.ec.db.Note toNote(Note n, String text) {
367 // com.yuji.ec.db.Note note = new com.yuji.ec.db.Note(n.getGuid(), text,
368 // n.getCreated(), n.getUpdated());
372 // public List<com.yuji.ec.db.Note> getNoteContentList(List<NoteList> noteListList) {
373 // List<com.yuji.ec.db.Note> list = new ArrayList<com.yuji.ec.db.Note>();
374 // for (NoteList noteList : noteListList){
375 // List<com.yuji.ec.db.Note> l = getNoteContentList(noteList);
384 // public List<com.yuji.ec.db.Note> getNoteContentList(NoteList noteList) {
385 // errorCode = Error.OTHER;
387 // List<com.yuji.ec.db.Note> list = new ArrayList<com.yuji.ec.db.Note>();
388 // HtmlParser parser = new HtmlParser();
391 // List<Note> notes = noteList.getNotes();
392 // int s = noteList.getNotesSize();
393 // System.out.println("" + s);
394 // for (Note n : notes) {
395 // String guid = n.getGuid();
396 // Note nc = getNote(guid);
397 // String content = nc.getContent();
398 // if (!parser.parse(content)){
401 // String text = parser.getText();
402 // com.yuji.ec.db.Note note = toNote(nc, text);
405 // notes = noteList.getNotes();
406 // s = noteList.getNotesSize();
407 // errorCode = Error.NONE;
408 // } catch (EDAMNotFoundException e) {
409 // errorCode = Error.NOT_FOUND;
415 // public List<INoteItem> toNoteItemFromNoteBook(List<Notebook> noteList) {
416 // List<INoteItem> list = new ArrayList<INoteItem>();
417 // for (Notebook notebook : noteList) {
418 // com.yuji.ec.db.Note item = new com.yuji.ec.db.Note(
419 // notebook.getGuid(), notebook.getName(), -1, -1);
425 // public List<INoteItem> toNoteItemFromTag(List<Tag> tagList) {
426 // List<INoteItem> list = new ArrayList<INoteItem>();
427 // for (Tag tag : tagList) {
428 // com.yuji.ec.db.Note item = new com.yuji.ec.db.Note(tag.getGuid(),
429 // tag.getName(), -1, -1);