Sto utilizzando l'API GMail su Python/Google App Engine. Ho una query che restituisce determinati ID di thread e ora vorrei ottenere l'ultimo messaggio di ogni thread. Poiché i risultati non sono necessariamente ordinati per data, mi chiedo quale sarebbe la chiamata API più efficiente per questo?GMail - Ricevi l'ultimo messaggio di una discussione
Sulla base dei commenti qui sotto ho impostare la seguente funzione batch:
if threads != []:
count = 0 #start a new batch request after every 1000 requests
batch = BatchHttpRequest(callback=get_items)
for t in threads:
batch.add(service.users().threads().get(userId=email, id=t), request_id=some_id)
count += 1
if count % 1000: #batch requests can handle max 1000 entries
batch.execute(http=http)
batch = BatchHttpRequest(callback=get_items)
if not count % 1000:
batch.execute(http=http)
Questo poi esegue get_items, che tra l'altro corre logica seguente per scoprire se l'ultima e-mail in un thread è stato un inviato articolo.
def get_items(request_id, response, exception):
if exception is not None:
print 'An error occurred: %s' % exception
else:
for m in response['messages']: #check each of the messages in the response
if m['historyId'] == response['historyId']: #if it equals the historyId of the thread
if 'SENT' in m['labelIds']: #and it is marked as a sent item
item = m #use this message for processing
Questo sembra funzionare per la maggior parte dei casi, tuttavia, ci sono casi in cui "voce", come creato in precedenza contiene 2 messaggi con differenti historyIds. Non sono sicuro di cosa lo stia causando e vorrei sapere prima di creare un work-around per questo ...
Secondo questo [link] [1] è possibile utilizzare la storia per ottenere ID ultimo messaggio di un filo [1]: https://developers.google. com/gmail/api/v1/reference/users/threads – cutteeth
Ah sì, ci ho pensato, non ero sicuro se in realtà lo avesse fatto. Un pensiero un nuovo ID di storia potrebbe anche essere aggiunto quando un messaggio viene eliminato. Lo scoprirò presto allora. Farò un tentativo, grazie. – Vincent
Sembra che funzioni nella maggior parte dei casi, tuttavia in alcuni casi, se corrispondo a historyId, ottengo più di 1 messaggio come risposta. Aggiungere il codice sopra. – Vincent