Evernote Python API - 达到率限制

时间:2017-06-01 07:26:06

标签: python api evernote

我写了一小段代码,将我的笔记的标签名称附加到标题上,然后删除所有相关标签。当我尝试在生产中运行时,我很快就达到了速率限制。有人可以帮我优化这段代码吗?或者我应该使用Evernote申请特殊费率限制吗?

另外,当笔记没有标签时,我收到错误。有效地从一个音符中获取标签数量的任何方法,所以我没有得到错误?

from evernote.api.client import EvernoteClient
from evernote.edam.notestore import NoteStore

dev_token = "dev_token"
client = EvernoteClient(token=dev_token, sandbox = False)
userStore = client.get_user_store()
user = userStore.getUser()
print
print user.username
print

noteStore = client.get_note_store()
notebooks = noteStore.listNotebooks()

for n in notebooks:
   print "Notebook = " + n.name + " GUID = " + n.guid

   filter = NoteStore.NoteFilter()
   filter.ascending = False
   filter.notebookGuid=n.guid

   spec = NoteStore.NotesMetadataResultSpec()
   spec.includeTitle = True
   spec.includeNotebookGuid = True
   spec.includeTagGuids = True

   notesMetadataList = noteStore.findNotesMetadata(filter, 0, 25, spec)
   for noteMetadata in notesMetadataList.notes:
      print "%s :: %s" % (noteMetadata.title, noteMetadata.guid)
      newNoteTitle = noteMetadata.title + " -- "
      for tagGuid in noteMetadata.tagGuids:
        tag = noteStore.getTag(tagGuid)
        tagName = tag.name
        print tagName
        newNoteTitle = newNoteTitle + " " + tagName
     print "newNoteTitle = " + newNoteTitle
     noteMetadata.title = newNoteTitle
     noteMetadata.tagGuids = []
     noteMetadata = noteStore.updateNote(noteMetadata)
     print noteMetadata.title

1 个答案:

答案 0 :(得分:0)

通过将EvernoteClient打包到速率限制代理中(基于{{3}})

来处理速率限制的方法
from time import sleep
from evernote.api.client import EvernoteClient
from evernote.edam.error.ttypes import (EDAMSystemException, EDAMErrorCode)


def evernote_wait_try_again(f):
    """
    Wait until mandated wait and try again
    http://dev.evernote.com/doc/articles/rate_limits.php
    """

    def f2(*args, **kwargs):
        try:
            return f(*args, **kwargs)
        except EDAMSystemException as e:
            if e.errorCode == EDAMErrorCode.RATE_LIMIT_REACHED:
                print("rate limit: {0} s. wait".format(e.rateLimitDuration))
                sleep(e.rateLimitDuration)
                print("wait over")
                return f(*args, **kwargs)

    return f2

class RateLimitingEvernoteProxy(object):
    __slots__ = ["_obj"]
    def __init__(self, obj):
        object.__setattr__(self, "_obj", obj)

    def __getattribute__(self, name):
        return evernote_wait_try_again(
            getattr(object.__getattribute__(self, "_obj"), name))

_client = EvernoteClient(token=auth_token, sandbox=sandbox)
client = RateLimitingEvernoteProxy(_client)