Skip to content

Instantly share code, notes, and snippets.

@danicuki
Created March 18, 2011 01:17
Show Gist options
  • Save danicuki/875469 to your computer and use it in GitHub Desktop.
Save danicuki/875469 to your computer and use it in GitHub Desktop.
imports google contacts to locaweb email marketing account
EMAIL_MKT_CHAVE = "[SUA_CHAVE]"
GOOGLE_EMAIL = "[SEU EMAIL NO GOOGLE]"
GOOGLE_PASSWD = "[SENHA GOOGLE]"
EMAIL_MKT_HOST = "[MAQUINA EMAIL MKT]"
EMAIL_MKT_LISTS = "11115"
EMAIL_MKT_LOGIN = "[LOGIN EMAIL MKT]"
EMAIL_MKT_URL = "http://#{EMAIL_MKT_HOST}.locaweb.com.br/admin/api/#{EMAIL_MKT_LOGIN}/contatos/importacao/?chave=#{EMAIL_MKT_CHAVE}&listas=#{EMAIL_MKT_LISTS}"
login = {:accountType => "HOSTED_OR_GOOGLE",
:Email => GOOGLE_EMAIL,
:Passwd => GOOGLE_PASSWD,
:service => "cp",
:source => "danicuki-teste-1"
}
a = RestClient.post("https://www.google.com/accounts/ClientLogin", login)
auth = {"Authorization" => "GoogleLogin auth=#{a.split.last.split("=")[1]}"}
c = RestClient.get("https://www.google.com/m8/feeds/contacts/default/full?max-results=2000", auth)
contacts = Hash.from_xml(c)["feed"]["entry"]
emails = contacts.select {|c| !c["title"].blank?}.select {|c| !c["email"].blank?}
email_mkt_hash = emails.map do |c|
{:nome => c["title"].split[0].camelize, :email => (c["email"][0] ? c["email"][0]["address"] : c["email"]["address"])}
end
RestClient.post(EMAIL_MKT_URL, email_mkt_hash.to_json)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment