13-inch MacBook Pro (non Touch Bar) Battery Replacement Program - check script

misterryan
New Contributor

I made a script to help me check if any of our computers are eligible for this program. I hope it can help others too.

https://www.apple.com/support/13inch-macbookpro-battery-replacement/

import requests
import lxml.html
import csv

# python 3.4
# 13-inch MacBook Pro (non Touch Bar) Battery Replacement Program
# based on https://www.apple.com/support/13inch-macbookpro-battery-replacement/
# using csv file from export feature of jamf
# format of csv file: Computer Name,Total RAM MB,Operating System Version,Drive Capacity MB,Battery Condition,DA Asset Tag,Last TM Backup,Serial Number,Model,Managed,Last Check-in,
# use at your own risk
# YMMV

not_eligible_count = 0
eligible_count = 0
total_items = 0


def eligible_for_battery(serial_number):
    global not_eligible_count
    global eligible_count
    global total_items

    url = 'https://www.apple.com/support/13inch-macbookpro-battery-replacement/'

    form_data = {
        'ihc_textbox': serial_number,
        'ihc_submit': 'Submit',
    }

    thing = False

    while thing is False:
        response = requests.post(url, data=form_data)
        if response.status_code == requests.codes.ok:
            total_items += 1
            thing = True
        else:
            thing = False

    tree = lxml.html.document_fromstring(response.content)
    is_not_eligible = tree.body.get_element_by_id('ihc_E01')
    if is_not_eligible is not None:
        not_eligible_count += 1
        return not_eligible_count, serial_number
    eligible = tree.body.get_element_by_id('ihc_E00')
    if eligible:
        eligible_count += 1
        return eligible.text, serial_number
    else:
        print("something didn't work right")


def check_against_jamf():
    global not_eligible_count
    global eligible_count
    global total_items

    rowcount = 0
    csvfile = 'allcomputersjamf.csv'
    datafile = open(csvfile, 'r')
    csvreader = csv.reader(datafile)
    for row in csvreader:
        eligible_for_battery(row[7])
        rowcount += 1
    print("eligible: {}, not eligible: {}, total devices checked: {}, rows in csv: {}".format(
        eligible_count, not_eligible_count, total_items, rowcount))


if __name__ == '__main__':
    check_against_jamf()
0 REPLIES 0