Skip to content

Instantly share code, notes, and snippets.

@acidprime
Created November 11, 2011 09:43
Show Gist options
  • Save acidprime/1357622 to your computer and use it in GitHub Desktop.
Save acidprime/1357622 to your computer and use it in GitHub Desktop.
Simple warranty parser based on the work done by glarizza ./warranty.py >foo.plist ; open foo.plist
#!/usr/bin/python
import ast
import plistlib
import urllib2
import subprocess
system_profiler = "/usr/sbin/system_profiler"
# Generic system_profiler parser
def systemReport():
global spx
spx = {}
# This is our key value schema
SPHardwareDataType = {
'serial': 'serial_number',
}
_dataTypes = {
'SPHardwareDataType': SPHardwareDataType,
}
dataTypes = _dataTypes.keys()
# run the system_profiler command with data types
arguments = [system_profiler,"-xml"] + dataTypes
getspx = subprocess.Popen(arguments, stdout=subprocess.PIPE)
spxOut, err = getspx.communicate()
rootObject = plistlib.readPlistFromString(spxOut)
# Parse datatype top level keys below
for array in rootObject:
for _dataType in _dataTypes:
if array['_dataType'] == _dataType:
_dataTypeSchema = _dataTypes[_dataType]
for key in _dataTypeSchema:
for item in array['_items']:
# add a key to our dict per the schema
spx[key] = item[_dataTypeSchema[key]]
# generate a xml plist so NSPropertyListSerialization is overkill here
systemReport()
# Generate the URL by concatenating the string
url = 'http://selfsolve.apple.com/warrantyChecker.do?sn=' + spx['serial'] + '&country=USA'
request = urllib2.Request(url)
httpResult = urllib2.urlopen(request)
# remove the "^null" string to make tuple with dict inside
warranty = ast.literal_eval(httpResult.read()[4:])
# Just an example of how to parse the data
#for key in warranty: print "%s\t%s" % (key,warranty[key])
print plistlib.writePlistToString(warranty)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment