14
3

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 1 year has passed since last update.

QiitaにElixirタグ付きで登録されている記事をElixirで取得することを楽しむ

Last updated at Posted at 2022-04-17

風をいたみ岩打つ波のおのれのみくだけてものを思ふ頃かな

Advent Calendar 2022 100日目1の記事です。
I'm looking forward to 12/25,2022 :santa::santa_tone1::santa_tone2::santa_tone3::santa_tone4::santa_tone5:
私のAdvent Calendar 2022 一覧


はじめに

Elixirを楽しんでいますか:bangbang::bangbang::bangbang:

この記事は、Elixirタグ付きで登録されている記事をElixirのプログラムで取得してみます。
もちろんElixirを使います。

プログラム

さっそくプログラムです。
どうぞ〜。
ファイル名はsample.exsにしています。

sample.exs
Mix.install([:req], force: true)

tag = "Elixir"

Stream.iterate(1, &(&1 + 1))
|> Enum.reduce_while([], fn page, acc ->
  IO.puts("page: #{page}")

  %{body: body, status: 200, headers: headers} =
    URI.encode_query(%{query: "tag:#{tag}", per_page: 100, page: page})
    |> then(fn query -> "https://qiita.com/api/v2/items?#{query}" end)
    |> Req.get!(pool_timeout: 50000, receive_timeout: 50000)

  total_count =
    headers
    |> Enum.find(fn {key, _} -> key == "total-count" end)
    |> elem(1)
    |> String.to_integer()

  if total_count > (page * 100) do
    {:cont, body ++ acc}
  else
    {:halt, body ++ acc}
  end
end)
|> Enum.sort_by(fn %{"likes_count" => likes_count} -> likes_count end, :desc)
|> Jason.encode!(pretty: true)
|> then(&File.write!("output.json", &1))

実行

上記の通り、sample.exsを作ってみましょう。
実行してみましょう。

Elixirをインストールしていない方のためにDockerでの実行方法も書いておきます。

2022/04/17現在、約2,700件弱、Elixirタグの付いた記事がQiitaに登録されています。
そのため、page: 27となったところでAPIコールは停止します。

この27回を前提にすると、APIの利用制限は以下のようになっておりますので、現時点では認証なしでもすべての記事を取得できます。

認証している状態ではユーザごとに1時間に1000回まで、認証していない状態ではIPアドレスごとに1時間に60回までリクエストを受け付けます。

Docker

docker pull hexpm/elixir:1.13.4-erlang-23.0.2-debian-bullseye-20210902

docker run --rm -w /app -v `pwd`:/app hexpm/elixir:1.13.4-erlang-23.0.2-debian-bullseye-20210902 mix local.hex --force && elixir sample.exs

Elixirインストールしているよ

elixir sample.exs

macOS Catalina 10.15.7
Elixir 1.13.1 (compiled with Erlang/OTP 24)


Wrapping up :lgtm::lgtm::lgtm::lgtm::lgtm:

この記事は、Elixirタグ付きで登録されている記事をElixirのプログラムで取得してみることを楽しんでみました。

区切りの私のAdvent Calendar 2022 100日目の記事はこんなところで終わります。

Enjoy Elixir:bangbang::bangbang::bangbang:
$\huge{Enjoy\ Elixir🚀}$

以上です。


I organize autoracex.
And I take part in NervesJP, fukuoka.ex, EDI, tokyo.ex, Pelemay.
I hope someday you'll join us.

We Are The Alchemists, my friends!


  1. @kaizen_nagoya さんの「「@e99h2121 アドベントカレンダーではありますまいか Advent Calendar 2020」の改訂版ではありますまいか Advent Calendar 2022 1日目 Most Breakthrough Generator」から着想を得て、模倣いたしました。

14
3
1

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
14
3

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?