-
Notifications
You must be signed in to change notification settings - Fork 2
Mix task to refresh popular_packages.txt #4
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| 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 | ||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I chose 1,100 packages ( 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"]) | ||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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: |
||
| 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 | ||
| 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"}, | ||
| } |
There was a problem hiding this comment.
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.