text
stringlengths
1
93.6k
def output_result(result, unzip = True):
"""Output (and when necessary unzip) the result of the request to the screen or into a report file"""
content, header = result
# unpack content into the final report file if it is gzip compressed.
if header.get_content_type() == 'application/a-gzip':
msg = header.get('downloadmsg')
filename = header.get('filename') or 'report.txt.gz'
if unzip:
msg = msg.replace('.txt.gz', '.txt')
filename = filename[:-3]
content = gzip.GzipFile(fileobj=io.BytesIO(content)).read()
file = open(filename, 'wb')
file.write(content)
file.close()
print(msg)
else:
print(content.decode())
# command line arguments
def parse_arguments():
"""Build and parse the command line arguments"""
parser_main = argparse.ArgumentParser(description="Reporting tool for querying Sales- and Financial Reports from App Store Connect", epilog="For a detailed description of report types, see https://help.apple.com/itc/appssalesandtrends/#/itc37a18bcbf")
# (most of the time) optional arguments
parser_main.add_argument('-a', '--account', type=int, help="account number (needed if your Apple ID has access to multiple accounts; for a list of your account numbers, use the 'getAccounts' command)")
parser_main.add_argument('-m', '--mode', choices=['Normal', 'Robot.XML'], default='Normal', help="output format: plain text or XML (defaults to '%(default)s')")
# always required arguments
required_args = parser_main.add_argument_group("required arguments")
required_args.add_argument('-u', '--userid', required=True, help="Apple ID for use with App Store Connect")
# template for commands that require authentication with password
parser_auth_password = argparse.ArgumentParser(add_help=False)
parser_auth_password.set_defaults(access_token=None, access_token_keychain_item=None)
auth_password_args = parser_auth_password.add_argument_group()
mutex_group = auth_password_args.add_mutually_exclusive_group(required=True)
mutex_group.add_argument('-p', '--password-keychain-item', metavar="KEYCHAIN_ITEM", help='name of the macOS Keychain item that holds the (optionally app-specific) password for the Apple ID (cannot be used together with -P)')
mutex_group.add_argument('-P', '--password', help='(optionally app-specific) password for the Apple ID (cannot be used together with -p)')
# template for commands that require authentication with access token
parser_auth_token = argparse.ArgumentParser(add_help=False)
parser_auth_token.set_defaults(password=None, password_keychain_item=None)
auth_token_args = parser_auth_token.add_argument_group()
mutex_group = auth_token_args.add_mutually_exclusive_group(required=True)
mutex_group.add_argument('-t', '--access-token-keychain-item', metavar="KEYCHAIN_ITEM", help='name of the macOS Keychain item that holds the App Store Connect access token (more secure alternative to -T)')
mutex_group.add_argument('-T', '--access-token', help='App Store Connect access token (can be obtained with the generateToken command or via App Store Connect -> Sales & Trends -> Saved -> Sales & Trends - Reports -> About Reports)')
# commands
subparsers = parser_main.add_subparsers(dest='command', title='commands', description="Specify the task you want to be carried out (use -h after a command's name to get additional help for that command)")
parser_cmd = subparsers.add_parser('getStatus', help="check if App Store Connect is available for queries", parents=[parser_auth_token])
parser_cmd.add_argument('service', choices=['Sales', 'Finance'], help="service endpoint to query")
parser_cmd.set_defaults(func=itc_get_status)
parser_cmd = subparsers.add_parser('getAccounts', help="fetch a list of accounts accessible to the Apple ID given in -u", parents=[parser_auth_token])
parser_cmd.add_argument('service', choices=['Sales', 'Finance'], help="service endpoint to query")
parser_cmd.set_defaults(func=itc_get_accounts)
parser_cmd = subparsers.add_parser('getVendors', help="fetch a list of vendors accessible to the Apple ID given in -u", parents=[parser_auth_token])
parser_cmd.set_defaults(func=itc_get_vendors)
parser_cmd = subparsers.add_parser('getVendorsAndRegions', help="fetch a list of financial reports you can download by vendor number and region", parents=[parser_auth_token])
parser_cmd.set_defaults(func=itc_get_vendor_and_regions)
parser_cmd = subparsers.add_parser('getReportVersion', help="query what is the latest available version of reports of a specific type and subtype", parents=[parser_auth_token])
parser_cmd.add_argument('reporttype', choices=['Sales', 'Subscription', 'SubscriptionEvent', 'Subscriber', 'Newsstand', 'Pre-Order'])
parser_cmd.add_argument('reportsubtype', choices=['Summary', 'Detailed', 'Opt-In'])
parser_cmd.set_defaults(func=itc_get_report_version)
parser_cmd = subparsers.add_parser('getFinancialReport', help="download a financial report file for a specific region and fiscal period", parents=[parser_auth_token])
parser_cmd.add_argument('vendor', type=int, help="vendor number of the report to download (for a list of your vendor numbers, use the 'getVendors' command)")
parser_cmd.add_argument('regioncode', help="two-character code of country of the report to download (for a list of country codes by vendor number, use the 'getVendorsAndRegions' command)")
parser_cmd.add_argument('fiscalyear', help="four-digit year of the report to download (year is specific to Apple’s fiscal calendar)")
parser_cmd.add_argument('fiscalperiod', help="period in fiscal year for the report to download (1-12; period is specific to Apple’s fiscal calendar)")
parser_cmd.set_defaults(func=itc_get_financial_report)
parser_cmd = subparsers.add_parser('getSalesReport', help="download a summary sales report file for a specific date range", parents=[parser_auth_token])
parser_cmd.add_argument('vendor', type=int, help="vendor number of the report to download (for a list of your vendor numbers, use the 'getVendors' command)")
parser_cmd.add_argument('datetype', choices=['Daily', 'Weekly', 'Monthly', 'Yearly'], help="length of time covered by the report")
parser_cmd.add_argument('date', help="specific time covered by the report (weekly reports use YYYYMMDD, where the day used is the Sunday that week ends; monthly reports use YYYYMM; yearly reports use YYYY)")
parser_cmd.set_defaults(func=itc_get_sales_report)
parser_cmd = subparsers.add_parser('getSubscriptionReport', help="download a subscription report file for a specific day", parents=[parser_auth_token])
parser_cmd.add_argument('vendor', type=int, help="vendor number of the report to download (for a list of your vendor numbers, use the 'getVendors' command)")
parser_cmd.add_argument('date', help="specific day covered by the report (use YYYYMMDD format)")
parser_cmd.add_argument('-v', '--version', choices=['1_0', '1_1', '1_2', '1_3'], default='1_3', help="report format version to use (if omitted, the latest available version is used)")
parser_cmd.set_defaults(func=itc_get_subscription_report)
parser_cmd = subparsers.add_parser('getSubscriptionEventReport', help="download an aggregated subscriber activity report file for a specific day", parents=[parser_auth_token])
parser_cmd.add_argument('vendor', type=int, help="vendor number of the report to download (for a list of your vendor numbers, use the 'getVendors' command)")
parser_cmd.add_argument('date', help="specific day covered by the report (use YYYYMMDD format)")
parser_cmd.add_argument('-v', '--version', choices=['1_0', '1_1', '1_2', '1_3'], default='1_3', help="report format version to use (if omitted, the latest available version is used)")
parser_cmd.set_defaults(func=itc_get_subscription_event_report)
parser_cmd = subparsers.add_parser('getSubscriberReport', help="download a transaction-level subscriber activity report file for a specific day", parents=[parser_auth_token])