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

wslでelixir その80

Last updated at Posted at 2023-10-09

概要

wsl(wsl2じゃない)で、elixirやってみた。
livebookでExplorerやってみた。

参考にしたページ

写真

image.png

サンプルコード

defmodule Qiita do
  @auth_header {"Authorization", "Bearer ***"}
  def get_articles(page) do
    "https://qiita.com/api/v2/authenticated_user/items?page=#{page}&per_page=100"
    |> Req.get!(headers: [@auth_header])
    |> Map.get(:body)
  end

  def get_articles_cyclic(page) do
    IO.inspect("get page #{page}")
    :timer.sleep(1000)
    articles = get_articles(page)

    case articles do
      [] ->
        IO.inspect("stop")
        articles

      _ ->
        articles ++ get_articles_cyclic(page + 1)
    end
  end

  def get_all_articles() do
    get_articles_cyclic(1)
  end
end

all_articles = Qiita.get_articles(1)
Enum.count(all_articles)

qiita_df =
  all_articles
  |> Enum.map(fn item ->
    %{
      "title" => item["title"],
      "private" => item["private"],
      "created_at" => NaiveDateTime.from_iso8601!(item["created_at"]),
      "page_views_count" => item["page_views_count"],
      "likes_count" => item["likes_count"],
      "likes_rate" => item["likes_count"] / item["page_views_count"],
      "stocks_count" => item["stocks_count"],
      "stocks_rate" => item["stocks_count"] / item["page_views_count"],
      "tags" => item["tags"] |> Enum.map(& &1["name"]) |> Enum.join(","),
      "length" => item["body"] |> String.length()
    }
  end)
  |> Explorer.DataFrame.new()
  |> Explorer.DataFrame.select([
    "title",
    "private",
    "created_at",
    "page_views_count",
    "likes_count",
    "likes_rate",
    "stocks_count",
    "stocks_rate",
    "tags"
    "length"
  ])

Kino.DataTable.new(qiita_df)

get_values = fn df, col ->
  df
  |> Explorer.DataFrame.pull(col)
  |> Explorer.Series.to_list()
end

x = get_values.(qiita_df, "title")
y = get_values.(qiita_df, "page_views_count")

VegaLite.new(width: 800, height: 400)
|> VegaLite.data_from_values(x: x, y: y)
|> VegaLite.mark(:bar)
|> VegaLite.encode_field(
  :x,
  "x",
  type: :nominal,
  title: "title",
  sort: %{"field" => "y", "order" => "descending"}
)
|> VegaLite.encode_field(
  :y,
  "y",
  type: :quantitative,
  title: "page_views_count"
)



以上。

1
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
1
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?