Skip to content

Instantly share code, notes, and snippets.

@slangley
Created December 7, 2024 03:27
Show Gist options
  • Save slangley/5907a3585b313398fcbbc94dd968f40b to your computer and use it in GitHub Desktop.
Save slangley/5907a3585b313398fcbbc94dd968f40b to your computer and use it in GitHub Desktop.
Script to Download iOS AppStore screenshots to an Index sheet.
# requests>=2.31.0
# beautifulsoup4>=4.12.0
# Pillow>=10.0.0
# urllib3<2.0.0
import requests
from bs4 import BeautifulSoup
from PIL import Image
import io
import re
def create_screenshot_index(app_store_url, output_file="screenshot_index.png"):
# Fetch the page
headers = {
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.124 Safari/537.36'
}
response = requests.get(app_store_url, headers=headers)
soup = BeautifulSoup(response.text, 'html.parser')
# Get app name from the page
app_name_element = soup.find('h1', class_='product-header__title')
if app_name_element:
app_name = app_name_element.text.strip()
# Sanitize the app name to create filename (keep only alphanumeric chars)
sanitized_name = ''.join(c for c in app_name if c.isalnum())
output_file = f"{sanitized_name}_screenshots.png"
print(f"App name: {app_name}")
# Debug: Print all div classes to see what we're working with
print("Available div classes:")
for div in soup.find_all('div'):
if div.get('class'):
print(div.get('class'))
# Find iPhone screenshots within the viewer__screenshots div
screenshot_urls = []
screenshots_div = soup.find('div', class_='we-screenshot-viewer__screenshots')
if screenshots_div:
print("\nFound screenshots div:")
print(screenshots_div)
picture_elements = screenshots_div.find_all('picture')
for picture in picture_elements:
# Get all source elements
sources = picture.find_all('source')
largest_url = None
largest_width = 0
# Check each source for the largest image
for source in sources:
srcset = source.get('srcset', '')
# Parse each source in srcset (format: "url 1x, url 2x, url 3x")
for src_item in srcset.split(','):
if not src_item.strip():
continue
parts = src_item.strip().split(' ')
if len(parts) == 2:
url = parts[0]
# Convert '2x' to 2, '3x' to 3, etc.
scale = float(parts[1].replace('x', '').replace('w', ''))
if scale > largest_width:
largest_width = scale
largest_url = url
# If we found a URL, add it to our list
if largest_url:
screenshot_urls.append(largest_url)
print(f"Found screenshot URL: {largest_url}")
if not screenshot_urls:
raise Exception("No iPhone screenshots found. Please check the URL or update the script.")
print(f"Found {len(screenshot_urls)} screenshots")
# Download and process images
screenshots = []
max_height = 0
total_width = 0
for url in screenshot_urls:
print(f"Downloading: {url}")
img_response = requests.get(url)
img = Image.open(io.BytesIO(img_response.content))
screenshots.append(img)
max_height = max(max_height, img.height)
total_width += img.width
# Create the combined image
combined_image = Image.new('RGB', (total_width, max_height), 'white')
current_x = 0
for img in screenshots:
# Calculate vertical position to center the image
y_position = (max_height - img.height) // 2
combined_image.paste(img, (current_x, y_position))
current_x += img.width
# Save the result
combined_image.save(output_file, 'PNG')
print(f"Created screenshot index at: {output_file}")
if __name__ == "__main__":
import sys
if len(sys.argv) != 2:
print("Usage: python app_store_screenshot_indexer.py <app_store_url>")
sys.exit(1)
app_store_url = sys.argv[1]
create_screenshot_index(app_store_url)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment