This Python script enables users to compare multiple iOS apps by inputting their names, retrieving details like ratings, versions, and descriptions from the iTunes Search API. It displays a side-by-side overview of each app’s name, short description, rating count, rating stars, current version, and the date the check was initiated.
Click to view script…
import requests
from datetime import datetime
# Initialize a list to store app names
app_names = []
while True:
app_name = input("Enter app name: ")
app_names.append(app_name)
another = input("Do you want to add another app? (y/n): ").lower()
if another != 'y':
break
# Record the current date and time when the check is initiated
current_time = datetime.now().strftime("%Y-%m-%d %H:%M:%S")
# Initialize a list to store app data
apps_data = []
# Query the iTunes Search API for each app
for app_name in app_names:
try:
response = requests.get(
"https://itunes.apple.com/search",
params={"term": app_name, "country": "us", "media": "software"}
)
response.raise_for_status() # Raise an exception for HTTP errors
data = response.json()
if data["resultCount"] > 0:
# Take the first result as the most relevant app
app = data["results"][0]
# Create a short description from the first 20 words of the full description
words = app["description"].split()
short_description = " ".join(words[:20]) + "..." if len(words) > 20 else " ".join(words)
# Get the rating stars, formatting to one decimal place if available
rating_stars = app.get("averageUserRating", "N/A")
if rating_stars != "N/A":
rating_stars = f"{rating_stars:.1f}"
# Compile the app information
app_info = {
"app_name": app["trackName"],
"short_description": short_description,
"rating_count": app.get("userRatingCount", 0),
"rating_stars": rating_stars,
"current_version": app.get("version", "N/A"),
"check_date": current_time
}
else:
app_info = {
"app_name": app_name,
"error": "App not found"
}
except requests.exceptions.RequestException as e:
app_info = {
"app_name": app_name,
"error": f"Error fetching data: {e}"
}
apps_data.append(app_info)
# Display the information for all apps
for app_info in apps_data:
if "error" in app_info:
print(f"App Name: {app_info['app_name']}")
print(f"Error: {app_info['error']}")
else:
print(f"App Name: {app_info['app_name']}")
print(f"Short Description: {app_info['short_description']}")
print(f"Rating Count: {app_info['rating_count']}")
print(f"Rating Stars: {app_info['rating_stars']}")
print(f"Current Version: {app_info['current_version']}")
print(f"Check Date: {app_info['check_date']}")
print("-" * 40)