# Path: allbus/thebus/utilities/client.py
# def parse_xml_to_dict(response):
#     options = {
#         'strip_ns': True,
#         'strip_nl': True,
#         'pretty': True,
#         'strip_text': True
#     }
#     elem = ET.fromstring(response.read())
#     if hasattr(elem, 'getroot'):
#         elem = elem.getroot()
#     return elem_to_internal(elem, options)
# 
# Path: allbus/thebus/utilities/client.py
# class TheBusClient(object):
#     """ Thin wrapper into theBus API """
# 
#     GTFS_SERVICE_ALERT_ENDPOINT = \
#         'http://webapps.thebus.org/transitdata/production/servicealerts/'
# 
#     GTFS_TRIP_UPDATE_ENDPOINT = \
#         'http://webapps.thebus.org/transitdata/production/tripupdates/'
# 
#     GTFS_VEHICLE_LOCATION_ENDPOINT = \
#         'http://webapps.thebus.org/transitdata/production/vehloc/'
# 
#     def __init__(self, api_key, domain='api.thebus.org', timeout=60):
#         self.api_key = api_key
#         self.domain = domain
#         self.timeout = timeout
# 
#     @cacheable(cache=cache, key=None, ttl=10)
#     def get_arrivals(self, stop_id, callback=None):
#         query_params = {'key': self.api_key, 'stop': stop_id}
#         endpoint = self._get_endpoint('/arrivals', query_params)
#         return self._call_endpoint(endpoint, callback)
# 
#     def track_vehicle(self, vehicle_id=None, callback=None):
#         query_params = {'key': self.api_key}
#         if vehicle_id:
#             query_params['num'] = vehicle_id
#         endpoint = self._get_endpoint('/vehicle', query_params)
#         return self._call_endpoint(endpoint, callback)
# 
#     def get_gtfs_service_alerts(self):
#         return self._get_gtfs_data(self.GTFS_SERVICE_ALERT_ENDPOINT)
# 
#     def get_gtfs_trip_updates(self):
#         return self._get_gtfs_data(self.GTFS_TRIP_UPDATE_ENDPOINT)
# 
#     def get_gtfs_vehicle_location(self):
#         return self._get_gtfs_data(self.GTFS_VEHICLE_LOCATION_ENDPOINT)
# 
#     def _get_gtfs_data(self, endpoint):
#         gtfs_data = self._call_endpoint(endpoint)
# 
#         gtfs_message = gtfs_realtime_pb2.FeedMessage()
#         gtfs_message.ParseFromString(gtfs_data)
#         return gtfs_message
# 
#     def _get_endpoint(self, path='', query_params=None):
#         normalized_path = path + '/' if path and path[-1] != '/' else path
#         endpoint = "{0[scheme]}://{0[domain]}{0[path]}".format(
#             {'scheme': 'http',
#              'domain': self.domain,
#              'path': normalized_path})
#         if query_params:
#             endpoint = endpoint + '?' + urllib.urlencode(query_params)
#         return endpoint
# 
#     def _call_endpoint(self, endpoint, callback=None):
#         request = urllib2.Request(endpoint)
#         try:
#             response = urllib2.urlopen(request)
#         except urllib2.URLError:
#             raise TheBusException("Unable to contact %s" % endpoint)
# 
#         payload = callback(response) if callback else response.read()
#         response.close()
#         return payload
# 
#     def __str__(self):
#         return "TheBus Client"
# 
# Path: allbus/thebus/utilities/time_utilities.py
# def naive_to_timestamp(naive_datetime, tz='Pacific/Honolulu'):
#     tz_aware_dt = pytz.timezone(tz).localize(naive_datetime, is_dst=None)
#     utc_epoch_dt = datetime.datetime(1970, 1, 1, tzinfo=pytz.utc)
#     return int((tz_aware_dt - utc_epoch_dt).total_seconds())


import datetime
import json
from django.conf import settings
from django.http import Http404
from django.http import HttpResponse
from ..utilities.client import parse_xml_to_dict
from ..utilities.client import TheBusClient
from ..utilities.time_utilities import naive_to_timestamp
#!/usr/bin/env python
# -*- coding: utf-8 -*-




def all_buses(request):
    c = TheBusClient(settings.THEBUS_API_CLIENT_TOKEN)
    gtfs_vl = c.get_gtfs_vehicle_location()
    vl = [{'id': v.id,
           'latitude': v.vehicle.position.latitude,
           'longitude': v.vehicle.position.longitude} for v in gtfs_vl.entity]
    return HttpResponse(json.dumps(vl), content_type="application/json")


def bus_details(request, bus):

# Path: allbus/thebus/utilities/client.py
# def parse_xml_to_dict(response):
#     options = {
#         'strip_ns': True,
#         'strip_nl': True,
#         'pretty': True,
#         'strip_text': True
#     }
#     elem = ET.fromstring(response.read())
#     if hasattr(elem, 'getroot'):
#         elem = elem.getroot()
#     return elem_to_internal(elem, options)
# 
# Path: allbus/thebus/utilities/client.py
# class TheBusClient(object):
#     """ Thin wrapper into theBus API """
# 
#     GTFS_SERVICE_ALERT_ENDPOINT = \
#         'http://webapps.thebus.org/transitdata/production/servicealerts/'
# 
#     GTFS_TRIP_UPDATE_ENDPOINT = \
#         'http://webapps.thebus.org/transitdata/production/tripupdates/'
# 
#     GTFS_VEHICLE_LOCATION_ENDPOINT = \
#         'http://webapps.thebus.org/transitdata/production/vehloc/'
# 
#     def __init__(self, api_key, domain='api.thebus.org', timeout=60):
#         self.api_key = api_key
#         self.domain = domain
#         self.timeout = timeout
# 
#     @cacheable(cache=cache, key=None, ttl=10)
#     def get_arrivals(self, stop_id, callback=None):
#         query_params = {'key': self.api_key, 'stop': stop_id}
#         endpoint = self._get_endpoint('/arrivals', query_params)
#         return self._call_endpoint(endpoint, callback)
# 
#     def track_vehicle(self, vehicle_id=None, callback=None):
#         query_params = {'key': self.api_key}
#         if vehicle_id:
#             query_params['num'] = vehicle_id
#         endpoint = self._get_endpoint('/vehicle', query_params)
#         return self._call_endpoint(endpoint, callback)
# 
#     def get_gtfs_service_alerts(self):
#         return self._get_gtfs_data(self.GTFS_SERVICE_ALERT_ENDPOINT)
# 
#     def get_gtfs_trip_updates(self):
#         return self._get_gtfs_data(self.GTFS_TRIP_UPDATE_ENDPOINT)
# 
#     def get_gtfs_vehicle_location(self):
#         return self._get_gtfs_data(self.GTFS_VEHICLE_LOCATION_ENDPOINT)
# 
#     def _get_gtfs_data(self, endpoint):
#         gtfs_data = self._call_endpoint(endpoint)
# 
#         gtfs_message = gtfs_realtime_pb2.FeedMessage()
#         gtfs_message.ParseFromString(gtfs_data)
#         return gtfs_message
# 
#     def _get_endpoint(self, path='', query_params=None):
#         normalized_path = path + '/' if path and path[-1] != '/' else path
#         endpoint = "{0[scheme]}://{0[domain]}{0[path]}".format(
#             {'scheme': 'http',
#              'domain': self.domain,
#              'path': normalized_path})
#         if query_params:
#             endpoint = endpoint + '?' + urllib.urlencode(query_params)
#         return endpoint
# 
#     def _call_endpoint(self, endpoint, callback=None):
#         request = urllib2.Request(endpoint)
#         try:
#             response = urllib2.urlopen(request)
#         except urllib2.URLError:
#             raise TheBusException("Unable to contact %s" % endpoint)
# 
#         payload = callback(response) if callback else response.read()
#         response.close()
#         return payload
# 
#     def __str__(self):
#         return "TheBus Client"
# 
# Path: allbus/thebus/utilities/time_utilities.py
# def naive_to_timestamp(naive_datetime, tz='Pacific/Honolulu'):
#     tz_aware_dt = pytz.timezone(tz).localize(naive_datetime, is_dst=None)
#     utc_epoch_dt = datetime.datetime(1970, 1, 1, tzinfo=pytz.utc)
#     return int((tz_aware_dt - utc_epoch_dt).total_seconds())


# Path: allbus/thebus/views/bus_views.py
import datetime
import json
from django.conf import settings
from django.http import Http404
from django.http import HttpResponse
from ..utilities.client import parse_xml_to_dict
from ..utilities.client import TheBusClient
from ..utilities.time_utilities import naive_to_timestamp

#!/usr/bin/env python
# -*- coding: utf-8 -*-




def all_buses(request):
    c = TheBusClient(settings.THEBUS_API_CLIENT_TOKEN)
    gtfs_vl = c.get_gtfs_vehicle_location()
    vl = [{'id': v.id,
           'latitude': v.vehicle.position.latitude,
           'longitude': v.vehicle.position.longitude} for v in gtfs_vl.entity]
    return HttpResponse(json.dumps(vl), content_type="application/json")


def bus_details(request, bus):

    c = TheBusClient(settings.THEBUS_API_CLIENT_TOKEN)