<s> import rsa <EOL> import urllib2 <EOL> from shared import db <EOL> from base64 import b64decode <EOL> from json import dumps <EOL> from sqlalchemy . dialects . mysql import INTEGER <EOL> from datetime import datetime <EOL> from config import google_api_key <EOL> from models import Subscription , Message <EOL> gcm_url = '<STR_LIT>' <EOL> class Gcm ( db . Model ) : <EOL> id = db . Column ( INTEGER ( unsigned = True ) , primary_key = True ) <EOL> uuid = db . Column ( db . VARCHAR ( <NUM_LIT> ) , nullable = False ) <EOL> gcmid = db . Column ( db . TEXT , nullable = False ) <EOL> pubkey = db . Column ( db . TEXT , default = None ) <EOL> timestamp_created = db . Column ( db . TIMESTAMP , default = datetime . utcnow ) <EOL> def __init__ ( self , device , gcmid , pubkey = None ) : <EOL> self . uuid = device <EOL> self . gcmid = gcmid <EOL> self . pubkey = pubkey <EOL> def __repr__ ( self ) : <EOL> return '<STR_LIT>' % self . uuid <EOL> def as_dict ( self ) : <EOL> data = { <EOL> "<STR_LIT>" : self . service . as_dict ( ) , <EOL> "<STR_LIT>" : self . gcmId , <EOL> "<STR_LIT>" : int ( self . timestamp_created . strftime ( '<STR_LIT:%s>' ) ) , <EOL> } <EOL> return data <EOL> @ staticmethod <EOL> def send_message ( message ) : <EOL> """<STR_LIT>""" <EOL> subscriptions = Subscription . query . filter_by ( service = message . service ) . all ( ) <EOL> if len ( subscriptions ) == <NUM_LIT:0> : <EOL> return <NUM_LIT:0> <EOL> gcm_filter = Gcm . query . filter ( Gcm . uuid . in_ ( [ l . device for l in subscriptions ] ) ) . all ( ) <EOL> devices_plain = [ r . gcmid for r in gcm_filter if r . pubkey is None ] <EOL> devices_crypto = [ r for r in gcm_filter if r . pubkey is not None ] <EOL> if len ( devices_plain ) > <NUM_LIT:0> : <EOL> data = { "<STR_LIT:message>" : dumps ( message . as_dict ( ) ) , "<STR_LIT>" : True } <EOL> Gcm . gcm_send ( devices_plain , data ) <EOL> for device in devices_crypto : <EOL> pubkey = rsa . PublicKey . load_pkcs1 ( b64decode ( device . pubkey ) , '<STR_LIT>' ) <EOL> message_enc = rsa . encrypt ( dumps ( message . as_dict ( ) ) , pubkey ) <EOL> data = { "<STR_LIT:message>" : message_enc , "<STR_LIT>" : True } <EOL> Gcm . gcm_send ( [ device . gcmid ] , data ) <EOL> if len ( gcm_filter ) > <NUM_LIT:0> : <EOL> uuids = [ g . uuid for g in gcm_filter ] <EOL> gcm_subscriptions = Subscription . query . filter_by ( service = message . service ) . filter ( Subscription . device . in_ ( uuids ) ) . all ( ) <EOL> for l in gcm_subscriptions : <EOL> l . timestamp_checked = datetime . utcnow ( ) <EOL> db . session . commit ( ) <EOL> return len ( gcm_filter ) <EOL> @ staticmethod <EOL> def gcm_send ( ids , data ) : <EOL> data = dumps ( { <EOL> "<STR_LIT>" : ids , <EOL> "<STR_LIT:data>" : data , <EOL> } ) <EOL> headers = { <EOL> '<STR_LIT>' : '<STR_LIT>' % google_api_key , <EOL> '<STR_LIT:Content-Type>' : '<STR_LIT:application/json>' , <EOL> } <EOL> req = urllib2 . Request ( '<STR_LIT>' , data , headers ) <EOL> urllib2 . urlopen ( req ) . read ( ) </s>
