Last active
November 27, 2024 14:51
-
-
Save emndeniz/1000e35e41770817fd0bfca9e66a916b to your computer and use it in GitHub Desktop.
DownloadAppStoreReviews.py
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 | |
import json | |
import csv | |
def get_reviews(app_id, jwt_token, fetch_all): | |
url = f"https://api.appstoreconnect.apple.com/v1/apps/{app_id}/customerReviews?limit=200&sort=-createdDate" | |
headers = { | |
"Authorization": f"Bearer {jwt_token}" | |
} | |
all_reviews = [] | |
total_reviews = None | |
fetched_reviews = 0 | |
while url: | |
response = requests.get(url, headers=headers) | |
if response.status_code != 200: | |
print(f"Failed to fetch data: {response.status_code}") | |
print(response.text) | |
return [] | |
data = response.json() | |
# Get the total number of reviews from the meta field if not already obtained | |
if total_reviews is None: | |
total_reviews = data.get('meta', {}).get('paging', {}).get('total', None) | |
all_reviews.extend(data['data']) | |
fetched_reviews += len(data['data']) | |
# Show progress | |
if total_reviews: | |
print(f"Fetched {fetched_reviews} of {total_reviews} reviews ({(fetched_reviews / total_reviews) * 100:.2f}%)") | |
if not fetch_all: | |
break | |
# Check if there's a next page | |
url = data.get('links', {}).get('next', None) | |
return all_reviews | |
def save_to_json(reviews): | |
with open('appstore_reviews.json', 'w') as f: | |
json.dump(reviews, f, indent=4) | |
print("Reviews have been saved to appstore_reviews.json") | |
def save_to_csv(reviews): | |
with open('appstore_reviews.csv', 'w', newline='', encoding='utf-8') as f: | |
writer = csv.writer(f) | |
# Write the header | |
writer.writerow(['id', 'rating', 'title', 'body', 'reviewerNickname', 'createdDate', 'territory']) | |
# Write the data | |
for review in reviews: | |
attributes = review['attributes'] | |
writer.writerow([ | |
review['id'], | |
attributes.get('rating'), | |
attributes.get('title'), | |
attributes.get('body'), | |
attributes.get('reviewerNickname'), | |
attributes.get('createdDate'), | |
attributes.get('territory') | |
]) | |
print("Reviews have been saved to appstore_reviews.csv") | |
def main(): | |
app_id = input("Enter the AppID: ") | |
jwt_token = input("Enter your JWT token: ") | |
fetch_all = input("Do you want to download all data or just the first page? (all/first): ").strip().lower() == 'all' | |
output_format = input("Enter the output format (json/csv/both): ").strip().lower() | |
reviews = get_reviews(app_id, jwt_token, fetch_all) | |
if reviews: | |
if output_format in ('json', 'both'): | |
save_to_json(reviews) | |
if output_format in ('csv', 'both'): | |
save_to_csv(reviews) | |
else: | |
print("No reviews fetched.") | |
if __name__ == "__main__": | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment