【问题标题】:Python Google Contacts API - Deleting contactsPython Google Contacts API - 删除联系人
【发布时间】:2016-10-18 02:09:18
【问题描述】:

阅读开发者指南我发现了如何删除单个联系人:

def delete_contact(gd_client, contact_url):
  # Retrieving the contact is required in order to get the Etag.
  contact = gd_client.GetContact(contact_url)

  try:
    gd_client.Delete(contact)
  except gdata.client.RequestError, e:
    if e.status == 412:
      # Etags mismatch: handle the exception.
      pass

有没有办法删除所有联系人? 找不到这样做的方法。

大批量迭代每个联系人需要几分钟

【问题讨论】:

  • 如果可以使用api获取所有联系人,则可以遍历所有联系人并将其传递给delete_contact函数。
  • 删除1000个联系人大约需要9分钟。我正在寻找更快的解决方案

标签: python gdata google-contacts-api


【解决方案1】:

如果您要执行大量操作,请使用批处理请求。您可以让服务器使用单个 HTTP 请求执行多个操作。批处理请求一次限制为 100 个操作。您可以在Google Data APIs Batch Processing documentation 中找到有关批处理操作的更多信息。

delete all contacts 使用contactsrequest.Batch 操作。对于此操作,创建一个LIST<type>,为每个联系人项设置BatchData,然后将列表传递给contactsrequest.Batch 操作。

private void DeleteAllContacts()
{
RequestSettings rs = new RequestSettings(this.ApplicationName, this.userName, this.passWord);
rs.AutoPaging = true // this will result in automatic paging for listing and deleting all contacts
ContactsRequest cr = new ContactsRequest(rs);
Feed<Contact> f = cr.GetContacts();
List<Contact> list = new List<Contact>();
int i=0;
foreach (Contact c in f.Entries)
{
c.BatchData = new GDataBatchEntryData();
c..BatchData.Id = i.ToString();
c.BatchData.Type = GDataBatchOperationType.delete;
i++;
list.Add(c);
}
cr.Batch(list, new Uri(f.AtomFeed.Batch), GDataBatchOperationType.insert);
f = cr.GetContacts();
Assert.IsTrue(f.TotalResults == 0, "Feed should be empty now");
}

【讨论】:

  • 这对我来说可能是一个完美的解决方案 - python 的 gdata 客户端是否支持批处理?我在那里找不到。
  • 查看one
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-02-11
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多