2017-06-01 57 views
0

我已經寫了一小段代碼,將我的筆記標記名稱追加到標題,然後刪除所有相關的標籤。當我嘗試在生產環境中運行時,我很快達到了速率限制。有人可以幫我優化這段代碼嗎?或者我應該要求Evernote提供特殊的費率限制嗎?的Evernote的Python API - 命中率限制

另外,我得到一個錯誤,當一個音符沒有標記。沒有辦法有效地從一個音符獲得標籤的數量,所以我沒有得到這個錯誤嗎?

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 

回答

0

這是我如何處理速率限制,在一個限速代理包裹EvernoteClient(基於http://code.activestate.com/recipes/496741-object-proxying/

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) 
+1

如何定義'evernote_wait_try_again'? – jwodder

+0

固定。見https://github.com/rdhyee/evernote-web-utility/blob/0.0.6/EvernoteWebUtil/__init__.py#L55-L80在上下文中。 –