Created
January 10, 2025 12:42
-
-
Save aarmn/e3bb5898ba622d0307bd8df977e92a1b to your computer and use it in GitHub Desktop.
Get all the versions of a python package with their respective release date, provided the package name.
This file contains hidden or 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
import requests | |
from packaging.version import parse | |
def get_package_versions_and_dates(package_name): | |
""" | |
Fetch all versions and release dates of a package from PyPI, sorted by version. | |
Args: | |
package_name (str): The name of the Python package. | |
Returns: | |
list: A list of tuples containing versions and release dates, sorted by version. | |
""" | |
url = f"https://pypi.org/pypi/{package_name}/json" | |
versions = [] | |
try: | |
response = requests.get(url) | |
response.raise_for_status() | |
data = response.json() | |
# Gather versions and release dates | |
for version, releases in data["releases"].items(): | |
if releases: # Ensure there are release entries | |
release_date = releases[0]["upload_time"] | |
versions.append((parse(version), version, release_date)) | |
# Sort by parsed version | |
versions.sort(key=lambda x: x[0]) | |
# Return version strings and dates | |
return [(v[1], v[2]) for v in versions] | |
except requests.exceptions.RequestException as e: | |
print(f"Error fetching package info: {e}") | |
except KeyError: | |
print("Package not found or does not have release data.") | |
return [] | |
if __name__ == "__main__": | |
package_name = input("Enter the package name: ").strip() | |
versions_and_dates = get_package_versions_and_dates(package_name) | |
if versions_and_dates: | |
print(f"All versions and release dates for '{package_name}':") | |
for version, release_date in versions_and_dates: | |
print(f"Version: {version}, Release Date: {release_date}") | |
else: | |
print("Could not fetch package information.") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment