def majAddress(): try: if (request.method =='POST'): logger.error(utils.get_debug_all(request)) # gets inseeid Siren__c = request.args.get('Siren__c') # gets new city city = request.args.get('city') if (city == '' or city == None): return utils.returnResponse("Please provide a city", 403, None, None) #check if siren__c exits if (postgres.__checkAccountBySiren(Siren__c) == False): return utils.returnResponse("Please provide a Siren", 403, None, None) # check if city exist # updates postgres.__updateCityInAccountBySiren(city, Siren__c) return "ok" elif (request.method == 'GET'): logger.error(utils.get_debug_all(request)) cookie, cookie_exists = utils.getCookie() # gets inseeid Siren__c = request.args.get('Siren__c') data = postgres.__getAccountBySiren(Siren__c) kafka_utils.sendToKafka_EDF(ujson.dumps(data)) return utils.returnResponse(ujson.dumps(data), 200, cookie, cookie_exists) except Exception as e: import traceback traceback.print_exc() cookie, cookie_exists = utils.getCookie() return utils.returnResponse("An error occured, check logDNA for more information", 403, cookie, cookie_exists)
def sendMessage(data, queue): # send a message global channel try: __checkQueue() channel.basic_publish( exchange='', routing_key=queue, body=data, properties=pika.BasicProperties( delivery_mode=2, # make message persistent )) logger.debug("[x] sent") except pika.exceptions.ConnectionClosed as e: try: logger.error( "Error with Rabbit MQ Connection. Trying to reinit it") init() channel.basic_publish( exchange='', routing_key=queue, body=data, properties=pika.BasicProperties( delivery_mode=2, # make message persistent )) except Exception as e: init() _retrySendData(data, queue) except Exception as e: init() _retrySendData(data, queue)
def badges(): try: if (request.method == 'POST'): logger.error(utils.get_debug_all(request)) # gets all the data required to save the badge object guest_id = request.args.get('guest_id') guest_firstname = request.args.get('guest_firstname') guest_lastname = request.args.get('guest_lastname') guest_company = request.args.get('guest_company') host_firstname = request.args.get('host_firstname') host_lastname = request.args.get('host_lastname') picture_url = request.args.get('picture_url') # id is auto generated uid = uuid.uuid4().__str__() badge_status = 'ACTIVE' badge_url = APPURL + uid # status is set to default -> INACTIVE (status are inactive / active ) # url will be calculated # gets new city if (guest_id == '' or guest_id == None): return utils.returnResponse("Please provide a guest_id", 403, None, None) # check if siren__c exits postgres.__insertBadge(uid, guest_id, guest_firstname, guest_lastname, guest_company, host_firstname, host_lastname, badge_status, badge_url, picture_url) # generates now the data #data = render_template(BADGE_DATA, GuestFirstname=guest_firstname, #GuestLastname = guest_lastname, GuestCompany=guest_company, HostFirstname=host_firstname, HostLastname=host_lastname, #ProfilePicture=picture_url, QRCode="") return "{'Result':'Ok'}" elif (request.method == 'GET'): logger.error(utils.get_debug_all(request)) cookie, cookie_exists = utils.getCookie() sqlRequest = sqlRequest = "select * from public.badge" attributes = None if ('badge_id' in request.args): sqlRequest += " where id = %(badge_id)s" attributes = {"badge_id": request.args.get('badge_id')} data = postgres.__execRequest(sqlRequest, attributes) return utils.returnResponse(ujson.dumps(data), 200, cookie, cookie_exists) except Exception as e: import traceback traceback.print_exc() cookie, cookie_exists = utils.getCookie() return utils.returnResponse( "An error occured, check logDNA for more information", 403, cookie, cookie_exists)
def error(): if (postgres.__checkHerokuLogsTable()): postgres.__saveLogEntry(request) logger.debug(utils.get_debug_all(request)) logger.error("Generating Error") error_code = 500 if ('error_code' in request.args): error_code = int(request.args['error_code']) return "Error !!!!!!", error_code
def error(): cookie, cookie_exists = utils.getCookie() if (postgres.__checkHerokuLogsTable()): postgres.__saveLogEntry(request, cookie) logger.debug(utils.get_debug_all(request)) logger.error("Generating Error") error_code = 500 if ('error_code' in request.args): error_code = int(request.args['error_code']) return utils.returnResponse("Error !! ", error_code, cookie, cookie_exists)
def checkAuthorization(request): if ("Authorization" not in request.headers): logger.error("Authorization code is not in headers") return False else: authorizationCode = request.headers['Authorization'] #base decode authorizationCodeB64 = authorizationCode.split(" ")[1] logger.info( "Authorization Code in B64={}".format(authorizationCodeB64)) logger.info("Authorization Code decoded={}".format( base64.b64decode(authorizationCodeB64))) return True
def make_authorization_url(): # Generate a random string for the state parameter # Save it for use later to prevent xsrf attacks from uuid import uuid4 state = str(uuid4()) save_created_state(state) params = { "client_id": APP_CLIENT_ID, "response_type": "code", "state": state, "redirect_uri": REDIRECT_URI_CODE, "scope": "full refresh_token" } import urllib url = SF_AUTHORIZE_TOKEN_URL + urllib.parse.urlencode(params) logger.error(url) return url
def sfconnectedapp(): logger.error(utils.get_debug_all(request)) error = request.args.get('error', '') if error: return "Error: " + error state = request.args.get('state', '') if not is_valid_state(state): # Uh-oh, this request wasn't started by us! abort(403) code = request.args.get('code') print(code) data = { 'client_id': APP_CLIENT_ID, 'client_secret': APP_CLIENT_SECRET, "redirect_uri": REDIRECT_URI_CODE, "code": code, 'grant_type': 'authorization_code' } headers = {'content-type': 'application/x-www-form-urlencoded'} req = requests.post(SF_REQUEST_TOKEN_URL, data=data, headers=headers) response = req.json() logger.error(response) #result = requests.post(url=url, data=data) access_token = response['access_token'] instance_url = response['instance_url'] url = instance_url + API_URL headers = { 'Authorization': "Bearer " + access_token, "X-Prettylogger.debug": "1" } result = requests.get(url, headers=headers) logger.info("Result Code : {}".format(result.status_code)) logger.info("Header : {}".format(result.headers)) logger.info("Content : {}".format(result.json())) #now stores everything in redis rediscache.__delCache(state) cookie, cookie_exists = utils.getCookie() key = {'cookie': cookie} rediscache.__setCache(key, ujson.dumps(response), 3600) return "got a code! %s" % response
def _retrySendData(data, queue): global channel, CLOUDAMQP_QUEUE try: # reinit __checkQueue() channel.basic_publish( exchange='', routing_key=queue, body=data, properties=pika.BasicProperties( delivery_mode=2, # make message persistent )) #logger.debug("[x] sent") except Exception as e: import traceback traceback.print_exc() logger.error(e.__str__()) logger.error( "{} - ERROR while trying to connect to RabbitMQ. Data are lost ! #FIXME #TODO - {}" .format(datetime.now(), e.__str__()))
def FACE_API_CALLBACK(ch, method, properties, body): try: # transforms body into dict body_dict = ujson.loads(body) logger.info(body_dict) logger.info(" [x] Received id=%r" % (body_dict['id'])) # gets the id of the image to retrieve in Redis image_id = body_dict['id'] if (body_dict['UPLOAD_IN_REDIS'] == True): image_binary_data = rediscache.__getCache(image_id) # write binary data into a file logger.debug("Writing file to disk") localfilename = '%s/%s' % (PATH_TO_TEST_IMAGES_DIR, "/rab_" + image_id + ".jpg") remotefilename = image_id + ".jpg" file = open(localfilename, "wb") file.write(image_binary_data) file.close() # sends data to AWS logger.debug("Starting AWS Upload") awsFilename = aws.uploadData(localfilename, remotefilename) logger.debug("uploaded file to amazon : {}".format(awsFilename)) # deletes from redis rediscache.__delCache(image_id) # deletes local file os.remove(localfilename) else: awsFilename = body_dict['remote_url'] # now detection !! logger.debug("Starting Face API") result, code = faceapi.face_http(awsFilename) logger.debug("Face API Result : {}".format(result)) if (code != 200): logger.error("Can't treat entry.") return else: img_width = body_dict['image_width'] img_height = body_dict["image_height"] # now treats each result for entry in result: try: personid = uuid.uuid4().__str__() data = { 'MediaId__c': image_id, 'image__c': awsFilename, 'UserAgent__c': body_dict['user-agent'], 'URL__c': body_dict['url'], 'Name': personid, 'PersonId__c': personid, 'Gender__c': entry['faceAttributes']['gender'], 'Age__c': int(entry['faceAttributes']['age']), 'Smile_value__c': entry['faceAttributes']['smile'], 'eyemakeup__c': entry['faceAttributes']['makeup']['eyeMakeup'], 'lipmakeup__c': entry['faceAttributes']['makeup']['lipMakeup'], 'Emotion_Value__c': 0.000 } # now let's treat things in the right order .. floatToBool(entry['faceAttributes'], 'smile', data, 'Smile__c', 0.5) stringToBool(entry['faceAttributes'], 'glasses', data, 'Glasses__c', ["ReadingGlasses"]) floatToBool(entry['faceAttributes']['hair'], 'bald', data, 'Bald__c', 0.5) # face square data['ImageWidth__c'] = img_width data['ImageHeight__c'] = img_height data['FaceTop__c'] = int( (entry['faceRectangle']['top'] / img_height) * 100) data['FaceLeft__c'] = int( (entry['faceRectangle']['left'] / img_width) * 100) data['FaceWidth__c'] = int( (entry['faceRectangle']['width'] / img_width) * 100) data['FaceHeight__c'] = int( (entry['faceRectangle']['height'] / img_height) * 100) if (entry['faceAttributes']['hair']['bald'] > 0.49): data['Hair__c'] = "bald" data['Haircolor__c'] = '' if ('hairColor' in entry['faceAttributes']['hair']): if (len(entry['faceAttributes']['hair']['hairColor']) >= 1): data['Haircolor__c'] = entry['faceAttributes'][ 'hair']['hairColor'][0]['color'] data['Hair__c'] = entry['faceAttributes']['hair'][ 'hairColor'][0]['color'] else: data['Hair__c'] = "bald" else: data['Hair__c'] = "bald" FacialHair = "None" if (entry['faceAttributes']['facialHair']['moustache'] > 0.49): FacialHair = 'moustache' if (entry['faceAttributes']['facialHair']['beard'] > 0.49): if (FacialHair != "None"): FacialHair += ' or beard' else: FacialHair += 'beard' data['FacialHair__c'] = FacialHair data['Emotion__c'] = "neutral" floatToBool(entry['faceAttributes']['facialHair'], 'moustache', data, 'Moustache__c', 0.5) floatToBool(entry['faceAttributes']['facialHair'], 'beard', data, 'Beard__c', 0.5) floatToBool(entry['faceAttributes']['emotion'], 'anger', data, 'Anger__c', 0.01) floatToBool(entry['faceAttributes']['emotion'], 'contempt', data, 'Contempt__c', 0.01) floatToBool(entry['faceAttributes']['emotion'], 'disgust', data, 'Disgust__c', 0.01) floatToBool(entry['faceAttributes']['emotion'], 'fear', data, 'Fear__c', 0.01) floatToBool(entry['faceAttributes']['emotion'], 'happiness', data, 'Happiness__c', 0.01) floatToBool(entry['faceAttributes']['emotion'], 'neutral', data, 'Neutral__c', 0.01) floatToBool(entry['faceAttributes']['emotion'], 'sadness', data, 'Sadness__c', 0.01) floatToBool(entry['faceAttributes']['emotion'], 'surprise', data, 'Surprise__c', 0.01) data['Emotion_Value__c'] = 0.0 data['Emotion__c'] = 'neutral' floatToString(entry['faceAttributes']['emotion'], 'anger', data['Emotion_Value__c'], data, 'Emotion__c', 'Emotion_Value__c') floatToString(entry['faceAttributes']['emotion'], 'contempt', data['Emotion_Value__c'], data, 'Emotion__c', 'Emotion_Value__c') floatToString(entry['faceAttributes']['emotion'], 'disgust', data['Emotion_Value__c'], data, 'Emotion__c', 'Emotion_Value__c') floatToString(entry['faceAttributes']['emotion'], 'fear', data['Emotion_Value__c'], data, 'Emotion__c', 'Emotion_Value__c') floatToString(entry['faceAttributes']['emotion'], 'happiness', data['Emotion_Value__c'], data, 'Emotion__c', 'Emotion_Value__c') floatToString(entry['faceAttributes']['emotion'], 'neutral', data['Emotion_Value__c'], data, 'Emotion__c', 'Emotion_Value__c') floatToString(entry['faceAttributes']['emotion'], 'sadness', data['Emotion_Value__c'], data, 'Emotion__c', 'Emotion_Value__c') floatToString(entry['faceAttributes']['emotion'], 'surprise', data['Emotion_Value__c'], data, 'Emotion__c', 'Emotion_Value__c') data['Description__c'] = ujson.dumps(entry) logger.debug(data) postgres.__saveImageAnalysisEntry(data) except Exception as e: import traceback traceback.print_exc() logger.error("Error treating entry, going to the next") logger.info(" [x] Finished id=%r" % (body_dict['id'])) except Exception as e: import traceback traceback.print_exc()