Created
May 10, 2023 20:15
-
-
Save jspeed-meyers/f4817da8a16c64bc77caa451546b1a6d to your computer and use it in GitHub Desktop.
A script to collect the names of the most downloaded python packages
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# script to retrieve most downloaded packages on the python package index | |
# also, oh yeah, chatgpt wrote some of this. I changed a little though. | |
# I'm not redundant yet. | |
import json | |
import urllib.request | |
def get_top_packages(top_n=1000): | |
"""Identify top packages by download count on pypi. | |
A friendly person has already provided an occasionally | |
updated JSON feed to enable this program to build a list | |
of the top pypi packages by download count. The default | |
does a fresh pull of this feed. | |
Args: | |
top_n (int): the number of top packages to retrieve | |
Returns: | |
dict: top packages | |
""" | |
top_packages_url = ( | |
"https://hugovk.github.io/top-pypi-packages/top-pypi-packages-30-days.json" | |
) | |
with urllib.request.urlopen(top_packages_url) as url: | |
data = json.loads(url.read().decode()) | |
# Place top_n packages in list | |
top_pypi_pkgs = [] | |
i = 0 | |
for pkg in data["rows"]: | |
if i == top_n: | |
break | |
top_pypi_pkgs.append(pkg["project"]) | |
i += 1 | |
return top_pypi_pkgs | |
def save_list_to_file(pkg_list): | |
"""Save package list to a file. | |
For each package, save to a .txt file with each | |
package on a new line. | |
Args: | |
pkg_list (list): a list of top packages | |
Returns: | |
Nothing | |
""" | |
with open('top-pypi-package-list.txt', 'w') as file: | |
for pkg in pkg_list: | |
file.write(str(pkg) + '\n') | |
if __name__ == "__main__": | |
pkgs = get_top_packages(1000) | |
save_list_to_file(pkgs) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment