Skip to content

Instantly share code, notes, and snippets.

@watsy0007
Last active December 9, 2021 09:50
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save watsy0007/2fd12b780ea356e1e97e7db20966eeb2 to your computer and use it in GitHub Desktop.
Save watsy0007/2fd12b780ea356e1e97e7db20966eeb2 to your computer and use it in GitHub Desktop.
yandex livebook

yandex邮箱注册

安装依赖

Mix.install([
  # https://github.com/edgurgel/httpoison
  {:httpoison, "~> 1.8"},
  # https://github.com/devinus/poison
  {:poison, "~> 5.0"},
  {:csv, "~> 2.4"},
  {:kino, "~> 0.4"}
])

HTTPoison.start()

封装module

defmodule Yandex do
  def register_mailbox(username, token, domain) do
    url = "https://pddimp.yandex.ru/api2/admin/email/add"
    headers = %{"PddToken" => token, "Content-Type" => "application/x-www-form-urlencoded"}

    body =
      URI.encode_query(%{
        "domain" => domain,
        "login" => username,
        "password" => "#{username}#2022!"
      })

    result = HTTPoison.post!(url, body, headers).body |> Poison.decode!()
    IO.inspect(body)
    IO.inspect(result)
    Map.get(result, "success")
  end

  def batch_register_mailbox(usernames, token \\ "", domain \\ "") do
    usernames
    |> Stream.map(&register_mailbox(&1, token, domain))
    |> Stream.run()
  end
end
domainInput = Kino.Input.text("domain")
tokenInput = Kino.Input.text("token")
domain = Kino.Input.read(domainInput) |> String.trim()
token = Kino.Input.read(tokenInput) |> String.trim()
"~/Downloads/mail.csv"
|> Path.expand(__DIR__)
|> File.stream!()
|> CSV.decode()
|> Enum.take(100)
|> Enum.drop(1)
|> Enum.map(fn {:ok, [_, name, email, _, _]} ->
  {String.trim(name),
   Yandex.register_mailbox(String.downcase(List.first(String.split(email, "@"))), token, domain)}
end)
|> Enum.into(%{})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment