折腾:
【已解决】Python中更新印象笔记中帖子中附件图片的数据
期间,现在搞清楚:
如何用Python的代码,去实现印象笔记的帖子,即note =post,去更新内容。
此处要更新的内容是:图片
对应着资源列表resources
以及,已经更新过了对应的content了。
现在只剩,调用接口,更新到印象笔记中。
知道是用NoteStore.updateNote
但是具体如何传递参数,需要搞清楚
evernote updateNote
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 | Function: NoteStore.updateNote Types.Note updateNote(string authenticationToken, Types.Note note) throws Errors.EDAMUserException, Errors.EDAMSystemException, Errors.EDAMNotFoundException Submit a set of changes to a note to the service. The provided data must include the note 's guid field for identification. The note' s title must also be set . @param note A Note object containing the desired fields to be populated on the service. With the exception of the note's title and guid, fields that are not being changed do not need to be set . If the content is not being modified, note.content should be left unset . If the list of resources is not being modified, note.resources should be left unset . @ return The metadata (no contents) for the Note on the server after the update. The Note.sharedNotes field will not be set . @throws EDAMUserException * BAD_DATA_FORMAT "Note.title" - invalid length or pattern * BAD_DATA_FORMAT "Note.content" - invalid length for ENML body * BAD_DATA_FORMAT "NoteAttributes.*" - bad resource string * BAD_DATA_FORMAT "ResourceAttributes.*" - bad resource string * BAD_DATA_FORMAT "Resource.mime" - invalid resource MIME type * DATA_CONFLICT "Note.deleted" - deleted time set on active note * DATA_REQUIRED "Resource.data" - resource data body missing * ENML_VALIDATION "*" - note content doesn't validate against DTD * LIMIT_REACHED "Note.tagGuids" - too many Tags on Note * LIMIT_REACHED "Note.resources" - too many resources on Note * LIMIT_REACHED "Note.size" - total note size too large * LIMIT_REACHED "Resource.data.size" - resource too large * LIMIT_REACHED "NoteAttribute.*" - attribute string too long * LIMIT_REACHED "ResourceAttribute.*" - attribute string too long * PERMISSION_DENIED "Note.notebookGuid" - user doesn't own destination * PERMISSION_DENIED "Note.tags" - user doesn 't have permission to modify the note' s tags. note.tags must be unset . * PERMISSION_DENIED "Note.attributes" - user doesn 't have permission to modify the note' s attributes. note.attributes must be unset . * QUOTA_REACHED "Accounting.uploadLimit" - note exceeds upload quota * BAD_DATA_FORMAT "Tag.name" - Note.tagNames was provided, and one of the specified tags had an invalid length or pattern * LIMIT_REACHED "Tag" - Note.tagNames was provided, and the required new tags would exceed the maximum number per account @throws EDAMNotFoundException * "Note.guid" - note not found, by GUID * "Note.notebookGuid" - if notebookGuid provided, but not found |
意思是:
必须要包含 title标题和guid
剩下字段:需要更新的,就包含在内,否则就不要设置值
那去试试,只设置title+guid后,再去加上 更新后的content和resources
那看起来,或许是需要,新建一个note,然后加上上面的值,再去调用更新?
看到了,官网的示例代码
就是这么做的:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 | note = Types.Note() note.title = "I will soon have application data" note.content = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>" note.content + = "<!DOCTYPE en-note SYSTEM \" http: / / xml.evernote.com / pub / enml2.dtd \ ">" note.content + = "<en-note>Content goes here</en-note>" note = note_store.createNote(note) # Now we have the note GUID and can add applicationData dkey = "my-consumer-key" # the first part of your API key dval = "I'm the value: an arbitrary string" note_store.setNoteApplicationDataEntry(note.guid, dkey, dval) note = note_store.updateNote(note) |
不过不是,而是演示了新建后,再去更新
setNoteApplicationDataEntry
算了,自己去尝试看看
不过从上述内容看,好像没有新建一个Note,再去设置对应值,然后再去更新
不过看了官网文档,感觉还是:最好新建一个,再去更新。
去试试
1 2 3 4 5 6 7 | # upload/sync to evernote newNote = Types.Note() newNote.title = curNoteDetail.title newNote.guid = curNoteDetail.guid # updated following newNote.content = curNoteDetail.content newNote.resources = curNoteDetail.resources |
去调试。
继续优化:
1 2 3 4 5 6 7 8 9 10 11 | # upload/sync to evernote newNote = Types.Note() newNote.title = curNoteDetail.title newNote.guid = curNoteDetail.guid # updated following newNote.content = curNoteDetail.content newNote.resources = curNoteDetail.resources updatedNote = gNoteStore.updateNote(newNote) # return curNoteDetail return updatedNote |
去调试:
title+guid
然后加上
content和resources

可以更新了,对比,之前的curNoteDetail:

和更新后的updatedNote:

变化是:
- contentLength
- 793 -> 818
- 看似正常
- content:
- 有值 -> None
- 很奇怪
- 后记:是正常的
- https://dev.yinxiang.com/doc/reference/NoteStore.html#Fn_NoteStore_updateNote
- @return The metadata (no contents) for the Note on the server after the update
- (即使)更新成功后,返回的内容中,也是没有content的
- 估计是怕content值太大,返回比较浪费资源
- contentHash
- 变了:
- b’\x9c\xe8\xa0H|x3j\x05Q\xacJ\xc6\xda\x93\x15′
- ->
- b”2\xa8\xed\x83\xa2F’\x02\xc1\x92DB\xb0i\xf2\xb9″
- -》正常
- updateSequenceNum
- 4555831 -> 4560714
- updated
- 1583760123000 -> 1583850369000
- = 2020-03-09 21:22:03 -> 2020-03-10 22:26:09
- 感觉正常
-》所以现在去:
用此处Mac中印象笔记软件打开帖子,看看:
图片是否被压缩了:
的确是压缩了图片:

以及帖子内容更新了了:

证明此处update成功了。
【总结】
此处,想要更新印象笔记中的图片资源列表和图片content本身
对应代码是:
1 2 3 4 5 6 7 8 9 10 11 12 | import evernote.edam. type .ttypes as Types curNoteDetail = updateNoteResouces(curNoteDetail, newResList) # upload/sync to evernote newNote = Types.Note() newNote.title = curNoteDetail.title newNote.guid = curNoteDetail.guid # updated following newNote.content = curNoteDetail.content newNote.resources = curNoteDetail.resources updatedNote = gNoteStore.updateNote(newNote) |
其中gNoteStore是之前就初始化好的:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 | def initEvernote(): """Init evernote things""" global gClient, gUserStore, gNoteStore gClient = EvernoteClient( token = AuthToken, # sandbox=isSandbox, # china=china, service_host = ServiceHost ) logging.info( "gClient=%s" , gClient) gUserStore = gClient.get_user_store() logging.info( "gUserStore=%s" , gUserStore) isVersionOk = gUserStore.checkVersion( "Evernote EDAMTest (Python)" , EDAM_VERSION_MAJOR, # UserStoreConstants.EDAM_VERSION_MAJOR, EDAM_VERSION_MINOR, # UserStoreConstants.EDAM_VERSION_MINOR ) logging.info( "Is my Evernote API version up to date? %s" , isVersionOk) gNoteStore = gClient.get_note_store() logging.info( "gNoteStore=%s" , gNoteStore) |
核心逻辑:
除了必须要传递title和guid外
其他只传递,需要更新的字段的值
不需要更新的字段,不要设置(默认保留None)即可。
具体解释参见官网:
【后记 20201125】
然后去:
【未解决】Python更新印象笔记帖子的笔记本
转载请注明:在路上 » 【已解决】Python更新印象笔记note帖子的内容