xwords/xwords4/relay/scripts/gcm_msg.py
Eric House d34ac1f86d rewrite to work without gcm library (whose lack of support for some
documented params makes it impossible to try using them)
2013-01-02 21:08:10 -08:00

49 lines
1.3 KiB
Python
Executable file

#!/usr/bin/python
import sys, psycopg2, json, urllib, urllib2
# I'm not checking my key in...
import mykey
GCM_URL = 'https://android.googleapis.com/gcm/send'
def usage():
print 'usage:', sys.argv[0], '[--to <name>] msg'
sys.exit()
def sendMsg( devid, msg ):
values = {
'registration_ids': [ devid ],
'data' : { 'title' : 'Msg from Darth2',
'msg' : msg,
}
}
params = json.dumps( values )
req = urllib2.Request("https://android.googleapis.com/gcm/send", params )
req.add_header( 'Content-Type' , 'application/x-www-form-urlencoded;charset=UTF-8' )
req.add_header( 'Authorization' , 'key=' + mykey.myKey )
req.add_header('Content-Type', 'application/json' )
response = urllib2.urlopen( req )
response = response.read()
print response
def main():
to = None
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
sendMsg( devid, msg )
##############################################################################
if __name__ == '__main__':
main()