# Path: data_model.py
# class BusStopAggregation(db.Model):
#     dateAdded   = db.DateTimeProperty(auto_now_add=True)
#     routeID     = db.StringProperty(indexed=False)
#     stopID      = db.StringProperty(indexed=False)
#     destination = db.StringProperty(indexed=False)
#     arrivalTime = db.StringProperty(indexed=False)
#     time        = db.IntegerProperty()
#     text        = db.StringProperty(multiline=True,indexed=False)
#     sid         = db.StringProperty()
# 
# Path: api/v1/api_utils.py
# def validateDevKey(devKey):
# def conformStopID(stopID):
# def conformRouteID(routeID):
# def inthepast(time):
# def getLocalDatetime():
# def getLocalTimestamp():
# def computeCountdownMinutes(arrivalTime):
# def afterHours():
# def buildErrorResponse(error,description):
# def getDirectionLabel(directionID):
# def handle_500(request, response, exception):
# def recordDeveloperRequest(devKey,type,terms,ipaddr,error='success'):
# def serialize_entities(models):
# def deserialize_entities(data):
# def get_time_from_text(text_with_time, findPos=0):
# GETARRIVALS = "get arrivals"
# GETVEHICLES = "get vehicles"
# GETSTOPS = "get stops"
# GETROUTES = "get routes"
# GETNEARBYSTOPS = "get nearby stops"
# GETSTOPLOCATION = "get stop location"
# 
# Path: stats_and_maps/stats/stathat.py
# STATHAT_MEMCACHE_ERROR_COUNT = 'stathat_error_count'
# STATHAT_MEMCACHE_REQ_COUNT = 'stathat_req_count'
#         URL_BASE = 'https://api.stathat.com'
# class StatHat:
# class StatValueTaskHandler(webapp.RequestHandler):
# class StatFlushHandler(webapp.RequestHandler):
# class StatStopsCollectHandler(webapp.RequestHandler):
#         def http_post_async(self, path, data):
#         def http_post(self, path, data):
#         def post_value(self, user_key, stat_key, value, callback=None):
#         def post_count(self, user_key, stat_key, count, callback=None):
#         def ez_post_value(self, ezkey, stat_name, value):
#         def ez_post_count(self, ezkey, stat_name, count):
# def apiStatCount():
# def apiErrorCount():
# def apiTimeStat(stat_key,value):
# def noop():
#   def post(self):
#     def get(self):
#     def post(self):
# def main():


import logging
import webapp2
import config
from google.appengine.api import mail
from google.appengine.api import urlfetch
from google.appengine.api import memcache
from google.appengine.ext import db
from BeautifulSoup import BeautifulSoup
from data_model import BusStopAggregation
from api.v1 import api_utils
from stats_and_maps.stats import stathat
        #     <td><a class="adatime" title="11:12 am">11:12 am</a></td>
        #     <td><a class="adatext" title="NTP">NTP</a></td>
        #   </tr><tr>
        #     <td><p class="stopLabel">Scheduled time shown</p></td>
        #   </tr><tr>
        #     <td><a class="adatime" title="11:42 am">11:42 am</a></td>
        #     <td><a class="adatext" title="NTP">NTP</a></td>
        #   </tr><tr>
        #     <td><p class="stopLabel">Scheduled time shown</p></td>
        #   </tr><tr>
        #     <td><a class="adatime" title="12:12 pm">12:12 pm</a></td>
        #     <td><a class="adatext" title="NTP">NTP</a></td>
        #   </tr><tr>
        #     <td><p class="stopLabel">Scheduled time shown</p></td>
        #   </tr><tr>
        #     <td><a class="ada" title="Times last updated 10:54:01 AM 10/16/2016">Times last updated 10:54:01 AM 10/16/2016</a></td>
        #   </tr>
        # </tbody>
        soup = BeautifulSoup(result.content)
        for slot in soup.html.body.findAll("a","adatime"):
            arrival = slot.string
            direction = slot.parent.nextSibling.a.string

            # ... correct the transfer point acronym we broke in the title() call above
            direction = direction.replace('Tp','TP')

            # the original implementaiton leveraged the datastore to store and
            # ultimately sort the results when we got all of the routes back.
            # we'll continute to use the model definition, but never actually store
            # # the results in the datastore.

# Path: data_model.py
# class BusStopAggregation(db.Model):
#     dateAdded   = db.DateTimeProperty(auto_now_add=True)
#     routeID     = db.StringProperty(indexed=False)
#     stopID      = db.StringProperty(indexed=False)
#     destination = db.StringProperty(indexed=False)
#     arrivalTime = db.StringProperty(indexed=False)
#     time        = db.IntegerProperty()
#     text        = db.StringProperty(multiline=True,indexed=False)
#     sid         = db.StringProperty()
# 
# Path: api/v1/api_utils.py
# def validateDevKey(devKey):
# def conformStopID(stopID):
# def conformRouteID(routeID):
# def inthepast(time):
# def getLocalDatetime():
# def getLocalTimestamp():
# def computeCountdownMinutes(arrivalTime):
# def afterHours():
# def buildErrorResponse(error,description):
# def getDirectionLabel(directionID):
# def handle_500(request, response, exception):
# def recordDeveloperRequest(devKey,type,terms,ipaddr,error='success'):
# def serialize_entities(models):
# def deserialize_entities(data):
# def get_time_from_text(text_with_time, findPos=0):
# GETARRIVALS = "get arrivals"
# GETVEHICLES = "get vehicles"
# GETSTOPS = "get stops"
# GETROUTES = "get routes"
# GETNEARBYSTOPS = "get nearby stops"
# GETSTOPLOCATION = "get stop location"
# 
# Path: stats_and_maps/stats/stathat.py
# STATHAT_MEMCACHE_ERROR_COUNT = 'stathat_error_count'
# STATHAT_MEMCACHE_REQ_COUNT = 'stathat_req_count'
#         URL_BASE = 'https://api.stathat.com'
# class StatHat:
# class StatValueTaskHandler(webapp.RequestHandler):
# class StatFlushHandler(webapp.RequestHandler):
# class StatStopsCollectHandler(webapp.RequestHandler):
#         def http_post_async(self, path, data):
#         def http_post(self, path, data):
#         def post_value(self, user_key, stat_key, value, callback=None):
#         def post_count(self, user_key, stat_key, count, callback=None):
#         def ez_post_value(self, ezkey, stat_name, value):
#         def ez_post_count(self, ezkey, stat_name, count):
# def apiStatCount():
# def apiErrorCount():
# def apiTimeStat(stat_key,value):
# def noop():
#   def post(self):
#     def get(self):
#     def post(self):
# def main():


# Path: api/asynch.py
import logging
import webapp2
import config
from google.appengine.api import mail
from google.appengine.api import urlfetch
from google.appengine.api import memcache
from google.appengine.ext import db
from BeautifulSoup import BeautifulSoup
from data_model import BusStopAggregation
from api.v1 import api_utils
from stats_and_maps.stats import stathat

        #     <td><a class="adatime" title="11:12 am">11:12 am</a></td>
        #     <td><a class="adatext" title="NTP">NTP</a></td>
        #   </tr><tr>
        #     <td><p class="stopLabel">Scheduled time shown</p></td>
        #   </tr><tr>
        #     <td><a class="adatime" title="11:42 am">11:42 am</a></td>
        #     <td><a class="adatext" title="NTP">NTP</a></td>
        #   </tr><tr>
        #     <td><p class="stopLabel">Scheduled time shown</p></td>
        #   </tr><tr>
        #     <td><a class="adatime" title="12:12 pm">12:12 pm</a></td>
        #     <td><a class="adatext" title="NTP">NTP</a></td>
        #   </tr><tr>
        #     <td><p class="stopLabel">Scheduled time shown</p></td>
        #   </tr><tr>
        #     <td><a class="ada" title="Times last updated 10:54:01 AM 10/16/2016">Times last updated 10:54:01 AM 10/16/2016</a></td>
        #   </tr>
        # </tbody>
        soup = BeautifulSoup(result.content)
        for slot in soup.html.body.findAll("a","adatime"):
            arrival = slot.string
            direction = slot.parent.nextSibling.a.string

            # ... correct the transfer point acronym we broke in the title() call above
            direction = direction.replace('Tp','TP')

            # the original implementaiton leveraged the datastore to store and
            # ultimately sort the results when we got all of the routes back.
            # we'll continute to use the model definition, but never actually store
            # # the results in the datastore.

            stop = BusStopAggregation()