Skip to content

Instantly share code, notes, and snippets.

@philipperemy
Created November 6, 2023 03:26
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save philipperemy/54352ab44b769c71e610c5d6d2005ef3 to your computer and use it in GitHub Desktop.
Save philipperemy/54352ab44b769c71e610c5d6d2005ef3 to your computer and use it in GitHub Desktop.
Count the total downloads of your packages on PyPi
import requests
from bs4 import BeautifulSoup
def get_downloads_count(package_name: str):
badge_url = f"https://static.pepy.tech/badge/{package_name}"
response = requests.get(badge_url)
download_count = int(
BeautifulSoup(response.text, 'html.parser')
.find_all('text')[-1].text.replace('k', '000').replace('m', '000000')
)
return download_count
def get_packages(pypi_username: str):
package_names = []
# URL to PyPI user's packages page
user_packages_url = f"https://pypi.org/user/{pypi_username}/"
# Make an HTTP GET request to the user's packages page
response = requests.get(user_packages_url)
if response.status_code == 200:
# Parse the HTML of the user's packages page
soup = BeautifulSoup(response.text, 'html.parser')
# Find the list of package links
package_links = soup.find_all('a', class_='package-snippet')
if not package_links:
print(f"No packages found for user {pypi_username}.")
else:
print(f"Packages uploaded by user {pypi_username} on PyPI:")
for link in package_links:
package_name = link.find('h3').text
package_names.append(package_name)
else:
print(f"Failed to retrieve packages. Status code: {response.status_code}")
return package_names
def main():
total_downloads = 0
package_names = get_packages('philipperemy')
for package_name in package_names:
downloads = get_downloads_count(package_name)
print(package_name, downloads)
total_downloads += downloads
print('TOTAL', total_downloads)
if __name__ == '__main__':
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment