Created
November 21, 2013 17:02
-
-
Save grapefrukt/7585532 to your computer and use it in GitHub Desktop.
This script takes a Google Play sales report CSV as input and outputs a plain text summary in the same folder. No guarantees for accuracy or validity.
This file contains 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
''' | |
Copyright (c) 2013 Martin Jonasson, grapefrukt games | |
Permission is hereby granted, free of charge, to any person obtaining a copy | |
of this software and associated documentation files (the "Software"), to deal | |
in the Software without restriction, including without limitation the rights | |
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | |
copies of the Software, and to permit persons to whom the Software is | |
furnished to do so, subject to the following conditions: | |
The above copyright notice and this permission notice shall be included in | |
all copies or substantial portions of the Software. | |
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | |
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | |
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | |
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | |
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | |
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN | |
THE SOFTWARE. | |
''' | |
from __future__ import print_function | |
import csv | |
from decimal import Decimal | |
import locale | |
import os | |
import sys | |
import platform | |
onWindows = platform.system() == 'Windows' | |
if not len(sys.argv) == 2 : | |
raise Exception('This script expects exactly one argument: A path to a Google Play CSV file') | |
filename = sys.argv[1] | |
out_filename, ext = os.path.splitext(filename) | |
# redirect output to textfile | |
f = open(out_filename +'.txt', 'w'); | |
sys.stdout = f | |
if not ext == '.csv' : | |
raise Exception('Input has to be .csv') | |
# Locale settings, tweak as needed | |
# annoyingly, windows has different locale strings | |
if onWindows : | |
# http://msdn.microsoft.com/en-us/library/39cwe7zf(vs.71).aspx | |
locale.setlocale(locale.LC_ALL, 'swedish') | |
def printf(str, *args): | |
print(str % args, end='\n') | |
else : | |
locale.setlocale(locale.LC_ALL, 'sv_SE') | |
def printf(str, *args): | |
print(str % args, end='\r\n') | |
input_file = csv.DictReader(open(filename)) | |
transactions = {}; | |
for row in input_file: | |
key = row['Description'] | |
if key not in transactions: | |
transactions[key] = {'Charge' : Decimal(0), 'Google fee' : Decimal(0), 'Tax' : Decimal(0), 'Charge refund' : Decimal(0), 'Google fee refund' : Decimal(0)} | |
transactions[key][row['Transaction Type']] += Decimal(row['Amount (Merchant Currency)']) | |
count_taxed_sales = 0 | |
count_untaxed_sales = 0 | |
count_refunds = 0 | |
sum_taxed_sales = Decimal(0) | |
sum_untaxed_sales = Decimal(0) | |
sum_tax = Decimal(0) | |
sum_fees = Decimal(0) | |
sum_fee_refund = Decimal(0) | |
sum_sales_refund = Decimal(0) | |
for key, row in transactions.iteritems() : | |
if row['Tax'] > 0 : | |
sum_taxed_sales += row['Charge'] | |
sum_tax += row['Tax'] | |
count_taxed_sales += 1 | |
else : | |
sum_untaxed_sales += row['Charge'] | |
count_untaxed_sales += 1 | |
sum_fees += row['Google fee'] | |
sum_fee_refund += row['Google fee refund'] | |
sum_sales_refund += row['Charge refund'] | |
if row['Google fee refund'] > 0 : | |
count_refunds += 1 | |
printf("Sales report for Google Play Apps " + out_filename[-6:-2] + '-' + out_filename[-2:]) | |
printf("") | |
printf("Taxed sales: " + str(locale.currency(sum_taxed_sales, grouping=True)).rjust(13) + str('({0} units)'.format(locale.format('%d', count_taxed_sales, grouping=True))).rjust(16)) | |
printf("Tax: " + str(locale.currency(sum_tax, grouping=True)).rjust(13)) | |
printf("Untaxed sales: " + str(locale.currency(sum_untaxed_sales, grouping=True)).rjust(13) + str('({0} units)'.format(locale.format('%d', count_untaxed_sales, grouping=True))).rjust(16)) | |
printf("Google fees: " + str(locale.currency(sum_fees, grouping=True)).rjust(13)) | |
printf("Refunds: " + str(locale.currency(sum_sales_refund, grouping=True)).rjust(13) + str('({0} units)'.format(locale.format('%d', count_refunds, grouping=True))).rjust(16)) | |
printf("Google fee refunds: " + str(locale.currency(sum_fee_refund, grouping=True)).rjust(13)) | |
printf("") | |
printf("Sum: " + str(locale.currency(sum_taxed_sales + sum_untaxed_sales + sum_tax + sum_fees + sum_sales_refund + sum_fee_refund, grouping=True)).rjust(13) + str('({0} units)'.format(locale.format('%d', count_taxed_sales + count_untaxed_sales - count_refunds, grouping=True)).rjust(16))) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment