Created
July 2, 2024 08:23
-
-
Save Isopach/9bb0ebd867c14b0c3a3825c588523fa5 to your computer and use it in GitHub Desktop.
Calculate how much money you've wasted on Google Play Games
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
""" | |
Usage: | |
1. Go to https://takeout.google.com/ | |
2. Export Google Play Store history. | |
3. Download the zip file. | |
4. Extract to a folder. | |
5. Link to the path "\Takeout\Google Play Store\Purchase History.json" on line 64. | |
6. Run the code with python3 purchase_history.py DEBUG or python3 purchase_history.py app_name | |
""" | |
import json | |
import re | |
import sys | |
def get_total_sum_for_app(purchase_history, app_name): | |
# Initialize total sums and variables to store the matched app name | |
total_sum_jpy = 0 | |
total_sum_sgd = 0 | |
total_sum_usd = 0 | |
matched_app_name = None | |
# Process each purchase | |
for purchase in purchase_history: | |
title = purchase['purchaseHistory']['doc']['title'] | |
if app_name.lower() in title.lower(): | |
# Extract the app name within the brackets | |
match = re.search(r'\((.*?)\)', title) | |
if match: | |
matched_app_name = match.group(1) | |
# Check if 'invoicePrice' key exists | |
if 'invoicePrice' in purchase['purchaseHistory']: | |
invoice_price = purchase['purchaseHistory']['invoicePrice'] | |
# Remove currency symbol and commas, convert to appropriate numeric type | |
if '¥' in invoice_price: | |
price_value = int(invoice_price.replace('¥', '').replace(',', '')) | |
total_sum_jpy += price_value | |
elif 'SGD' in invoice_price: | |
price_value = float(invoice_price.replace('SGD', '').replace(',', '')) | |
total_sum_sgd += price_value | |
elif 'USD' in invoice_price: | |
price_value = float(invoice_price.replace('USD', '').replace(',', '')) | |
total_sum_usd += price_value | |
# Return the total sums and the matched app name | |
return total_sum_jpy, total_sum_sgd, total_sum_usd, matched_app_name | |
def get_unique_app_names(purchase_history): | |
# Initialize a set to store unique app names | |
unique_app_names = set() | |
# Process each purchase | |
for purchase in purchase_history: | |
title = purchase['purchaseHistory']['doc']['title'] | |
# Extract the app name within the brackets | |
match = re.search(r'\((.*?)\)', title) | |
if match: | |
unique_app_names.add(match.group(1)) | |
# Convert the set to a list and return the unique app names | |
return list(unique_app_names) | |
if __name__ == "__main__": | |
app_name = sys.argv[1] | |
with open("Purchase History.json", encoding='utf-8') as f: | |
purchase_history = json.loads(f.read()) | |
# Get the unique app names | |
if app_name.lower() == "debug": | |
unique_app_names = get_unique_app_names(purchase_history) | |
print(*unique_app_names, sep='\n') | |
else: | |
# Get the total sums for the given app name | |
total_sum_jpy, total_sum_sgd, total_sum_usd, matched_app_name = get_total_sum_for_app(purchase_history, app_name) | |
# Print the total sums with the matched app name | |
if matched_app_name: | |
print(f"Total sum of purchases for '{matched_app_name}':") | |
if total_sum_jpy > 0: | |
print(f"JPY: ¥{total_sum_jpy}") | |
if total_sum_sgd > 0: | |
print(f"SGD: S${total_sum_sgd}") | |
if total_sum_usd > 0: | |
print(f"USD: US${total_sum_usd}") | |
else: | |
print("No purchases found for the specified app name.") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment