Being a Pascal programmer with little to no clue about Python I am trying to write a small and simple Python script utilizing pyicloud https://github.com/picklepete/pyicloud/ that redirects the location data json of all devices every so and so seconds over an UDP socket with desired output as shown in the bottom. The python script should be something like:
from pyicloud import PyiCloudService
api = []
api.append(PyiCloudService('JAppleseedAppleIdEmailHere', 'password'))
api.append(PyiCloudService('SomeOtherAppleIdEmailHere', 'psadfsdfassword'))
# more accounts here if desired...
#PeerIp = '10.0.0.5'
PeerIp = '127.0.0.1'
PeerPort = 2028
import time, threading, socket, json, struct
ContinueDevicesPostionUpdate = True
def DevicesPostionUpdate():
if ContinueDevicesPostionUpdate:
# print(time.ctime())
threading.Timer(10, DevicesPostionUpdate).start()
# Code for generating json from api.devices and api.devices[].location()
# This is where i get all sort of errors in my shotgun programming
MESSAGE = bytes(smsg, 'utf-8')
sock.sendto(MESSAGE, (PeerIp, PeerPort))
sock = socket.socket(socket.AF_INET, # Internet
socket.SOCK_DGRAM) # UDP
DevicesPostionUpdate()
For example the:
for k, v in api:
for key, value in api[k].devices.items():
print (api[k].devices[key].location())
#results in:
Exception in thread Thread-4:
Traceback (most recent call last):
File "C:\Python33\lib\threading.py", line 901, in _bootstrap_inner
self.run()
File "C:\Python33\lib\threading.py", line 1142, in run
self.function(*self.args, **self.kwargs)
File "test.py", line 17, in DevicesPostionUpdate
for k, v in api:
TypeError: 'PyiCloudService' object is not iterable
Or
MESSAGE = json.dumps([dict(device=device, location=device.location()) for a in api for device in a.devices.values()]).encode()
#results in:
Traceback (most recent call last):
File "test.py", line 23, in <module>
DevicesPostionUpdate()
File "test.py", line 17, in DevicesPostionUpdate
MESSAGE = json.dumps([dict(device=device, location=device.location()) for a in api for device in a.devices.values()]).encode()
File "C:\Python33\lib\json\__init__.py", line 236, in dumps
return _default_encoder.encode(obj)
File "C:\Python33\lib\json\encoder.py", line 191, in encode
chunks = self.iterencode(o, _one_shot=True)
File "C:\Python33\lib\json\encoder.py", line 249, in iterencode
return _iterencode(o, 0)
File "C:\Python33\lib\json\encoder.py", line 173, in default
raise TypeError(repr(o) + " is not JSON serializable")
TypeError: <AppleDevice(iPhone 4S: Johnny Appleseed's iPhone)> is not JSON serializable
Desired output should be something like:
{
{
u'i9vbKRGIcLYqJnXMd1b257kUWnoyEBcEh6yM+IfmiMLh7BmOpALS+w==': <AppleDevice(iPhone 4S: Johnny Appleseed's iPhone)>,
{
u'timeStamp': 1357753796553,
u'locationFinished': True,
u'longitude': -0.14189,
u'positionType': u'GPS',
u'locationType': None,
u'latitude': 51.501364,
u'isOld': False,
u'horizontalAccuracy': 5.0
}
}
,
{
u'reGYDh9XwqNWTGIhNBuEwP1ds0F/Lg5t/fxNbI4V939hhXawByErk+HYVNSUzmWV': <AppleDevice(MacBook Air 11": Johnny Appleseed's MacBook Air)>
{
u'timeStamp': 1357753796553,
u'locationFinished': True,
u'longitude': -0.14189,
u'positionType': u'GPS',
u'locationType': None,
u'latitude': 51.501364,
u'isOld': False,
u'horizontalAccuracy': 5.0
}
}
,
{
u'reGYDh9XwqNWTGIhNBuEwP1ds0F/Lg5t/fxNbI4V939hhXawByErk+HYVNSUzmWV': <AppleDevice(iPhone 4S: Some Other's iPhone)>
{
u'timeStamp': 1357753796553,
u'locationFinished': True,
u'longitude': -0.14189,
u'positionType': u'GPS',
u'locationType': None,
u'latitude': 51.501364,
u'isOld': False,
u'horizontalAccuracy': 5.0
}
}
}
If you do not wish to help me here you could always earn a 30$ by presenting your solution at freelancer.com