Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
64 changes: 64 additions & 0 deletions lib/mix/tasks/popular_packages.ex
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could just as well be another subcommand delegated from the main entry point.

Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
defmodule Mix.Tasks.PopularPackages do
use Mix.Task

@shortdoc "Rebuild popular_packages.txt"

@moduledoc """
Fetches a list of most downloaded packages from hex.pm and stores into the
top-level popular_packages.txt file.

## Usage

mix popular_packages
"""

@base_url "https://hex.pm/api/packages"
@file_path File.cwd!() <> "/popular_packages.txt"
@page_count 11
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I chose 1,100 packages (@page_count of 11) quite arbitrarily. Seems a reasonable default.

I'll happily merge this PR with a hard-coded value of 11 here, but it would be even better if your Mix task let the caller specify a different value for their personal use. Just an idea. Feel free to ignore.


def run([]) do
Mix.shell().info("Fetching #{@page_count} pages of packages...")

names = build_popular_list()
count = length(names)

header = """
# The #{count} most popular packages on hex.pm, according to #{@base_url} on #{Date.utc_today() |> Date.to_string()}
# Generated using:
# mix popular_packages
"""

File.write!(@file_path, header <> Enum.join(names, "\n"))
Mix.shell().info("Wrote #{count} package names to #{@file_path}")
end

defp build_popular_list() do
1..@page_count
|> Enum.flat_map(&fetch_page/1)
|> Enum.map(& &1["name"])
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I love this task, but should we sort this list before writing it to a file so we don't modify the file just to re-order package names? I'm seeing many pointless "changes." For example:

-credo
 bunt
+credo
 recon
 websock
 telemetry_poller
@@ -46,16 +46,16 @@ finch
 phoenix_ecto
 expo
 jose
-nimble_parsec
 phoenix_live_view
+nimble_parsec
 httpoison
 verl
 tzdata
 tesla
 combine
 hex_core
-rebar3_hex
 phoenix_view
+rebar3_hex
 phoenix_live_dashboard
 dialyxir
 timex
@@ -69,9 +69,9 @@ earmark_parser
 makeup_elixir
 makeup
 joken
-ex_doc
-ex_aws
 gen_stage
+ex_aws
+ex_doc

end

defp fetch_page(page),
do: api_get(@base_url <> "?sort=recent_downloads&page=#{page}")

defp user_agent do
mix_config = Mix.Project.config()
to_string(mix_config[:app]) <> " " <> mix_config[:version]
end

defp api_get(url) do
{:ok, {{_, 200, _}, _, body}} =
:httpc.request(
:get,
{to_charlist(url),
[
{~c"user-agent", to_charlist(user_agent())},
{~c"accept", ~c"application/json"}
]},
[],
[]
)

Jason.decode!(body)
end
end
3 changes: 2 additions & 1 deletion mix.exs
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,13 @@ defmodule LocalHexdocs.MixProject do

def application do
[
extra_applications: [:logger]
extra_applications: [:logger, :inets, :ssl]
]
end

defp deps do
[
{:jason, "~> 1.0"},
{:recursive_selective_match, only: :test}
]
end
Expand Down
1 change: 1 addition & 0 deletions mix.lock
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
%{
"atomic_map": {:hex, :atomic_map, "0.9.3", "3c7f1302e0590164732d08ca999708efbb2cd768abf2911cf140280ce2dc499d", [:mix], [], "hexpm", "c237babf301bd2435bd85b96cffc973022b4cbb7721537059ee0dd3bb74938d2"},
"jason": {:hex, :jason, "1.4.4", "b9226785a9aa77b6857ca22832cffa5d5011a667207eb2a0ad56adb5db443b8a", [:mix], [{:decimal, "~> 1.0 or ~> 2.0", [hex: :decimal, repo: "hexpm", optional: true]}], "hexpm", "c5eb0cab91f094599f94d55bc63409236a8ec69a21a67814529e8d5f6cc90b3b"},
"recursive_selective_match": {:hex, :recursive_selective_match, "0.2.7", "c211d33ef1909e37540b4640cfbdf43803186c30a36d08e68126dc32196446f2", [:mix], [{:atomic_map, "~> 0.8", [hex: :atomic_map, repo: "hexpm", optional: false]}, {:utc_datetime, "~> 1.0", [hex: :utc_datetime, repo: "hexpm", optional: false]}], "hexpm", "3a38647b0366a0a2a9ed7631b197aae9ee6c9e2293c758cf6484e2bbf20f11d7"},
"utc_datetime": {:hex, :utc_datetime, "1.0.0", "617b7bac9de12d7afd99dfbbbc3cea7534ac044996bc40ab35f5bf78ca35dc6a", [:mix], [{:ecto, ">= 3.0.0", [hex: :ecto, repo: "hexpm", optional: true]}, {:jason, ">= 1.0.0", [hex: :jason, repo: "hexpm", optional: true]}], "hexpm", "c6c7db85badc8e6133b08107c09fcee1bd7290ee868ae17924c827b9704d718e"},
}
Loading