def create_report_removed_from_store(extensions_removed_from_chrome_store, filename, file_id, sheet_id):
    extensions = ['Name,ID,Version,Chrome Link,Devices,Number of Devices,Users,Number of Users'.split(',')]
    for id in extensions_removed_from_chrome_store:
        for version in extensions_removed_from_chrome_store[id]:
            if version == 'id':
                continue

            sheet_row = [
                extensions_removed_from_chrome_store[id]['name'],
                id,
                version,
                "https://crxcavator.io/report/{0}/{1}".format(id, version),
                ';'.join(extensions_removed_from_chrome_store[id][version]['devices']),
                str(len(extensions_removed_from_chrome_store[id][version]['devices'])),
                ';'.join(extensions_removed_from_chrome_store[id][version]['users']),
                str(len(extensions_removed_from_chrome_store[id][version]['users']))
            ]

            char_length = 0
            if len(';'.join(extensions_removed_from_chrome_store[id][version]['users'])) > 49000:
                char_length = len(';'.join(extensions_removed_from_chrome_store[id][version]['users']))

            if len(';'.join(extensions_removed_from_chrome_store[id][version]['devices'])) > 49000 and len(';'.join(extensions_removed_from_chrome_store[id][version]['devices'])) > char_length:
                char_length = len(';'.join(extensions_removed_from_chrome_store[id][version]['devices']))

            users_list = []
            device_list = []

            if len(';'.join(extensions_removed_from_chrome_store[id][version]['users'])) > 49000:
                number_in_each_user_block_list = ceil(len(extensions_removed_from_chrome_store[id][version]['users']) / ceil(char_length / 45000))
                users_list = [extensions_removed_from_chrome_store[id][version]['users'][i:i + number_in_each_user_block_list] for i in range(0, len(extensions_removed_from_chrome_store[id][version]['users']), number_in_each_user_block_list)]

            if len(';'.join(extensions_removed_from_chrome_store[id][version]['devices'])) > 49000:
                number_in_each_device_block_list = ceil(len(extensions_removed_from_chrome_store[id][version]['devices']) / ceil(char_length / 45000))
                device_list = [extensions_removed_from_chrome_store[id][version]['devices'][i:i + number_in_each_device_block_list] for i in range(0, len(extensions_removed_from_chrome_store[id][version]['devices']), number_in_each_device_block_list)]

            if users_list or device_list:
                if users_list:
                    for index, users in enumerate(users_list):
                        sheet_row[6] = ';'.join(users)
                        sheet_row[7] = str(len(users))
                        if device_list:
                            try:
                                sheet_row[4] = ';'.join(device_list[index])
                                sheet_row[5] = str(len(device_list[index]))
                            except IndexError:
                                sheet_row[4] = ''
                                sheet_row[5] = ''
                        extensions.append(sheet_row)
                elif device_list:
                    for index, devices in enumerate(device_list):
                        sheet_row[4] = ';'.join(devices)
                        sheet_row[5] = str(len(devices))
                        extensions.append(sheet_row)
            else:
                extensions.append(sheet_row)

    logger.info('Updating spreadsheet %s in %s drive with data for Chrome Extensions Removed from Store' % (filename, team_drive))
    sheet.append_sheet(filename, extensions, gsuite_service_account, file_id, sheet_id)
def create_report_unscanned_extensions(extension_ids, extension_names, filename, file_id, sheet_id):
    extensions = ['Name,ID'.split(',')]
    for extension_id in extension_ids:
        extensions.append(
            [
                extension_names[extension_id],
                extension_id
            ]
        )
    logger.info('Updating spreadsheet %s in %s drive with data for Unscanned in Crxcavator' % (filename, team_drive))
    sheet.append_sheet(filename, extensions, gsuite_service_account, file_id, sheet_id)
def create_report_risky_extensions(risky_extensions, filename, file_id, sheet_id):
    extensions = ['Name,ID,Version,CrxCavatorv Link,Chrome Link,Devices,Number of Devices,Users,Number of Users,Score,CSP,External JS,External Call,Dangerous Functions,Entry Points'.split(',')]
    for id in risky_extensions:
        for version in risky_extensions[id]:
            if risky_extensions[id][version].users:
                dangerous_funcs = {}
                if risky_extensions[id][version].dangerous_functions:
                    for key in risky_extensions[id][version].dangerous_functions:
                        dangerous_funcs[key] = list(risky_extensions[id][version].dangerous_functions[key].keys())

                sheet_row = [
                    risky_extensions[id][version].name,
                    id,
                    version,
                    risky_extensions[id][version].crxcavator_link,
                    risky_extensions[id][version].chrome_link,
                    ';'.join(risky_extensions[id][version].devices),
                    str(len(risky_extensions[id][version].devices)),
                    ';'.join(risky_extensions[id][version].users),
                    str(len(risky_extensions[id][version].users)),
                    str(risky_extensions[id][version].risk_score),
                    json.dumps(risky_extensions[id][version].risk_csp, indent=2),
                    json.dumps(risky_extensions[id][version].risk_external_javascript, indent=2),
                    json.dumps(risky_extensions[id][version].risk_external_calls, indent=2),
                    json.dumps(dangerous_funcs, indent=2),
                    json.dumps(risky_extensions[id][version].entry_points, indent=2)
                ]

                char_length = 0
                if len(';'.join(risky_extensions[id][version].users)) > 49000:
                    char_length = len(';'.join(risky_extensions[id][version].users))

                if len(';'.join(risky_extensions[id][version].devices)) > 49000 and len(';'.join(risky_extensions[id][version].devices)) > char_length:
                    char_length = len(';'.join(risky_extensions[id][version].devices))

                if len(json.dumps(risky_extensions[id][version].risk_external_calls, indent=2)) > 49000 and len(json.dumps(risky_extensions[id][version].risk_external_calls, indent=2)) > char_length:
                    char_length = len(json.dumps(risky_extensions[id][version].risk_external_calls, indent=2))

                users_list = []
                device_list = []
                _list = []

                if len(json.dumps(risky_extensions[id][version].risk_external_calls, indent=2)) > 49000:
                    number_in_each_block_list = ceil(len(risky_extensions[id][version].risk_external_calls) / ceil(char_length / 45000))
                    _list = [risky_extensions[id][version].risk_external_calls[i:i + number_in_each_block_list] for i in range(0, len(risky_extensions[id][version].risk_external_calls), number_in_each_block_list)]

                if len(';'.join(risky_extensions[id][version].users)) > 49000:
                    number_in_each_user_block_list = ceil(len(risky_extensions[id][version].users) / ceil(char_length / 45000))
                    users_list = [risky_extensions[id][version].users[i:i + number_in_each_user_block_list] for i in range(0, len(risky_extensions[id][version].users), number_in_each_user_block_list)]

                if len(';'.join(risky_extensions[id][version].devices)) > 49000:
                    number_in_each_device_block_list = ceil(len(risky_extensions[id][version].devices) / ceil(char_length / 45000))
                    device_list = [risky_extensions[id][version].devices[i:i + number_in_each_device_block_list] for i in range(0, len(risky_extensions[id][version].devices), number_in_each_device_block_list)]

                if users_list or device_list or _list:
                    if users_list:
                        for index, users in enumerate(users_list):
                            sheet_row[7] = ';'.join(users)
                            sheet_row[8] = str(len(users))
                            if device_list:
                                try:
                                    sheet_row[5] = ';'.join(device_list[index])
                                    sheet_row[6] = str(len(device_list[index]))
                                except IndexError:
                                    sheet_row[5] = ''
                                    sheet_row[6] = ''
                            if _list:
                                try:
                                    sheet_row[12] = json.dumps(_list[index], indent=2)
                                except IndexError:
                                    sheet_row[12] = ''
                            extensions.append(sheet_row)
                    elif device_list:
                        for index, devices in enumerate(device_list):
                            sheet_row[5] = ';'.join(devices)
                            sheet_row[6] = str(len(devices))
                            if _list:
                                try:
                                    sheet_row[12] = json.dumps(_list[index], indent=2)
                                except IndexError:
                                    sheet_row[12] = ''
                            extensions.append(sheet_row)
                    elif _list:
                        for function_list in _list:
                            sheet_row[12] = json.dumps(function_list, indent=2)
                            extensions.append(sheet_row)
                else:
                    extensions.append(sheet_row)

    logger.info('Updating spreadsheet %s in %s drive with data for Risky Chrome Extensions' % (filename, team_drive))
    sheet.append_sheet(filename, extensions, gsuite_service_account, file_id, sheet_id)
def create_report_no_version_extension(version_not_scanned_in_crxcavator, filename, file_id, sheet_id):
    extensions = ['Name,ID,Version,Version Scanned in Crxcavator,Chrome Link,Devices,Number of Devices,Users,Number of Users,Score,CSP,External JS,External Call,Dangerous Functions,Entry Points'.split(',')]
    for id in version_not_scanned_in_crxcavator:
        for version in version_not_scanned_in_crxcavator[id]:
            if version == 'id' or version == 'latest_version' or version == 'risk_associated_with_latest_version':
                continue

            if not version_not_scanned_in_crxcavator[id]['risk_associated_with_latest_version']:
                sheet_row = [
                    version_not_scanned_in_crxcavator[id]['name'],
                    id,
                    version,
                    version_not_scanned_in_crxcavator[id]['latest_version'],
                    "https://chrome.google.com/webstore/detail/{0}".format(id),
                    ';'.join(version_not_scanned_in_crxcavator[id][version]['devices']),
                    str(len(version_not_scanned_in_crxcavator[id][version]['devices'])),
                    ';'.join(version_not_scanned_in_crxcavator[id][version]['users']),
                    str(len(version_not_scanned_in_crxcavator[id][version]['users'])),
                    '',
                    '',
                    '',
                    '',
                    '',
                    ''
                ]
            else:
                dangerous_funcs = {}
                if version_not_scanned_in_crxcavator[id]['risk_associated_with_latest_version'].dangerous_functions:
                    for key in version_not_scanned_in_crxcavator[id]['risk_associated_with_latest_version'].dangerous_functions:
                        dangerous_funcs[key] = list(version_not_scanned_in_crxcavator[id]['risk_associated_with_latest_version'].dangerous_functions[key].keys())

                sheet_row = [
                    version_not_scanned_in_crxcavator[id]['name'],
                    id,
                    version,
                    version_not_scanned_in_crxcavator[id]['latest_version'],
                    "https://chrome.google.com/webstore/detail/{0}".format(id),
                    ';'.join(version_not_scanned_in_crxcavator[id][version]['devices']),
                    str(len(version_not_scanned_in_crxcavator[id][version]['devices'])),
                    ';'.join(version_not_scanned_in_crxcavator[id][version]['users']),
                    str(len(version_not_scanned_in_crxcavator[id][version]['users'])),
                    str(version_not_scanned_in_crxcavator[id]['risk_associated_with_latest_version'].risk_score),
                    json.dumps(version_not_scanned_in_crxcavator[id]['risk_associated_with_latest_version'].risk_csp, indent=2),
                    json.dumps(version_not_scanned_in_crxcavator[id]['risk_associated_with_latest_version'].risk_external_javascript, indent=2),
                    json.dumps(version_not_scanned_in_crxcavator[id]['risk_associated_with_latest_version'].risk_external_calls, indent=2),
                    json.dumps(dangerous_funcs, indent=2),
                    json.dumps(version_not_scanned_in_crxcavator[id]['risk_associated_with_latest_version'].entry_points, indent=2)
                ]
            char_length = 0
            if len(';'.join(version_not_scanned_in_crxcavator[id][version]['users'])) > 49000:
                char_length = len(';'.join(version_not_scanned_in_crxcavator[id][version]['users']))

            if len(';'.join(version_not_scanned_in_crxcavator[id][version]['devices'])) > 49000 and len(';'.join(version_not_scanned_in_crxcavator[id][version]['devices'])) > char_length:
                char_length = len(';'.join(version_not_scanned_in_crxcavator[id][version]['devices']))

            users_list = []
            device_list = []

            if len(';'.join(version_not_scanned_in_crxcavator[id][version]['users'])) > 49000:
                number_in_each_user_block_list = ceil(len(version_not_scanned_in_crxcavator[id][version]['users']) / ceil(char_length / 45000))
                users_list = [version_not_scanned_in_crxcavator[id][version]['users'][i:i + number_in_each_user_block_list] for i in range(0, len(version_not_scanned_in_crxcavator[id][version]['users']), number_in_each_user_block_list)]

            if len(';'.join(version_not_scanned_in_crxcavator[id][version]['devices'])) > 49000:
                number_in_each_device_block_list = ceil(len(version_not_scanned_in_crxcavator[id][version]['devices']) / ceil(char_length / 45000))
                device_list = [version_not_scanned_in_crxcavator[id][version]['devices'][i:i + number_in_each_device_block_list] for i in range(0, len(version_not_scanned_in_crxcavator[id][version]['devices']), number_in_each_device_block_list)]

            if users_list or device_list:
                if users_list:
                    for index, users in enumerate(users_list):
                        sheet_row[7] = ';'.join(users)
                        sheet_row[8] = str(len(users))
                        if device_list:
                            try:
                                sheet_row[5] = ';'.join(device_list[index])
                                sheet_row[6] = str(len(device_list[index]))
                            except IndexError:
                                sheet_row[6] = ''
                                sheet_row[5] = ''
                        extensions.append(sheet_row)
                elif device_list:
                    for index, devices in enumerate(device_list):
                        sheet_row[5] = ';'.join(devices)
                        sheet_row[6] = str(len(devices))
                        extensions.append(sheet_row)
            else:
                extensions.append(sheet_row)

    logger.info('Updating spreadsheet %s in %s drive with data for Version Unavailable in Crxcavator' % (filename, team_drive))
    sheet.append_sheet(filename, extensions, gsuite_service_account, file_id, sheet_id)