4
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

【小ネタ】さっそく、ジブリの画像をダウンロードする(Elixir)

Posted at

はじめに

0. プロジェクトの作成

$ mix new ghibli_download
$ cd ghibli_download

1. HTTPoisonの導入

mix.exs
  defp deps do
    [
      {:httpoison, "~> 1.6"}
    ]
  end
$ mix deps.get
  • 依存関係の解決

2. ソースコードを書く

lib/ghibli_download.ex
defmodule GhibliDownload do
  @works ~W{marnie kaguyahime kazetachinu kokurikozaka karigurashi ponyo ged chihiro}
  @url_base "http://www.ghibli.jp/gallery"
  @ext ".jpg"
  @local_base "#{System.user_home()}/memo/ghibli"

  def run do
    @works
    |> Enum.flat_map(&urls(&1, 50))
    |> Enum.map(fn {url, file_name} ->
      IO.puts(url)
      download(url, file_name)
      Process.sleep(1000 * 20)
    end)
  end

  defp urls(work, num) do
    1..num
    |> Enum.map(&{"#{@url_base}/#{file_name(work, &1)}", file_name(work, &1)})
  end

  defp file_name(work, num) do
    "#{work}#{Integer.to_string(num) |> String.pad_leading(3, "0")}#{@ext}"
  end

  defp download(url, file_name) do
    HTTPoison.get!(url)
    |> Map.get(:body)
    |> (fn content -> File.write("#{@local_base}/#{file_name}", content) end).()
  end
end

3. 実行

$ iex -S mix

iex> GhibliDownload.run
  • 息継ぎをいれたので2時間半くらい放っておけば終わっていることでしょう

Wrapping Up

4
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
4
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?