2012-11-08 15:13:01 +01:00
|
|
|
#!/usr/bin/python
|
|
|
|
|
2012-12-01 19:27:02 +01:00
|
|
|
import sys, gcm, psycopg2, json
|
2012-11-08 15:13:01 +01:00
|
|
|
|
|
|
|
# I'm not checking my key in...
|
|
|
|
import mykey
|
|
|
|
|
2012-12-01 19:27:02 +01:00
|
|
|
def usage():
|
|
|
|
print 'usage:', sys.argv[0], '[--to <name>] msg'
|
|
|
|
sys.exit()
|
2012-11-08 15:13:01 +01:00
|
|
|
|
|
|
|
def msgViaGCM( devid, msg ):
|
|
|
|
instance = gcm.GCM( mykey.myKey )
|
|
|
|
data = { 'title' : 'Msg from Darth',
|
|
|
|
'msg' : msg,
|
|
|
|
}
|
|
|
|
|
|
|
|
response = instance.json_request( registration_ids = [devid],
|
|
|
|
data = data )
|
|
|
|
if 'errors' in response:
|
2012-12-01 19:27:02 +01:00
|
|
|
response = response['errors']
|
|
|
|
if 'NotRegistered' in response:
|
|
|
|
ids = response['NotRegistered']
|
|
|
|
for id in ids:
|
|
|
|
print 'need to remove "', id, '" from db'
|
2012-11-08 15:13:01 +01:00
|
|
|
else:
|
|
|
|
print 'no errors'
|
|
|
|
|
|
|
|
def main():
|
2012-12-01 19:27:02 +01:00
|
|
|
to = None
|
2012-11-08 15:13:01 +01:00
|
|
|
msg = sys.argv[1]
|
2012-12-01 19:27:02 +01:00
|
|
|
if msg == '--to':
|
|
|
|
to = sys.argv[2]
|
|
|
|
msg = sys.argv[3]
|
|
|
|
elif 2 < len(sys.argv):
|
|
|
|
usage()
|
|
|
|
if not to in mykey.devids.keys():
|
|
|
|
print 'Unknown --to param;', to, 'not in', ','.join(mykey.devids.keys())
|
|
|
|
usage()
|
|
|
|
if not to: usage()
|
|
|
|
devid = mykey.devids[to]
|
|
|
|
print 'sending: "%s" to' % msg, to
|
|
|
|
msgViaGCM( devid, msg )
|
2012-11-08 15:13:01 +01:00
|
|
|
|
|
|
|
##############################################################################
|
|
|
|
if __name__ == '__main__':
|
|
|
|
main()
|