OSDN Git Service

add delete
[everfolder/source.git] / source / workspace / EverFolder / src / com / yuji / ef / utility / EvernoteUtil.java
1 package com.yuji.ef.utility;
2
3 import java.net.ConnectException;
4 import java.util.ArrayList;
5 import java.util.List;
6
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;
11
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;
26
27 public class EvernoteUtil {
28         private static final int N = 50;
29         private String username;
30         private String password;
31
32         public enum Error {
33                 NONE, CONNECT, INVALID_AUTH, NOT_FOUND, OTHER
34         };
35
36         private Error errorCode;
37
38         public static EvernoteUtil getInstance() {
39                 if (instance == null) {
40                         instance = new EvernoteUtil();
41                         instance.init();
42                 }
43                 return instance;
44         }
45
46         private static EvernoteUtil instance = null;
47
48         private EvernoteUtil() {
49                 errorCode = Error.NONE;
50         }
51
52         private String authenticationToken = null;
53         private NoteStore.Client noteStore = null;
54
55         public void setConfig(String username, String password) {
56                 this.username = username;
57                 this.password = password;
58                 authenticationToken = null;
59                 noteStore = null;
60         }
61
62         private void init() {
63
64         }
65
66         public Note getNote(String guid) throws EDAMNotFoundException {
67                 boolean withContent = true;
68                 boolean withResourcesData = false;
69                 boolean withResourcesRecognition = false;
70                 boolean withResourcesAlternateData = false;
71                 Note note = null;
72                 errorCode = Error.OTHER;
73                 try {
74                         String token = getAuthenticationToken();
75                         if (token == null) {
76                                 return null;
77                         }
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);
88                         throw e;
89                 } catch (TException e) {
90                         Debug.d(this, null, e);
91
92                         if (e.getCause() instanceof ConnectException) {
93                                 errorCode = Error.CONNECT;
94                         }
95                 }
96                 return note;
97         }
98
99         private NoteFilter getDefaultFilter(){
100                 // 検索条件として、検索語なし、更新日順ソートを指定
101                 NoteFilter filter = new NoteFilter();
102                 filter.setOrder(NoteSortOrder.UPDATED.getValue());
103                 filter.setAscending(false);             
104
105                 return filter;
106         }
107         
108         public List<NoteList> getNoteList() {
109                 NoteFilter filter = getDefaultFilter();
110                 return getNoteList(filter);
111         }
112
113         public List<NoteList> getNoteListByNotebook(String notebookGuid) {
114                 NoteFilter filter = getDefaultFilter();
115                 filter.setNotebookGuid(notebookGuid);
116                 return getNoteList(filter);
117         }
118         
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);
125         }
126         
127         public List<NoteList> getNoteList(NoteFilter filter) {
128                 errorCode = Error.OTHER;
129
130                 List<NoteList> list = new ArrayList<NoteList>();
131                 try {
132                         String token = getAuthenticationToken();
133                         if (token == null) {
134                                 return null;
135                         }
136                         int index = 0;
137                         while (true){
138                                 NoteList noteList = noteStore.findNotes(token, filter, index, N);
139                                 int size = noteList.getNotesSize();
140                                 
141                                 if (size <= 0){
142                                         break;
143                                 }
144                                 list.add(noteList);
145                                 if (size < N){
146                                         break;
147                                 }
148                                 index += size;
149                         }
150                         
151                         errorCode = Error.NONE;
152                 } catch (EDAMUserException e) {
153                         Debug.d(this, null, e);
154                         list = null;
155                 } catch (EDAMSystemException e) {
156                         Debug.d(this, null, e);
157                         list = null;
158                 } catch (EDAMNotFoundException e) {
159                         Debug.d(this, null, e);
160                         list = null;
161                 } catch (TException e) {
162                         Debug.d(this, null, e);
163
164                         if (e.getCause() instanceof ConnectException) {
165                                 errorCode = Error.CONNECT;
166                         }
167                         list = null;
168                 }
169                 return list;
170         }
171
172         public List<Notebook> getNoteBookList() {
173                 errorCode = Error.OTHER;
174
175                 List<Notebook> noteBookList = null;
176                 try {
177                         String token = getAuthenticationToken();
178                         if (token == null) {
179                                 return null;
180                         }
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);
189
190                         if (e.getCause() instanceof ConnectException) {
191                                 errorCode = Error.CONNECT;
192                         }
193                 }
194                 return noteBookList;
195         }
196
197         public List<Tag> getTagList() {
198                 errorCode = Error.OTHER;
199
200                 List<Tag> tagList = null;
201                 try {
202                         String token = getAuthenticationToken();
203                         if (token == null) {
204                                 return null;
205                         }
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);
214
215                         if (e.getCause() instanceof ConnectException) {
216                                 errorCode = Error.CONNECT;
217                         }
218                 }
219                 return tagList;
220         }
221
222         public Note updateNoteContext(String guid, String title, String text) {
223                 errorCode = Error.OTHER;
224
225                 Note note = null;
226                 try {
227                         note = getNote(guid);
228                 } catch (EDAMNotFoundException e) {
229                         errorCode = Error.NOT_FOUND;
230                 }
231                 if (note == null) {
232                         return null;
233                 }
234                 String content = note.getContent();
235
236                 // #27970
237                 // 見つからない場合
238                 String endTag = "</en-note>";
239                 int pos = content.indexOf(endTag);
240                 if (pos < 0) {
241                         return null;
242                 }
243                 StringBuffer sb = new StringBuffer();
244                 sb.append(content.substring(0, pos));
245                 if (title != null) {
246                         sb.append(title);
247                 }
248                 sb.append("<div>");
249                 sb.append(text);
250                 sb.append("</div>");
251                 sb.append(content.substring(pos));
252                 content = sb.toString();
253                 note.setContent(content);
254
255                 Note n = updateNote(note);
256
257                 errorCode = Error.NONE;
258                 return n;
259         }
260
261         public Note updateNote(Note note) {
262                 errorCode = Error.OTHER;
263                 Note n = null;
264                 try {
265                         String token = getAuthenticationToken();
266                         if (token == null) {
267                                 return null;
268                         }
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) {
276                         e.printStackTrace();
277                 } catch (TException e) {
278                         Debug.d(this, null, e);
279
280                         if (e.getCause() instanceof ConnectException) {
281                                 errorCode = Error.CONNECT;
282                         }
283                 }
284                 return n;
285         }
286
287         private String getAuthenticationToken() {
288                 try {
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(
295                                                 userStoreTrans);
296                                 UserStore.Client userStore = new UserStore.Client(userStoreProt);
297
298                                 // #27612
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
302                                 // を取得し直すべし
303
304                                 String consumerKey = "yuji_k64613";
305                                 String consumerSecret = "d5528b4fdb3a7fee";
306                                 AuthenticationResult authResult = userStore.authenticate(
307                                                 username, password, consumerKey, consumerSecret);
308                                 authenticationToken = authResult.getAuthenticationToken();
309
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(
317                                                 noteStoreTrans);
318                                 noteStore = new NoteStore.Client(noteStoreProt);
319                         }
320                 } catch (TTransportException e) {
321                         authenticationToken = null;
322                         noteStore = null;
323                         Debug.d(this, null, e);
324
325                         errorCode = Error.CONNECT;
326                 } catch (EDAMUserException e) {
327                         Debug.d(this, null, e);
328
329                         EDAMErrorCode code = e.getErrorCode();
330                         if (code.equals(EDAMErrorCode.INVALID_AUTH)) {
331                                 errorCode = Error.INVALID_AUTH;
332                         }
333                 } catch (EDAMSystemException e) {
334                         Debug.d(this, null, e);
335                 } catch (TException e) {
336                         Debug.d(this, null, e);
337
338                         if (e.getCause() instanceof ConnectException) {
339                                 errorCode = Error.CONNECT;
340                         }
341                 }
342
343                 return authenticationToken;
344         }
345
346         public Error getErrorCode() {
347                 return errorCode;
348         }
349
350 //      public String getErrorMessage(Context context, Error code) {
351 //              if (code.equals(Error.NONE)) {
352 //                      return "";
353 //              }
354 //              if (code.equals(Error.CONNECT)) {
355 //                      return context.getString(R.string.toastConnect);
356 //              }
357 //              if (code.equals(Error.INVALID_AUTH)) {
358 //                      return context.getString(R.string.toastInvalidAuth);
359 //              }
360 //              if (code.equals(Error.NOT_FOUND)) {
361 //                      return context.getString(R.string.toastNoteDoesNotExist);
362 //              }
363 //              return context.getString(R.string.toastEvernoteSystem);
364 //      }
365 //
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());
369 //              return note;
370 //      }
371 //
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);
376 //                      if (l == null){
377 //                              return null;
378 //                      }
379 //                      list.addAll(l);
380 //              }
381 //              return list;
382 //      }
383 //      
384 //      public List<com.yuji.ec.db.Note> getNoteContentList(NoteList noteList) {
385 //              errorCode = Error.OTHER;
386 //
387 //              List<com.yuji.ec.db.Note> list = new ArrayList<com.yuji.ec.db.Note>();
388 //              HtmlParser parser = new HtmlParser();
389 //
390 //              try {
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)){
399 //                                      return null;
400 //                              }
401 //                              String text = parser.getText();
402 //                              com.yuji.ec.db.Note note = toNote(nc, text);
403 //                              list.add(note);
404 //                      }
405 //                      notes = noteList.getNotes();
406 //                      s = noteList.getNotesSize();
407 //                      errorCode = Error.NONE;
408 //              } catch (EDAMNotFoundException e) {
409 //                      errorCode = Error.NOT_FOUND;
410 //                      return null;
411 //              }
412 //              return list;
413 //      }
414 //
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);
420 //                      list.add(item);
421 //              }
422 //              return list;
423 //      }
424 //
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);
430 //                      list.add(item);
431 //              }
432 //              return list;
433 //      }
434
435 }