Last active
February 12, 2024 04:35
-
-
Save opus-x/bd125599350b5625c17bcad539ec54c1 to your computer and use it in GitHub Desktop.
Some Python 3 scripts that might come in handy
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
#!/usr/bin/env python3 | |
# Copyright (c) 2023 Opus-X | |
# URL: https://gist.github.com/opus-x/bd125599350b5625c17bcad539ec54c1#file-pymrc-py | |
import sys | |
import requests | |
from bs4 import BeautifulSoup | |
import re | |
import csv | |
def get_latest_release_date(module_name): | |
""" | |
Get the latest release date for a Python module on PyPI. | |
Args: | |
module_name (str): The name of the Python module. | |
Returns: | |
tuple: A tuple containing the latest version and release date, or an error message. | |
""" | |
url = f"https://pypi.org/pypi/{module_name}/json" | |
try: | |
response = requests.get(url) | |
response.raise_for_status() | |
data = response.json() | |
releases = data["releases"] | |
release_dates = [] | |
for version, release_info in releases.items(): | |
if release_info: | |
release_date = release_info[0]["upload_time"] | |
release_dates.append((version, release_date)) | |
if release_dates: | |
# Sort release dates by date in descending order | |
release_dates.sort(key=lambda x: x[1], reverse=True) | |
latest_version, latest_release_date = release_dates[0] | |
return latest_version, latest_release_date | |
else: | |
return None, f"Release date not found for {module_name}" | |
except requests.exceptions.RequestException as e: | |
return None, f"Error fetching data for {module_name}: {e}" | |
def get_github_repo_url(module_name): | |
""" | |
Get the GitHub repository URL from the PyPI page of a Python module. | |
Args: | |
module_name (str): The name of the Python module. | |
Returns: | |
str: The GitHub repository URL, or None if not found. | |
""" | |
url = f"https://pypi.org/project/{module_name}/" | |
try: | |
response = requests.get(url) | |
response.raise_for_status() | |
soup = BeautifulSoup(response.text, "html.parser") | |
home_page_tag = soup.find("a", href=re.compile(r"https://github.com/.*")) | |
if home_page_tag: | |
return home_page_tag.get("href") | |
return None | |
except requests.exceptions.RequestException as e: | |
return None | |
if __name__ == "__main__": | |
if len(sys.argv) < 2: | |
print("Usage: python script.py module1 module2 module3") | |
sys.exit(1) | |
module_set = set(sys.argv[1:]) # Create a set from command-line arguments | |
module_data = [] | |
for module_name in module_set: | |
print(f"Checking release date for module '{module_name}'...", flush=True, end="\t\t\t\t\r") | |
latest_version, latest_release_date = get_latest_release_date(module_name) | |
github_url = get_github_repo_url(module_name) | |
module_data.append((latest_release_date, module_name, github_url)) | |
# Sort the list of module data by release date in descending order | |
sorted_module_data = sorted(module_data, key=lambda x: x[0], reverse=True) | |
# Write the data to a CSV file | |
with open("module_info.csv", mode="w", newline="", encoding="utf-8") as csv_file: | |
csv_writer = csv.writer(csv_file) | |
csv_writer.writerow(["Date", "Module Name", "GitHub Homepage"]) | |
for data_row in sorted_module_data: | |
csv_writer.writerow(data_row) | |
print("CSV file 'module_info.csv' created successfully.\t\t\t\t") |
This Python program has the ability to check and record information about the latest release date of Python modules on the PyPI site along with the GitHub URL address of each module. It uses libraries like requests, BeautifulSoup, and csv to perform this task and is capable of handling potential errors. The program can help track and manage Python modules in development projects.
Thanks a lot! I can use it for apk that I have stored for a long time.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
release_date
,module_name
andhome_page
to a csv-file