def gcm_request(self, notification, max_retries = 10): from gcm.gcm import GCM, GCMConnectionException, GCMUnavailableException, GCMAuthenticationException from settings import GOOGLE_GCM_API_KEY from coresql.models import UserProfile logger.info("[GCMClientThread] Handling request for notification: " + str(notification)) gcm_client = GCM(GOOGLE_GCM_API_KEY) registration_ids, collapse_key, delay_while_idle, ttl, data = notification try: response = gcm_client.json_request(registration_ids, data = data, collapse_key = collapse_key, delay_while_idle = delay_while_idle, time_to_live = ttl, retries = max_retries) # Handling errors if 'errors' in response: for error, reg_ids in response['errors'].items(): # Check for errors and act accordingly if error in ['NotRegistered', 'InvalidRegistration']: # Remove reg_ids from database for reg_id in reg_ids: """ Either the owner has turned off notifications (NotRegistered) or there is a problem with the registration_id (InvalidRegistration). In both cases, log the error and set the corresponding registration_id to null in the UserProfile """ try: user_profile = UserProfile.objects.get(c2dm_id = reg_id) user_profile.c2dm_id = None user_profile.save() except Exception: pass logger.error("[GCM " + error + " ERROR] Encountered at notification request: " + str(notification)) else: """ Any other error that should normally not be encountered because of the internal business logic: MissingRegistration, MismatchSenderId, MessageTooBig, MissingCollapseKey """ logger.error("[GCM " + error + " ERROR] Encountered at notification request: " + str(notification)) if 'canonical' in response: for canonical_id, reg_id in response['canonical'].items(): # Repace reg_id with canonical_id in your database try: user_profile = UserProfile.objects.get(c2dm_id=reg_id) user_profile.c2dm_id = canonical_id user_profile.save() except Exception: pass logger.info("[GCM INFO] Finished processing GCM notifications: " + str(notification)) except GCMConnectionException, e: logger.critical("[GCM EXCEPTION] Connection error at GCM request (" + str(notification) + "): " + str(e))
def send_push(api_key, data): response_list = {Field.SUCCESS: {}, Field.ERRORS: {}} for key in api_key: try: gcm = GCM(key) total_of_gcm_ids = len(api_key[key]) start = 0 end = 1000 if total_of_gcm_ids >= Constants.LIMIT_OF_PLAYERS_TO_SEND_PUSH: while total_of_gcm_ids > 0: response = gcm.json_request( registration_ids=api_key[key][start:end], data=data) response_list = list_results_from_push_notification_response( response=response, response_list=response_list) start = end end += Constants.LIMIT_OF_PLAYERS_TO_SEND_PUSH total_of_gcm_ids -= Constants.LIMIT_OF_PLAYERS_TO_SEND_PUSH else: response = gcm.json_request(registration_ids=api_key[key], data=data) response_list = list_results_from_push_notification_response( response=response, response_list=response_list) except Exception: response_list = push_notification_exception_treatment( response_list, api_key[key]) return response_list
def perform(data): gcm = GCM(GCM_APIKEY) msisdn = data.get('msisdn') user = None try: user=User.objects.get(msisdn) except Exception as e: logging.exception("GCM push send failed to this msisdn : %s", msisdn) return if(user.devices and len(user.devices) > 0): devices = user.devices reg_id = devices.get('dev_token', None) if reg_id and data['message']: gcm.plaintext_request(registration_id=reg_id, data=data['message'])
def perform(data): gcm = GCM(GCM_APIKEY) msisdn = data.get('msisdn') user = None try: user = User.objects.get(msisdn) except Exception as e: logging.exception("GCM push send failed to this msisdn : %s", msisdn) return if (user.devices and len(user.devices) > 0): devices = user.devices reg_id = devices.get('dev_token', None) if reg_id and data['message']: gcm.plaintext_request(registration_id=reg_id, data=data['message'])
def get_gcm_client(): gcm_api_key = htk_setting('HTK_GCM_API_KEY', None) if gcm_api_key: client = GCM(gcm_api_key) else: # TODO: raise exception? client = None return client
def test(config): device = torch.device(config.device) # load data data_dir = os.path.join(config.model, config.dataset) test_fname = os.path.join(data_dir, config.test_data) test_data = get_loader(test_fname, config.batch) wordemb = np.loadtxt(os.path.join(data_dir, config.wordmat_file)) # charemb = np.loadtxt(os.path.join(data_dir, config.charmat_file)) # init model model = GCM(dim_word=config.dim_word, num_channel=config.num_channel, kernel_size=config.kernel_size, aspect_kernel_size=config.aspect_kernel_size, num_layer=config.num_layer, num_class=config.num_class, wordmat=wordemb, dropout_rate=config.dropout_rate, device=device) # load model model_save_dir = os.path.join(config.model_save, config.dataset, config.model) result_dir = os.path.join(config.result_save, config.dataset, config.model, 'test') if not os.path.exists(result_dir): os.makedirs(result_dir) save_fout = open(os.path.join(result_dir, 'best.txt'), 'w') model.load_state_dict(torch.load(os.path.join(model_save_dir, 'best.pth'))) model = model.to(device) model.eval() # init loss logit_list = [] rating_list = [] for batch_data in tqdm(test_data): sent_ids, lens, aspect_ids, aspect_lens, polarity, pws = batch_data sent_ids, aspect_ids, polarity = sent_ids.to(device), aspect_ids.to( device), polarity.to(device) logit = model(sent_ids, aspect_ids) save(sent_ids.tolist(), lens.tolist(), aspect_ids.tolist(), aspect_lens.tolist(), polarity.tolist(), logit.tolist(), save_fout, config) logit_list.append(logit.cpu().data.numpy()) rating_list.append(polarity.cpu().data.numpy()) test_acc, test_precision, test_recall, test_f1 = get_score( np.concatenate(logit_list, 0), np.concatenate(rating_list, 0)) print( 'test_acc=%.4f, test_precision=%.4f, test_recall=%.4f, test_f1=%.4f' % (test_acc, test_precision, test_recall, test_f1))
def send_gcm_message(device, app, message_type, data=None): """ Send a Google Cloud Messaging message. """ token_list = [device.token] unique_key = device.token key = '%d-cycle.key' % int(time()) if message_type == TYPE_CALL: unique_key = data['unique_key'] message = get_call_push_payload( unique_key, data['phonenumber'], data['caller_id'], data['attempt'], ) elif message_type == TYPE_MESSAGE: message = get_message_push_payload(data['message']) else: log_middleware_information( '{0} | Trying to sent message of unknown type: {1}', OrderedDict([ ('unique_key', unique_key), ('message_type', message_type), ]), logging.WARNING, device=device, ) gcm = GCM(app.push_key) try: start_time = time() response = gcm.json_request( registration_ids=token_list, data=message, collapse_key=key, priority='high', ) success = response.get('success') canonical = response.get('canonical') errors = response.get('errors') if success: for reg_id, msg_id in success.items(): log_middleware_information( '{0} | GCM \'{1}\' message sent at time:{2} to {3}', OrderedDict([ ('unique_key', unique_key), ('message_type', message_type), ('sent_time', datetime.datetime.fromtimestamp(start_time).strftime( '%H:%M:%S.%f')), ('registration_id', reg_id), ]), logging.INFO, device=device, ) if canonical: for reg_id, new_reg_id in canonical.items(): log_middleware_information( '{0} | Should replace device token {1} with {2} in database', OrderedDict([ ('unique_key', unique_key), ('registration_id', reg_id), ('new_registration_id', new_reg_id), ]), logging.WARNING, device=device, ) if errors: for err_code, reg_id in errors.items(): log_middleware_information( '{0} | Should remove {1} because {2}', OrderedDict([ ('unique_key', unique_key), ('registration_id', reg_id), ('error_code', err_code), ]), logging.WARNING, device=device, ) except GCMAuthenticationException: # Stop and fix your settings. log_middleware_information( '{0} | Our Google API key was rejected!!!', OrderedDict([ ('unique_key', unique_key), ]), logging.ERROR, device=device, ) except ValueError: # Probably your extra options, such as time_to_live, # are invalid. Read error message for more info. log_middleware_information( '{0} | Invalid message/option or invalid GCM response', OrderedDict([ ('unique_key', unique_key), ]), logging.ERROR, device=device, ) except Exception: log_middleware_information( '{0} | Error sending GCM message', OrderedDict([ ('unique_key', unique_key), ]), logging.CRITICAL, device=device, )
import cStringIO as StringIO import ho.pisa as pisa from django.template.loader import get_template from django.template import Context from django.http import HttpResponse from cgi import escape import stomp import requests logger = logging.getLogger(__name__) if settings.HAS_TWILIO: client = TwilioRestClient(settings.TWILIO_ACCOUNT_SID, settings.TWILIO_AUTH_TOKEN) gcm = GCM(settings.GCM_APIKEY) stomp_conn = stomp.Connection() stomp_conn.start() stomp_conn.connect() def clear_cart(request): items = cart.get_cart_items(request) for item in items: item.delete() return HttpResponse() def single_order_point_qr_code(request, object_id=''): order_point = ShoppingPlace.objects.get(pk=object_id)
def send_gcm_message(device, app, message_type, data=None): """ Send a Google Cloud Messaging message. """ token_list = [ device.token, ] unique_key = device.token key = "%d-cycle.key" % int(time()) if message_type == TYPE_CALL: unique_key = data['unique_key'] message = get_call_push_payload( unique_key, data['phonenumber'], data['caller_id'], ) elif message_type == TYPE_MESSAGE: message = get_message_push_payload(data['message']) else: logger.warning( '{0} | Trying to sent message of unknown type: {1}'.format( unique_key, message_type)) gcm = GCM(app.push_key) try: start_time = time() response = gcm.json_request( registration_ids=token_list, data=message, collapse_key=key, priority='high', ) success = response.get('success') canonical = response.get('canonical') errors = response.get('errors') if success: for reg_id, msg_id in success.items(): logger.info( '{0} | GCM \'{1}\' message sent at time:{2} to {3} Data:{4}' .format( unique_key, message_type, datetime.datetime.fromtimestamp(start_time).strftime( '%H:%M:%S.%f'), reg_id, data, )) if canonical: for reg_id, new_reg_id in canonical.items(): logger.warning( '%s | Should replace device token %s with %s in database' % (unique_key, reg_id, new_reg_id)) if errors: for err_code, reg_id in errors.items(): logger.warning('%s | Should remove %s because %s' % (unique_key, reg_id, err_code)) except GCMAuthenticationException: # Stop and fix your settings. logger.error( '{0} | Our Google API key was rejected!!!'.format(unique_key)) except ValueError: # Probably your extra options, such as time_to_live, # are invalid. Read error message for more info. logger.error( '{0} | Invalid message/option or invalid GCM response'.format( unique_key)) except Exception: logger.exception('{0} | Error sending GCM message'.format(unique_key))
def gcm_request(self, notification, max_retries=10): from gcm.gcm import GCM, GCMConnectionException, GCMUnavailableException, GCMAuthenticationException from settings import GOOGLE_GCM_API_KEY from coresql.models import UserProfile logger.info("[GCMClientThread] Handling request for notification: " + str(notification)) gcm_client = GCM(GOOGLE_GCM_API_KEY) registration_ids, collapse_key, delay_while_idle, ttl, data = notification try: response = gcm_client.json_request( registration_ids, data=data, collapse_key=collapse_key, delay_while_idle=delay_while_idle, time_to_live=ttl, retries=max_retries, ) # Handling errors if "errors" in response: for error, reg_ids in response["errors"].items(): # Check for errors and act accordingly if error in ["NotRegistered", "InvalidRegistration"]: # Remove reg_ids from database for reg_id in reg_ids: """ Either the owner has turned off notifications (NotRegistered) or there is a problem with the registration_id (InvalidRegistration). In both cases, log the error and set the corresponding registration_id to null in the UserProfile """ try: user_profile = UserProfile.objects.get(c2dm_id=reg_id) user_profile.c2dm_id = None user_profile.save() except Exception: pass logger.error( "[GCM " + error + " ERROR] Encountered at notification request: " + str(notification) ) else: """ Any other error that should normally not be encountered because of the internal business logic: MissingRegistration, MismatchSenderId, MessageTooBig, MissingCollapseKey """ logger.error( "[GCM " + error + " ERROR] Encountered at notification request: " + str(notification) ) if "canonical" in response: for canonical_id, reg_id in response["canonical"].items(): # Repace reg_id with canonical_id in your database try: user_profile = UserProfile.objects.get(c2dm_id=reg_id) user_profile.c2dm_id = canonical_id user_profile.save() except Exception: pass logger.info("[GCM INFO] Finished processing GCM notifications: " + str(notification)) except GCMConnectionException, e: logger.critical("[GCM EXCEPTION] Connection error at GCM request (" + str(notification) + "): " + str(e))
def train(config): device = torch.device(config.device) # random seed if torch.cuda.is_available(): torch.cuda.manual_seed_all(config.seed) # load data data_dir = os.path.join(config.model, config.dataset) train_fname = os.path.join(data_dir, config.train_data) test_fname = os.path.join(data_dir, config.test_data) train_data = get_loader(train_fname, config.batch) test_data = get_loader(test_fname, config.batch) wordemb = np.loadtxt(os.path.join(data_dir, config.wordmat_file)) # init model model = GCM(dim_word=config.dim_word, num_channel=config.num_channel, kernel_size=config.kernel_size, aspect_kernel_size=config.aspect_kernel_size, num_layer=config.num_layer, num_class=config.num_class, wordmat=wordemb, dropout_rate=config.dropout_rate, device=device) model = model.to(device) # init loss cross_entropy = nn.CrossEntropyLoss() # train # summary writer writer = SummaryWriter('logs/%s/%s/%s' % (config.dataset, config.model, config.timestr)) model_save_dir = os.path.join(config.model_save, config.dataset, config.model) if not os.path.exists(model_save_dir): os.makedirs(model_save_dir) result_dir = os.path.join(config.result_save, config.dataset, config.model, 'train') if not os.path.exists(result_dir): os.makedirs(result_dir) parameters = filter(lambda p: p.requires_grad, model.parameters()) optim = torch.optim.Adam(parameters, lr=config.lr, weight_decay=config.weight_decay) best_acc = 0.0 for epoch in tqdm(range(config.max_epoch)): # train save_fout = open(os.path.join(result_dir, '{}.txt'.format(epoch)), 'w') model.train() for i, batch_data in tqdm(enumerate(train_data)): model.zero_grad() sent_ids, lens, aspect_ids, aspect_lens, polarity, pws = batch_data sent_ids, aspect_ids, polarity = sent_ids.to( device), aspect_ids.to(device), polarity.to(device) logit = model(sent_ids, aspect_ids) save(sent_ids.tolist(), lens.tolist(), aspect_ids.tolist(), aspect_lens.tolist(), polarity.tolist(), logit.tolist(), save_fout, config) loss = cross_entropy(logit, polarity) writer.add_scalar('loss', loss, len(train_data) * epoch + i) loss.backward() optim.step() # eval model.eval() # eval on train logit_list = [] rating_list = [] for batch_data in tqdm(train_data): sent_ids, lens, aspect_ids, aspect_lens, polarity, pws = batch_data sent_ids, aspect_ids, polarity = sent_ids.to( device), aspect_ids.to(device), polarity.to(device) logit = model(sent_ids, aspect_ids) # loss = cross_entropy(logit, polarity) logit_list.append(logit.cpu().data.numpy()) rating_list.append(polarity.cpu().data.numpy()) train_acc, train_precision, train_recall, train_f1 = get_score( np.concatenate(logit_list, 0), np.concatenate(rating_list, 0)) # writer.add_scalar('train_loss', train_loss, epoch) writer.add_scalar('train_acc', train_acc, epoch) writer.add_scalar('train_precision', train_precision, epoch) writer.add_scalar('train_recall', train_recall, epoch) writer.add_scalar('train_f1', train_f1, epoch) # eval on test logit_list = [] rating_list = [] for batch_data in tqdm(test_data): sent_ids, lens, aspect_ids, aspect_lens, polarity, pws = batch_data sent_ids, aspect_ids, polarity = sent_ids.to( device), aspect_ids.to(device), polarity.to(device) logit = model(sent_ids, aspect_ids) # loss = cross_entropy(logit, polarity) logit_list.append(logit.cpu().data.numpy()) rating_list.append(polarity.cpu().data.numpy()) test_acc, test_precision, test_recall, test_f1 = get_score( np.concatenate(logit_list, 0), np.concatenate(rating_list, 0)) # writer.add_scalar('test_loss', test_loss, epoch) writer.add_scalar('test_acc', test_acc, epoch) writer.add_scalar('test_precision', test_precision, epoch) writer.add_scalar('test_recall', test_recall, epoch) writer.add_scalar('test_f1', test_f1, epoch) print( 'epoch %2d : ' ' train_acc=%.4f, train_precision=%.4f, train_recall=%.4f,train_f1=%.4f,' ' test_acc=%.4f, test_precision=%.4f, test_recall=%.4f, test_f1=%.4f' % (epoch, train_acc, train_precision, train_recall, train_f1, test_acc, test_precision, test_recall, test_f1)) # show parameters for name, param in model.named_parameters(): writer.add_histogram(name, param, epoch, bins='doane') # save model torch.save(model.state_dict(), os.path.join(model_save_dir, '{}.pth'.format(epoch))) if test_acc > best_acc: torch.save(model.state_dict(), os.path.join(model_save_dir, 'best.pth')) best_acc = test_acc