def make_response(data, marshal_table, cursors=None): if util.is_iterable(data): response = { 'status': 'success', 'count': len(data), 'now': datetime.utcnow().isoformat(), 'result': map(lambda l: flask_restful.marshal(l, marshal_table), data), } if cursors: if isinstance(cursors, dict): if cursors.get('next'): response['next_cursor'] = cursors['next'] response['next_url'] = util.generate_next_url(cursors['next']) if cursors.get('prev'): response['prev_cursor'] = cursors['prev'] response['prev_url'] = util.generate_next_url(cursors['prev']) else: response['next_cursor'] = cursors response['next_url'] = util.generate_next_url(cursors) return util.jsonpify(response) return util.jsonpify({ 'status': 'success', 'now': datetime.utcnow().isoformat(), 'result': flask_restful.marshal(data, marshal_table), })
def add_blob_content(): response_object = { 'status': 'success', 'now': datetime.utcnow().isoformat(), 'filelink': None, } folder = Folder.retrieve_by_id(3001) if not folder: response_object['status'] = 'failure' return util.jsonpify(response_object) upload_files = get_uploads(flask.request, 'file') if len(upload_files): blob_info = upload_files[0] if blob_info.size: f = File.create( blob_info.key(), size=blob_info.size, filename=os.path.basename(blob_info.filename.replace('\\','/')), content_type=blob_info.content_type) f.put() if f.get_cached_url(): folder.files.append(f.key) folder.put() response_object['filelink'] = f.get_cached_url() else: blob_info.delete() return util.jsonpify(response_object)
def make_response(data, marshal_table, cursors=None): if util.is_iterable(data): response = { 'status': 'success', 'count': len(data), 'now': datetime.utcnow().isoformat(), 'result': [flask_restful.marshal(d, marshal_table) for d in data], } if cursors: if isinstance(cursors, dict): if cursors.get('next'): response['next_cursor'] = cursors['next'] response['next_url'] = util.generate_next_url( cursors['next']) if cursors.get('prev'): response['prev_cursor'] = cursors['prev'] response['prev_url'] = util.generate_next_url( cursors['prev']) else: response['next_cursor'] = cursors response['next_url'] = util.generate_next_url(cursors) return util.jsonpify(response) return util.jsonpify({ 'status': 'success', 'now': datetime.utcnow().isoformat(), 'result': flask_restful.marshal(data, marshal_table), })
def error_handler(e): try: e.code except: class e(object): code = 500 name = "Internal Server Error" if flask.request.path.startswith("/_s/"): return ( util.jsonpify( { "status": "error", "error_code": e.code, "error_name": e.name.lower().replace(" ", "_"), "error_message": e.name, } ), e.code, ) return ( flask.render_template( "error.html", title="Error %d (%s)!!1" % (e.code, e.name), html_class="error-page", error=e ), e.code, )
def get(self, in_query): index = search.Index('spots') query_dict = parse_query(in_query) if query_dict[ 'query'] == '' and 'lat' not in query_dict and 'lon' not in query_dict: search_results = index.get_range(start_id="0", limit=10) else: # search_results = index.search(query_dict['query']) search_query = search.Query( query_string=query_dict['query'].strip(), options=get_query_options(query_dict)) search_results = index.search(search_query) features = [] for doc in search_results: if (doc.field('place').value.longitude != 0.0 and doc.field('place').value.latitude != 0.0): features.append(build_feature_dict(doc)) # features = in_query if len(features) == 0 else features dict = {"type": "FeatureCollection", "features": features} return util.jsonpify(dict) # curl http://127.0.0.1:8080/api/v1/post/a # AIzaSyAbcMGMULgp5l0Trav2G3OseIrNGIxHDZk # curl "http://127.0.0.1:8080/api/v1/post/a&-73.9858118&40.7701926" # curl "http://localhost:3000/api/v1/post/*&40.770696199999996&-73.9858118"
def error_handler(e): logging.exception(e) try: e.code except AttributeError: e.code = 500 e.name = "Internal Server Error" if flask.request.path.startswith("/_s/"): return ( util.jsonpify( { "status": "error", "error_code": e.code, "error_name": util.slugify(e.name), "error_message": e.name, "error_class": e.__class__.__name__, } ), e.code, ) return ( flask.render_template( "error.html", title="Error %d (%s)!!1" % (e.code, e.name), html_class="error-page", error=e ), e.code, )
def handle_error(e): if not e: e = {} else: logging.exception(e) try: e.code except AttributeError: e.code = 500 e.name = e.description = 'Internal Server Error' result = { 'status': 'error', 'error_code': e.code, 'error_name': util.slugify(e.name), 'error_message': e.name, 'error_class': e.__class__.__name__, 'description': e.description, 'data': None, 'validations': None } if hasattr(e, 'data'): result['data'] = e.data if hasattr(e, 'validations'): result['validations'] = e.validations return util.jsonpify(result), e.code
def make_response(data, marshal_table, next_cursor=None): if util.is_iterable(data): response = { 'status': 'success', 'count': len(data), 'now': datetime.utcnow().isoformat(), 'result': map(lambda l: restful.marshal(l, marshal_table), data), } if next_cursor: response['next_cursor'] = next_cursor response['next_url'] = util.generate_next_url(next_cursor) return util.jsonpify(response) return util.jsonpify({ 'status': 'success', 'now': datetime.utcnow().isoformat(), 'result': restful.marshal(data, marshal_table), })
def make_response(data, marshal_table, next_cursor=None): if util.is_iterable(data): response = { 'status': 'success', 'count': len(data), 'now': datetime.utcnow().isoformat(), 'result': map(lambda l: restful.marshal(l, marshal_table), data), } if next_cursor: response['next_cursor'] = next_cursor response['next_url'] = util.generate_next_url(next_cursor) return util.jsonpify(response) return util.jsonpify({ 'status': 'success', 'now': datetime.utcnow().isoformat(), 'result': restful.marshal(data, marshal_table), })
def put(self, post_key): post_db = model.Post.get_by_id(post_key) user_db = auth.current_user_key().get() star = model.Star(user_key=user_db.key, post_key=post_db.key) star_id = star.put() return util.jsonpify({ 'post_key': str(post_db.key.id), 'user_key': str(user_db.key.id), 'star': str(star_id) })
def put(self, recommender_key): recommender_db = model.Recommender.get_by_id(recommender_key) user_db = auth.current_user_key().get() following = model.Following(user_key=user_db.key, recommender_key=recommender_db.key) following_id = following.put() return util.jsonpify({ 'post_key': str(recommender_db.key.id), 'user_key': str(user_db.key.id), 'following_id': str(following_id) })
def get(self): parser = reqparse.RequestParser() parser.add_argument('hub.mode') parser.add_argument('hub.challenge') parser.add_argument('hub.verify_token') args = parser.parse_args() logging.debug(args) if args['hub.mode'] == 'subscribe': if args['hub.verify_token'] == config.FACEBOOK_WEBHOOK_VERIFY_TOKEN: return int(args['hub.challenge']) result = {} return util.jsonpify(result)
def get(self): parser = reqparse.RequestParser() parser.add_argument('hub.mode') parser.add_argument('hub.challenge') parser.add_argument('hub.verify_token') args = parser.parse_args() logging.debug(args) if args['hub.mode'] == 'subscribe': if args['hub.verify_token'] == config.FACEBOOK_WEBHOOK_VERIFY_TOKEN: return int(args['hub.challenge']) result = {} return util.jsonpify(result)
def handle_error(e): logging.exception(e) try: e.code except AttributeError: e.code = 500 e.name = e.description = 'Internal Server Error' return util.jsonpify({ 'status': 'error', 'error_code': e.code, 'error_name': util.slugify(e.name), 'error_message': e.name, 'error_class': e.__class__.__name__, 'description': e.description, }), e.code
def handle_error(e): logging.exception(e) try: e.code except AttributeError: e.code = 500 e.name = e.description = 'Internal Server Error' return util.jsonpify({ 'status': 'error', 'error_code': e.code, 'error_name': util.slugify(e.name), 'error_message': e.name, 'error_class': e.__class__.__name__, 'description': e.description, }), e.code
def handle_error(e): logging.exception(e) return util.jsonpify({ 'status': 'error', 'error_code': getattr(e, 'code', 500), 'error_name': util.slugify(getattr(e, 'name', 'Internal Server Error')), 'error_message': getattr(e, 'name', 'Internal Server Error'), 'error_class': e.__class__.__name__, 'description': getattr(e, 'description', 'Internal Server Error'), }), getattr(e, 'code', 500)
def get(self): args = parser.parse({ 'fsym': wf.Str(), 'tsym': wf.Str(), 'ts': wf.Str(), }) result = urlfetch.fetch( 'https://min-api.cryptocompare.com/data/pricehistorical?fsym=%s&tsyms=%s&ts=%s&extraParams=%s' % (args['fsym'], args['tsym'], args['ts'], config.APPLICATION_NAME)) if result.status_code == 200: content = json.loads(result.content) try: rate = content[args['fsym']][args['tsym']] return util.jsonpify( {'%s%s' % (args['fsym'], args['tsym']): rate}) except: flask.abort(404) else: flask.abort(result.status_code)
def error_handler(e): try: e.code except AttributeError as e: e.code = 500 e.name = 'Internal Server Error' if flask.request.path.startswith('/_s/'): return util.jsonpify({ 'status': 'error', 'error_code': e.code, 'error_name': e.name.lower().replace(' ', '_'), 'error_message': e.name, }), e.code return flask.render_template( 'error.html', title='Error %d (%s)!!1' % (e.code, e.name), html_class='error-page', error=e, ), e.code
def error_handler(e): try: e.code except: class e(object): code = 500 name = 'Internal Server Error' if flask.request.path.startswith('/_s/'): return util.jsonpify({ 'status': 'error', 'error_code': e.code, 'error_name': e.name.lower().replace(' ', '_'), 'error_message': e.name, }), e.code return flask.render_template( 'error.html', title='Error %d (%s)!!1' % (e.code, e.name), html_class='error-page', error=e, ), e.code
def error_handler(e): logging.exception(e) try: e.code except AttributeError: e.code = 500 e.name = 'Internal Server Error' if flask.request.path.startswith('/_s/'): return util.jsonpify({ 'status': 'error', 'error_code': e.code, 'error_name': util.slugify(e.name), 'error_message': e.name, 'error_class': e.__class__.__name__, }), e.code return flask.render_template( 'error.html', title='Error %d (%s)!!1' % (e.code, e.name), html_class='error-page', error=e, ), e.code
def post(self): obj = request.get_json() if obj: for entry in obj['entry']: fb_messaging = entry['messaging'] fb_page_id = entry['id'] for fb_obj in fb_messaging: fb_sender = fb_obj['sender'] fb_recipient = fb_obj['recipient'] if 'message' in fb_obj: fb_content = fb_obj['message'] fb_timestamp = fb_obj['timestamp'] fb_mid = fb_content['mid'] fb_seq = fb_content['seq'] if'attachments' in fb_content: for attachment in fb_content['attachments']: atype = attachment['type'] if atype is 'image' or atype is 'video' or atype is 'audio': logging.debug( '"%s" "%s" "%s" "%s" "%s" "%s" "%s" "%s" "%s"' % ( 'incoming', fb_timestamp, fb_page_id, fb_sender['id'], fb_recipient['id'], attachment['type'], attachment['payload']['url'], fb_seq, fb_mid ) ) elif atype is 'location': logging.debug( '"%s" "%s" "%s" "%s" "%s" "%s" "%s" "%s" "%s"' % ( 'incoming', fb_timestamp, fb_page_id, fb_sender['id'], fb_recipient['id'], attachment['type'], attachment['payload'], fb_seq, fb_mid ) ) else: logging.debug( '"%s" "%s" "%s" "%s" "%s" "%s" "%s" "%s" "%s"' % ( 'incoming', fb_timestamp, fb_page_id, fb_sender['id'], fb_recipient['id'], attachment['type'], attachment['payload'], fb_seq, fb_mid ) ) else: logging.debug( '"%s" "%s" "%s" "%s" "%s" "%s" "%s" "%s" "%s"' % ( 'incoming', fb_timestamp, fb_page_id, fb_sender['id'], fb_recipient['id'], 'text', fb_content['text'], fb_seq, fb_mid ) ) if 'show example text' in fb_content['text']: example_message_text( fb_sender, 'This is an example of a message with text only.' ) example_message_text( fb_sender, 'The last thing you said was: "%s"' % fb_content['text'] ) if 'show example image' in fb_content['text']: example_message_text( fb_sender, 'This is an example of a message with an image only.' ) example_message_image( fb_sender, 'http://petersapparel.parseapp.com/img/item100-thumb.png' ) elif 'delivery' in fb_obj: fb_delivery = fb_obj['delivery'] fb_watermark = fb_delivery['watermark'] fb_seq = fb_delivery['seq'] if 'mids' in fb_delivery and len(fb_delivery['mids']) > 0: for fb_mid in fb_delivery['mids']: logging.debug( '"%s" "%s" "%s" "%s" "%s" "%s" "%s"' % ( 'delivery', fb_page_id, fb_sender['id'], fb_recipient['id'], fb_watermark, fb_seq, fb_mid ) ) else: logging.debug( '"%s" "%s" "%s" "%s" "%s" "%s"' % ( 'delivery', fb_page_id, fb_sender['id'], fb_recipient['id'], fb_watermark, fb_seq ) ) else: logging.debug( '"%s" "%s" "%s" "%s" "%s"' % ( 'incoming', fb_page_id, fb_sender['id'], fb_recipient['id'], fb_obj, ) ) result = {} return util.jsonpify(result)
def make_object_response(obj): return util.jsonpify({ 'status': 'success', 'now': datetime.utcnow().isoformat(), 'result': obj })
def post(self): cu = nlpfunc.Checkup() obj = request.get_json() if obj: for entry in obj['entry']: fb_messaging = entry['messaging'] fb_page_id = entry['id'] for fb_obj in fb_messaging: fb_sender = fb_obj['sender'] fb_recipient = fb_obj['recipient'] if 'message' in fb_obj: fb_content = fb_obj['message'] logging.debug(fb_content['text']) #logging.debug(fb_sender['id']) fb_timestamp = fb_obj['timestamp'] fb_mid = fb_content['mid'] fb_seq = fb_content['seq'] if 'attachments' in fb_content: for attachment in fb_content['attachments']: atype = attachment['type'] if atype is 'image' or atype is 'video' or atype is 'audio': logging.debug( '"%s" "%s" "%s" "%s" "%s" "%s" "%s" "%s" "%s"' % ('incoming', fb_timestamp, fb_page_id, fb_sender['id'], fb_recipient['id'], attachment['type'], attachment['payload']['url'], fb_seq, fb_mid)) elif atype is 'location': logging.debug( '"%s" "%s" "%s" "%s" "%s" "%s" "%s" "%s" "%s"' % ('incoming', fb_timestamp, fb_page_id, fb_sender['id'], fb_recipient['id'], attachment['type'], attachment['payload'], fb_seq, fb_mid)) else: logging.debug( '"%s" "%s" "%s" "%s" "%s" "%s" "%s" "%s" "%s"' % ('incoming', fb_timestamp, fb_page_id, fb_sender['id'], fb_recipient['id'], attachment['type'], attachment['payload'], fb_seq, fb_mid)) elif 'text' in fb_content: logging.debug( '"%s" "%s" "%s" "%s" "%s" "%s" "%s" "%s" "%s"' % ('Facebook incoming message', fb_timestamp, fb_page_id, fb_sender['id'], fb_recipient['id'], 'text', fb_content['text'], fb_seq, fb_mid)) # connect to fb graph api to get details of message sender url = "https://graph.facebook.com/v2.6/" + str( fb_sender['id'] ) + "?fields=first_name,last_name,profile_pic,locale,timezone,gender&access_token=" + config.FACEBOOK_PAGE_ACCESS_TOKEN response = urllib.urlopen(url) fb_data = json.loads(response.read()) fn = '<fn>' ln = '<ln>' if 'first_name' in fb_data: fn = fb_data["first_name"] ln = fb_data["last_name"] processed = 0 #to check if a sample text code triggered if 'show example text' in fb_content['text']: processed = 1 # yes sample text code triggered example_message_text( fb_sender, 'This is an example of a message with text only.' ) example_message_text( fb_sender, 'The last thing you said was: "%s"' % fb_content['text']) if 'button' in fb_content['text']: processed = 1 example_message_button( fb_sender, "It looks like you said: I have a tooth ache", "Is that correct?", "I have tooth ache") if 'show example image' in fb_content['text']: processed = 1 example_message_text( fb_sender, 'This is an example of a message with an image only.' ) example_message_image( fb_sender, 'https://jjmdev-a.appspot.com/client/small_utw.png' ) if 'show video' in fb_content['text']: processed = 1 example_message_text( fb_sender, 'This is an example of a message with a HSE video.' ) example_message_video( fb_sender, '<div style="position:relative;height:0;padding-bottom:56.25%"><iframe src="https://www.youtube.com/embed/Kn7I-Vp-TJI?ecver=2" style="position:absolute;width:100%;height:100%;left:0" width="640" height="360" frameborder="0" allowfullscreen></iframe></div>' ) logging.debug(fb_content['text']) utw_reply = cu.classify(fb_content['text']) if processed == 0: # no sample text code triggered, ok proceed to use nlp functions if not utw_reply: example_message_image( fb_sender, 'https://jjmdev-a.appspot.com/client/small_icon_sad.png' ) example_message_text( fb_sender, 'I am sorry, "%s" there is no response for that statement in my lexicon! This is a proof of concept bot. Try asking general medical questions as per HSE website www.undertheweather.ie thankyou' % fn) else: example_message_image( fb_sender, 'https://jjmdev-a.appspot.com/client/small_utw.png' ) example_message_text( fb_sender, 'Hi "%s", so you said: "%s"' % (fn, utw_reply[0])) example_message_image( fb_sender, 'https://jjmdev-a.appspot.com/client/small_icon_scope.png' ) example_message_text( fb_sender, utw_reply[1]) example_message_image( fb_sender, 'https://jjmdev-a.appspot.com/client/small_icon_pill.png' ) example_message_text( fb_sender, utw_reply[2]) elif 'delivery' in fb_obj: fb_delivery = fb_obj['delivery'] fb_watermark = fb_delivery['watermark'] fb_seq = fb_delivery['seq'] if 'mids' in fb_delivery and len( fb_delivery['mids']) > 0: for fb_mid in fb_delivery['mids']: logging.debug( '"%s" "%s" "%s" "%s" "%s" "%s" "%s"' % ('delivery', fb_page_id, fb_sender['id'], fb_recipient['id'], fb_watermark, fb_seq, fb_mid)) else: logging.debug( '"%s" "%s" "%s" "%s" "%s" "%s"' % ('delivery', fb_page_id, fb_sender['id'], fb_recipient['id'], fb_watermark, fb_seq)) else: logging.debug('"%s" "%s" "%s" "%s" "%s"' % ( 'incoming', fb_page_id, fb_sender['id'], fb_recipient['id'], fb_obj, )) result = {} return util.jsonpify(result)