# Path: data_model.py
# class StopLocation(GeoModel):
#     stopID       = db.StringProperty()
#     intersection = db.StringProperty()
#     direction    = db.StringProperty()
#     description  = db.StringProperty(multiline=True)
# 
# Path: data_model.py
# class StopLocationLoader(db.Model):
#     stopID       = db.StringProperty()
#     name         = db.StringProperty()
#     description  = db.StringProperty(multiline=True)
#     lat          = db.FloatProperty()
#     lon          = db.FloatProperty()
#     direction    = db.StringProperty()
# 
# Path: data_model.py
# class RouteListing(db.Model):
#     route        = db.StringProperty()
#     routeCode    = db.StringProperty()
#     direction    = db.StringProperty()
#     stopID       = db.StringProperty()
#     stopCode     = db.StringProperty()
#     scheduleURL  = db.StringProperty(indexed=False)
#     stopLocation = db.ReferenceProperty(StopLocation,collection_name="stops")
# 
# Path: data_model.py
# class RouteListingLoader(db.Model):
#     routeID       = db.StringProperty()
#     routeCode     = db.StringProperty()
#     direction     = db.StringProperty()
#     directionCode = db.StringProperty()
#     stopID        = db.StringProperty()
#     stopCode      = db.StringProperty()
# 
# Path: data_model.py
# class DestinationListing(db.Model):
#     id    = db.StringProperty()
#     label = db.StringProperty()


import logging
from google.appengine.api.taskqueue import Task
from google.appengine.ext import webapp
from google.appengine.ext import db
from google.appengine.ext.db import GeoPt
from google.appengine.ext.db import TransactionFailedError
from google.appengine.ext.db import Timeout
from google.appengine.runtime import DeadlineExceededError
from data_model import StopLocation
from data_model import StopLocationLoader
from data_model import RouteListing
from data_model import RouteListingLoader
from data_model import DestinationListing



CRAWL_URLBASE = "http://webwatch.cityofmadison.com/tmwebwatch/LiveADADepartureTimes"

#
# a collection of handlers that will transform RouteListingLoader entities
# into routes and destinations
#
# note that order matters. the Stop transformation has to happen first.
#
class RouteTransformationStart(webapp.RequestHandler):
    def get(self) :
        # shove a task in the queue because we need more time then
        # we may be able to get in the browser
        for route in range(1, 100):
            task = Task(url="/gtfs/port/routes/task",params={'route':route})
            task.add('crawler')
        self.response.out.write('done. spawned a task to go do the route transformations')

## end RouteTransformationStart

class RouteTransformationTask(webapp.RequestHandler):
    def post(self):
        try:
            routeID = self.request.get('route')
            if len(routeID) == 1:
                routeID = '0' + routeID


# Path: data_model.py
# class StopLocation(GeoModel):
#     stopID       = db.StringProperty()
#     intersection = db.StringProperty()
#     direction    = db.StringProperty()
#     description  = db.StringProperty(multiline=True)
# 
# Path: data_model.py
# class StopLocationLoader(db.Model):
#     stopID       = db.StringProperty()
#     name         = db.StringProperty()
#     description  = db.StringProperty(multiline=True)
#     lat          = db.FloatProperty()
#     lon          = db.FloatProperty()
#     direction    = db.StringProperty()
# 
# Path: data_model.py
# class RouteListing(db.Model):
#     route        = db.StringProperty()
#     routeCode    = db.StringProperty()
#     direction    = db.StringProperty()
#     stopID       = db.StringProperty()
#     stopCode     = db.StringProperty()
#     scheduleURL  = db.StringProperty(indexed=False)
#     stopLocation = db.ReferenceProperty(StopLocation,collection_name="stops")
# 
# Path: data_model.py
# class RouteListingLoader(db.Model):
#     routeID       = db.StringProperty()
#     routeCode     = db.StringProperty()
#     direction     = db.StringProperty()
#     directionCode = db.StringProperty()
#     stopID        = db.StringProperty()
#     stopCode      = db.StringProperty()
# 
# Path: data_model.py
# class DestinationListing(db.Model):
#     id    = db.StringProperty()
#     label = db.StringProperty()


# Path: crawl/load_gtfs.py
import logging
from google.appengine.api.taskqueue import Task
from google.appengine.ext import webapp
from google.appengine.ext import db
from google.appengine.ext.db import GeoPt
from google.appengine.ext.db import TransactionFailedError
from google.appengine.ext.db import Timeout
from google.appengine.runtime import DeadlineExceededError
from data_model import StopLocation
from data_model import StopLocationLoader
from data_model import RouteListing
from data_model import RouteListingLoader
from data_model import DestinationListing




CRAWL_URLBASE = "http://webwatch.cityofmadison.com/tmwebwatch/LiveADADepartureTimes"

#
# a collection of handlers that will transform RouteListingLoader entities
# into routes and destinations
#
# note that order matters. the Stop transformation has to happen first.
#
class RouteTransformationStart(webapp.RequestHandler):
    def get(self) :
        # shove a task in the queue because we need more time then
        # we may be able to get in the browser
        for route in range(1, 100):
            task = Task(url="/gtfs/port/routes/task",params={'route':route})
            task.add('crawler')
        self.response.out.write('done. spawned a task to go do the route transformations')

## end RouteTransformationStart

class RouteTransformationTask(webapp.RequestHandler):
    def post(self):
        try:
            routeID = self.request.get('route')
            if len(routeID) == 1:
                routeID = '0' + routeID


            q = RouteListingLoader.all()