#!/usr/bin/env python
#
# Copyright 2015 Google Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
import helpers
import json
import logging
import models
import webapp2

################################################################################

class Index(webapp2.RequestHandler):
    def get(self):
        self.response.out.write('')

################################################################################

class GoUrl(webapp2.RequestHandler):
    def get(self):
        url = self.request.get('url')
        url = url.encode('ascii', 'ignore')
        self.redirect(url)

################################################################################

class GooglRedirect(webapp2.RequestHandler):
    def get(self, path):
        try:
            rssi = float(self.request.headers['X-PhysicalWeb-Rssi'])
            txpower = float(self.request.headers['X-PhysicalWeb-TxPower'])
            path_loss = txpower - rssi;
        except:
            path_loss = None

        logging.info('GoogleRedirect with rssi:{0} and txpower:{1}'.format(rssi, txpower))

        if path_loss > 40:
            self.response.set_status(204)
            return

        self.redirect('http://goo.gl/{0}'.format(path))

################################################################################

class RefreshUrl(webapp2.RequestHandler):
    def post(self):
        url = self.request.get('url')
        helpers.RefreshUrl(url)

################################################################################

class ResolveScan(webapp2.RequestHandler):
    def post(self):
        input_data = self.request.body

        try:
            input_object = json.loads(input_data) # TODO: Data is not sanitised.
            objects = input_object.get('objects', [])
        except:
            objects = []

        metadata_output = helpers.BuildResponse(objects)
        output = {
          'metadata': metadata_output
        }
        self.response.headers['Content-Type'] = 'application/json'
        json_data = json.dumps(output);
        self.response.write(json_data)

################################################################################

class DemoMetadata(webapp2.RequestHandler):
    def get(self):
        objects = [
            {'url': 'http://www.caltrain.com/schedules/realtime/stations/mountainviewstation-mobile.html'},
            {'url': 'http://benfry.com/distellamap/'},
            {'url': 'http://en.wikipedia.org/wiki/Le_D%C3%A9jeuner_sur_l%E2%80%99herbe'},
            {'url': 'http://sfmoma.org'}
        ]
        metadata_output = helpers.BuildResponse(objects)
        output = {
          'metadata': metadata_output
        }
        self.response.headers['Content-Type'] = 'application/json'
        json_data = json.dumps(output);
        self.response.write(json_data)

################################################################################

app = webapp2.WSGIApplication([
    ('/', Index),
    ('/resolve-scan', ResolveScan),
    ('/refresh-url', RefreshUrl),
    ('/go', GoUrl),
    ('/googl/(.*)', GooglRedirect),
    ('/demo', DemoMetadata)
], debug=True)

@@ -1446,32 +1446,83 @@
         except:%0A
+            rssi = None%0A            txpower = None%0A
             path
@@ -1650,17 +1650,17 @@
 _loss %3E 
-4
+6
 0:%0A     

Add some much needed logging to help diagnose server issues