xwords/xwords4/relay/scripts/gcm_msg.py

48 lines
1.2 KiB
Python
Raw Normal View History

2012-11-08 15:13:01 +01:00
#!/usr/bin/python
import sys, gcm, psycopg2, json
2012-11-08 15:13:01 +01:00
# I'm not checking my key in...
import mykey
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:
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():
to = None
2012-11-08 15:13:01 +01:00
msg = sys.argv[1]
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()