<s> import re <EOL> import sys <EOL> import urllib <EOL> import urllib2 <EOL> import datetime <EOL> import time <EOL> import base64 <EOL> json_available = True <EOL> json = None <EOL> try : <EOL> import simplejson as json <EOL> except ImportError : <EOL> try : <EOL> import json <EOL> except ImportError : <EOL> try : <EOL> from django . utils import simplejson as json <EOL> except ImportError : <EOL> json_available = False <EOL> class FindMyIPhone ( object ) : <EOL> partition = None <EOL> def __init__ ( self , username , password , debug = False ) : <EOL> if not json_available : <EOL> raise Exception ( "<STR_LIT>" ) <EOL> self . devices = [ ] <EOL> self . debug = debug <EOL> self . username = username <EOL> self . password = password <EOL> self . get_partition ( ) <EOL> self . update_devices ( ) <EOL> def get_partition ( self ) : <EOL> body = json . dumps ( { <EOL> "<STR_LIT>" : { <EOL> "<STR_LIT>" : "<STR_LIT>" , <EOL> "<STR_LIT>" : "<STR_LIT>" , <EOL> "<STR_LIT>" : "<STR_LIT>" , <EOL> "<STR_LIT>" : "<STR_LIT>" , <EOL> "<STR_LIT>" : <NUM_LIT> , <EOL> "<STR_LIT>" : "<STR_LIT>" , <EOL> "<STR_LIT>" : <NUM_LIT:0> , <EOL> "<STR_LIT>" : "<STR_LIT>" <EOL> } <EOL> } ) <EOL> headers , content = self . post ( '<STR_LIT>' % self . username , body , None , True ) <EOL> self . partition = headers . get ( '<STR_LIT>' ) <EOL> def locate ( self , device_num = <NUM_LIT:0> , max_wait = <NUM_LIT> ) : <EOL> start = int ( time . time ( ) ) <EOL> while not ( hasattr ( self . devices [ device_num ] , '<STR_LIT>' ) and self . devices [ device_num ] . location_finished ) : <EOL> if int ( time . time ( ) ) - start > max_wait : <EOL> raise Exception ( "<STR_LIT>" % max_wait ) <EOL> time . sleep ( <NUM_LIT:5> ) <EOL> self . update_devices ( ) <EOL> device = self . devices [ device_num ] <EOL> return { <EOL> '<STR_LIT>' : device . latitude , <EOL> '<STR_LIT>' : device . longitude , <EOL> '<STR_LIT>' : device . horizontal_accuracy , <EOL> '<STR_LIT>' : device . location_timestamp , <EOL> } <EOL> def send_message ( self , msg , alarm = False , device_num = <NUM_LIT:0> , subject = '<STR_LIT>' ) : <EOL> device = self . devices [ device_num ] <EOL> body = json . dumps ( { <EOL> "<STR_LIT>" : { <EOL> "<STR_LIT>" : "<STR_LIT>" , <EOL> "<STR_LIT>" : "<STR_LIT>" , <EOL> "<STR_LIT>" : "<STR_LIT>" , <EOL> "<STR_LIT>" : "<STR_LIT>" , <EOL> "<STR_LIT>" : <NUM_LIT> , <EOL> "<STR_LIT>" : "<STR_LIT>" , <EOL> "<STR_LIT>" : "<STR_LIT>" , <EOL> "<STR_LIT>" : device . id , <EOL> "<STR_LIT>" : False <EOL> } , <EOL> "<STR_LIT>" : device . id , <EOL> "<STR_LIT>" : { <EOL> "<STR_LIT>" : <NUM_LIT> , <EOL> "<STR_LIT>" : "<STR_LIT>" , <EOL> "<STR_LIT>" : "<STR_LIT>" , <EOL> "<STR_LIT>" : True , <EOL> "<STR_LIT>" : None , <EOL> "<STR_LIT>" : <NUM_LIT> , <EOL> "<STR_LIT>" : <NUM_LIT> , <EOL> "<STR_LIT>" : "<STR_LIT>" , <EOL> "<STR_LIT>" : <NUM_LIT> , <EOL> "<STR_LIT>" : <NUM_LIT> , <EOL> "<STR_LIT>" : { <EOL> "<STR_LIT>" : - <NUM_LIT> , <EOL> "<STR_LIT>" : - <NUM_LIT> , <EOL> "<STR_LIT>" : <NUM_LIT> , <EOL> "<STR_LIT>" : "<STR_LIT>" , <EOL> "<STR_LIT>" : "<STR_LIT>" <EOL> } , <EOL> "<STR_LIT>" : True <EOL> } , <EOL> "<STR_LIT>" : alarm , <EOL> "<STR_LIT>" : subject , <EOL> "<STR_LIT:text>" : msg <EOL> } ) <EOL> self . post ( '<STR_LIT>' % self . username , body ) <EOL> def remote_lock ( self , passcode , device_num = <NUM_LIT:0> ) : <EOL> device = self . devices [ device_num ] <EOL> body = json . dumps ( { <EOL> "<STR_LIT>" : { <EOL> "<STR_LIT>" : "<STR_LIT>" , <EOL> "<STR_LIT>" : "<STR_LIT>" , <EOL> "<STR_LIT>" : "<STR_LIT>" , <EOL> "<STR_LIT>" : "<STR_LIT>" , <EOL> "<STR_LIT>" : <NUM_LIT> , <EOL> "<STR_LIT>" : "<STR_LIT>" , <EOL> "<STR_LIT>" : "<STR_LIT>" , <EOL> "<STR_LIT>" : device . id , <EOL> "<STR_LIT>" : False <EOL> } , <EOL> "<STR_LIT>" : device . id , <EOL> "<STR_LIT>" : "<STR_LIT>" , <EOL> "<STR_LIT>" : passcode , <EOL> "<STR_LIT>" : { <EOL> "<STR_LIT>" : <NUM_LIT> , <EOL> "<STR_LIT>" : "<STR_LIT>" , <EOL> "<STR_LIT>" : "<STR_LIT>" , <EOL> "<STR_LIT>" : True , <EOL> "<STR_LIT>" : None , <EOL> "<STR_LIT>" : <NUM_LIT> , <EOL> "<STR_LIT>" : <NUM_LIT> , <EOL> "<STR_LIT>" : "<STR_LIT>" , <EOL> "<STR_LIT>" : <NUM_LIT> , <EOL> "<STR_LIT>" : <NUM_LIT> , <EOL> "<STR_LIT>" : { <EOL> "<STR_LIT>" : - <NUM_LIT> , <EOL> "<STR_LIT>" : - <NUM_LIT> , <EOL> "<STR_LIT>" : <NUM_LIT> , <EOL> "<STR_LIT>" : "<STR_LIT>" , <EOL> "<STR_LIT>" : "<STR_LIT>" <EOL> } , <EOL> "<STR_LIT>" : True <EOL> } <EOL> } ) <EOL> self . post ( '<STR_LIT>' % self . username , body ) <EOL> def remote_wipe ( self , device_num = <NUM_LIT:0> ) : <EOL> device = self . devices [ device_num ] <EOL> body = json . dumps ( { <EOL> "<STR_LIT>" : { <EOL> "<STR_LIT>" : "<STR_LIT>" , <EOL> "<STR_LIT>" : "<STR_LIT>" , <EOL> "<STR_LIT>" : "<STR_LIT>" , <EOL> "<STR_LIT>" : "<STR_LIT>" , <EOL> "<STR_LIT>" : <NUM_LIT> , <EOL> "<STR_LIT>" : "<STR_LIT>" , <EOL> "<STR_LIT>" : "<STR_LIT>" , <EOL> "<STR_LIT>" : device . id , <EOL> "<STR_LIT>" : False <EOL> } , <EOL> "<STR_LIT>" : device . id , <EOL> "<STR_LIT>" : { <EOL> "<STR_LIT>" : <NUM_LIT> , <EOL> "<STR_LIT>" : "<STR_LIT>" , <EOL> "<STR_LIT>" : "<STR_LIT>" , <EOL> "<STR_LIT>" : True , <EOL> "<STR_LIT>" : None , <EOL> "<STR_LIT>" : <NUM_LIT> , <EOL> "<STR_LIT>" : <NUM_LIT> , <EOL> "<STR_LIT>" : "<STR_LIT>" , <EOL> "<STR_LIT>" : <NUM_LIT> , <EOL> "<STR_LIT>" : <NUM_LIT> , <EOL> "<STR_LIT>" : { <EOL> "<STR_LIT>" : - <NUM_LIT> , <EOL> "<STR_LIT>" : - <NUM_LIT> , <EOL> "<STR_LIT>" : <NUM_LIT> , <EOL> "<STR_LIT>" : "<STR_LIT>" , <EOL> "<STR_LIT>" : "<STR_LIT>" <EOL> } , <EOL> "<STR_LIT>" : True <EOL> } <EOL> } ) <EOL> self . post ( '<STR_LIT>' % self . username , body ) <EOL> def update_devices ( self ) : <EOL> body = json . dumps ( { <EOL> "<STR_LIT>" : { <EOL> "<STR_LIT>" : "<STR_LIT>" , <EOL> "<STR_LIT>" : "<STR_LIT>" , <EOL> "<STR_LIT>" : "<STR_LIT>" , <EOL> "<STR_LIT>" : "<STR_LIT>" , <EOL> "<STR_LIT>" : <NUM_LIT> , <EOL> "<STR_LIT>" : "<STR_LIT>" , <EOL> "<STR_LIT>" : <NUM_LIT:0> , <EOL> "<STR_LIT>" : "<STR_LIT>" <EOL> } <EOL> } ) <EOL> header , json_str = self . post ( '<STR_LIT>' % self . username , body , None , True ) <EOL> json_obj = json . loads ( json_str ) <EOL> if None == json_obj : <EOL> raise Exception ( '<STR_LIT>' ) <EOL> if json_obj . get ( '<STR_LIT:error>' ) is not None : <EOL> raise Exception ( '<STR_LIT>' % json_obj [ '<STR_LIT:error>' ] ) <EOL> self . devices = [ ] <EOL> for json_device in json_obj [ '<STR_LIT:content>' ] : <EOL> device = Device ( ) <EOL> if json_device . get ( '<STR_LIT:location>' ) and type ( json_device [ '<STR_LIT:location>' ] ) is dict : <EOL> device . location_timestamp = datetime . datetime . fromtimestamp ( <EOL> json_device [ '<STR_LIT:location>' ] [ '<STR_LIT>' ] / <NUM_LIT:1000> ) <EOL> device . location_type = json_device [ '<STR_LIT:location>' ] [ '<STR_LIT>' ] <EOL> device . horizontal_accuracy = json_device [ '<STR_LIT:location>' ] [ '<STR_LIT>' ] <EOL> device . location_finished = json_device [ '<STR_LIT:location>' ] [ '<STR_LIT>' ] <EOL> device . longitude = json_device [ '<STR_LIT:location>' ] [ '<STR_LIT>' ] <EOL> device . latitude = json_device [ '<STR_LIT:location>' ] [ '<STR_LIT>' ] <EOL> device . is_locating = json_device [ '<STR_LIT>' ] <EOL> device . device_model = json_device [ '<STR_LIT>' ] <EOL> device . device_status = json_device [ '<STR_LIT>' ] <EOL> device . id = json_device [ '<STR_LIT:id>' ] <EOL> device . name = json_device [ '<STR_LIT:name>' ] . encode ( '<STR_LIT:utf-8>' ) <EOL> device . deviceClass = json_device [ '<STR_LIT>' ] <EOL> device . chargingStatus = json_device [ '<STR_LIT>' ] <EOL> device . batteryLevel = json_device [ '<STR_LIT>' ] <EOL> self . devices . append ( device ) <EOL> def post ( self , url , body , headers = None , return_headers = False ) : <EOL> if self . partition : <EOL> url = '<STR_LIT>' + self . partition + url <EOL> else : <EOL> url = '<STR_LIT>' + url <EOL> if type ( body ) is dict : <EOL> body = urllib . urlencode ( body ) <EOL> headers = headers or { } <EOL> headers . update ( { <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT:1.0>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' % base64 . encodestring ( '<STR_LIT>' % ( self . username , self . password , ) ) [ : - <NUM_LIT:1> ] , <EOL> } ) <EOL> opener = urllib2 . build_opener ( HTTPErrorProcessor ( ) ) <EOL> req = urllib2 . Request ( url , '<STR_LIT:%s>' % body , headers ) <EOL> resp = opener . open ( req ) <EOL> if return_headers : <EOL> return resp . headers , resp . read ( ) <EOL> return resp . read ( ) <EOL> class Device ( object ) : <EOL> id = '<STR_LIT>' <EOL> name = '<STR_LIT>' <EOL> def __init__ ( self , ** kwargs ) : <EOL> self . __dict__ . update ( kwargs ) <EOL> def __repr__ ( self ) : <EOL> return '<STR_LIT>' % ( self . id , self . name , ) <EOL> class HTTPErrorProcessor ( urllib2 . HTTPErrorProcessor ) : <EOL> """<STR_LIT>""" <EOL> handler_order = <NUM_LIT:1000> <EOL> def http_response ( self , request , response ) : <EOL> code , msg , hdrs = response . code , response . msg , response . info ( ) <EOL> if code == <NUM_LIT> : <EOL> code = <NUM_LIT> <EOL> if not ( <NUM_LIT:200> <= code < <NUM_LIT> ) : <EOL> response = self . parent . error ( <EOL> '<STR_LIT:http>' , request , response , code , msg , hdrs ) <EOL> return response <EOL> https_response = http_response <EOL> if __name__ == "<STR_LIT:__main__>" : <EOL> try : <EOL> username = sys . argv [ <NUM_LIT:1> ] <EOL> password = sys . argv [ <NUM_LIT:2> ] <EOL> reference = None <EOL> if len ( sys . argv ) == <NUM_LIT:5> : <EOL> reference = { '<STR_LIT>' : float ( sys . argv [ <NUM_LIT:3> ] ) , <EOL> '<STR_LIT>' : float ( sys . argv [ <NUM_LIT:4> ] ) } <EOL> fmi = FindMyIPhone ( sys . argv [ <NUM_LIT:1> ] , sys . argv [ <NUM_LIT:2> ] ) <EOL> for i , device in enumerate ( fmi . devices ) : <EOL> location = fmi . locate ( i ) <EOL> print device . name , <EOL> if reference : <EOL> distance = [ ] <EOL> for x in [ '<STR_LIT>' , '<STR_LIT>' ] : <EOL> distance . append ( str ( abs ( reference [ x ] - location [ x ] ) ) ) <EOL> location [ '<STR_LIT>' ] = ( distance [ <NUM_LIT:0> ] , distance [ <NUM_LIT:1> ] ) <EOL> print location <EOL> except IndexError : <EOL> print '<STR_LIT>' % sys . argv [ <NUM_LIT:0> ] </s>
