From cf8f76533d4a226348e3d9f95fc71580c46b571e Mon Sep 17 00:00:00 2001 From: Paul Gauthier Date: Thu, 20 Jul 2023 08:24:46 -0300 Subject: [PATCH] aider: Refactored versioncheck.py to use the PyPI JSON API instead of web scraping. # Aider chat conversation: USER: can we use a json api not scrape? ASSISTANT: Yes, we can use the PyPI JSON API to get the information about the latest version of the package. We will modify the `aider/versioncheck.py` file. Here are the steps: 1. Send a GET request to the PyPI JSON API for the `aider` package. 2. Parse the JSON response to find the latest version number. 3. Compare this version number with the current version of `aider` installed in the system. 4. Print a message indicating whether a newer version is available. Here is the code: ```python aider/versioncheck.py <<<<<<< ORIGINAL import requests from bs4 import BeautifulSoup import pkg_resources def check_version(): response = requests.get('https://pypi.org/project/aider/') soup = BeautifulSoup(response.text, 'html.parser') latest_version = soup.find('h1', {'class': 'package-header__name'}).text.split()[-1] current_version = pkg_resources.get_distribution('aider').version if pkg_resources.parse_version(latest_version) > pkg_resources.parse_version(current_version): print(f"A newer version of 'aider' is available: {latest_version}") else: print("You are using the latest version of 'aider'.") if __name__ == "__main__": check_version() ======= import requests import pkg_resources def check_version(): response = requests.get('https://pypi.org/pypi/aider/json') data = response.json() latest_version = data['info']['version'] current_version = pkg_resources.get_distribution('aider').version if pkg_resources.parse_version(latest_version) > pkg_resources.parse_version(current_version): print(f"A newer version of 'aider' is available: {latest_version}") else: print("You are using the latest version of 'aider'.") if __name__ == "__main__": check_version() >>>>>>> UPDATED ``` This version of the script uses the PyPI JSON API, which is more reliable and faster than web scraping. --- aider/versioncheck.py | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/aider/versioncheck.py b/aider/versioncheck.py index 96963581c..a09a2b536 100644 --- a/aider/versioncheck.py +++ b/aider/versioncheck.py @@ -1,11 +1,10 @@ import requests -from bs4 import BeautifulSoup import pkg_resources def check_version(): - response = requests.get('https://pypi.org/project/aider/') - soup = BeautifulSoup(response.text, 'html.parser') - latest_version = soup.find('h1', {'class': 'package-header__name'}).text.split()[-1] + response = requests.get('https://pypi.org/pypi/aider/json') + data = response.json() + latest_version = data['info']['version'] current_version = pkg_resources.get_distribution('aider').version if pkg_resources.parse_version(latest_version) > pkg_resources.parse_version(current_version):