Skip to content

Instantly share code, notes, and snippets.

@greg76
Last active September 14, 2021 18:09
Show Gist options
  • Save greg76/fadc6f09c20a4b14fe11d59d46f1f7d5 to your computer and use it in GitHub Desktop.
Save greg76/fadc6f09c20a4b14fe11d59d46f1f7d5 to your computer and use it in GitHub Desktop.
fastest growing homebrew packages
import locale
import requests
import tabulate
locale.setlocale(locale.LC_ALL, "en_US")
# available snapshots: 30, 90, 365 days
data_ranges = {"new": 30, "old": 90}
def sum_formulae(l):
new = {
k: sum([locale.atoi(f["count"]) for f in v]) for k, v in l["formulae"].items()
}
return new
def upcoming(old, new):
deltas = {
k: v - int( (old[k] - v) * data_ranges["new"] / (data_ranges["old"] - data_ranges["new"]) )
for k, v in new.items()
if k in old
}
gainers = [
(k, new[k], old[k], v, 100 * v / new[k]) for k, v in deltas.items() if v > 2500
]
return list(sorted(gainers, key=lambda e: e[4], reverse=True))
def main():
# source = "https://formulae.brew.sh/api/analytics/cask-install/homebrew-cask/{}d.json"
source = "https://formulae.brew.sh/api/analytics/install-on-request/homebrew-core/{}d.json"
data = {}
for data_range in data_ranges.items():
url = source.format(data_range[1])
resp = requests.get(url)
data[data_range[0]] = sum_formulae(resp.json())
diff = set(data["new"]) - set(data["old"])
if len(diff) > 0:
header = ("New formulae", f"last {data_ranges['new']}d")
new_entries = filter(lambda x: x[0] in diff, data["new"].items())
print(tabulate.tabulate(new_entries, header))
else:
print("There were no new entries.")
print()
top20 = upcoming(data["old"], data["new"])[:20]
header = (
"Formula",
f"last {data_ranges['new']}d",
f"last {data_ranges['old']}d",
"total inc.",
"gain%",
)
print(tabulate.tabulate(top20, header, floatfmt=".1f"))
if __name__ == "__main__":
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment