LoginSignup
15
0

完走賞を目指してみましょう! on Livebook

Last updated at Posted at 2022-12-01

$\huge{元氣ですかーーーーッ!!!}$

はじめに

2022/12/12 更新

最新のデータに更新しました

この記事は @torifukukaiou さんの記事をパクったリスペクトしたものです

アカウント毎に Qiita Advent Calendar 2022 に何件記事を投稿したかを Elixir で取得しています

これで自分が完走賞がもらえそうなのか分かりますね!

すごく面白かったので、私は愛すべきLivebookで動かしてみました

ノートブックはこちら

実行環境

Livebook 0.8.0 の Docker イメージを元にしたコンテナで動かしました

コンテナ定義はこちらを参照

セットアップ

必要なものをインストールします

せっかく Livebook なので Explorer と VegaLite で可視化しましょう

Mix.install([
  {:req, "~> 0.3"},
  {:floki, "~> 0.34"},
  {:explorer, "~> 0.4"},
  {:kino, "~> 0.8"},
  {:kino_vega_lite, "~> 0.1"}
])

エイリアスなどを準備します

require Explorer.DataFrame を忘れると、 Query の機能が使えないので気をつけましょう

alias Explorer.DataFrame
alias Explorer.Series
alias VegaLite, as: Vl
require Explorer.DataFrame

カレンダーデータの取得

@torifukukaiou さんのコードを参考に、ひとつなぎにしました

最後に dbg を付けているので、途中経過を楽しめます

calendar_df =
  "https://qiita.com/advent-calendar/2022/elixir"
  |> Req.get!()
  |> then(&(&1.body))
  |> Floki.parse_document!()
  |> Floki.find("[data-js-react-on-rails-store=AppStoreWithReactOnRails]")
  |> Enum.at(0)
  |> elem(2)
  |> Jason.decode!()
  |> get_in(["adventCalendars", "tableAdventCalendars"])
  |> Enum.at(0)
  |> Map.fetch!("items")
  |> Enum.frequencies_by(&get_in(&1, ["user", "urlName"]))
  |> then(fn map ->
    %{
      name: Enum.map(map, fn {name, _} -> name end),
      count: Enum.map(map, fn {_, count} -> count end),
    }
  end)
  |> DataFrame.new()
  |> dbg()

cal.gif

データテーブルの表示

calendar_df
|> DataFrame.arrange(desc: count)
|> Kino.DataTable.new()

スクリーンショット 2022-12-12 10.51.16.png

どうやら何とか完走賞は貰えそうです

グラフの表示

x = Series.to_list(calendar_df["name"])
y = Series.to_list(calendar_df["count"])

Vl.new(width: 800, height: 400)
|> Vl.data_from_values(x: x, y: y)
|> Vl.mark(:bar)
|> Vl.encode_field(
  :x,
  "x",
  type: :nominal,
  sort: "-y",
  title: "アカウント名"
)
|> Vl.encode_field(
  :y,
  "y",
  type: :quantitative,
  title: "記事数"
)

image.png

グラフにすると勢いの凄さが分かりますね

まとめ

パイプ |> で繋いで dbg するのが楽しすぎる

まだまだ記事は増やせそう

15
0
4

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
15
0