From 7842288a6320efb63da84e431eeb8d9bdc2c64a0 Mon Sep 17 00:00:00 2001 From: Sanj Date: Tue, 8 May 2012 21:41:50 +0530 Subject: [PATCH] mumbaitrains + apiviews minor optimization --- chaloBEST/mumbai/apiviews.py | 4 +-- chaloBEST/mumbaitrains/__init__.py | 0 chaloBEST/mumbaitrains/models.py | 32 ++++++++++++++++++ chaloBEST/mumbaitrains/parse.py | 52 ++++++++++++++++++++++++++++++ chaloBEST/mumbaitrains/tests.py | 16 +++++++++ chaloBEST/mumbaitrains/views.py | 1 + chaloBEST/settings.py | 3 ++ 7 files changed, 106 insertions(+), 2 deletions(-) create mode 100644 chaloBEST/mumbaitrains/__init__.py create mode 100644 chaloBEST/mumbaitrains/models.py create mode 100644 chaloBEST/mumbaitrains/parse.py create mode 100644 chaloBEST/mumbaitrains/tests.py create mode 100644 chaloBEST/mumbaitrains/views.py diff --git a/chaloBEST/mumbai/apiviews.py b/chaloBEST/mumbai/apiviews.py index c7935d4..0016866 100644 --- a/chaloBEST/mumbai/apiviews.py +++ b/chaloBEST/mumbai/apiviews.py @@ -67,9 +67,9 @@ def areas(request): def stops(request): q = request.GET.get("q", "") if q != '': - qset = Stop.objects.find_approximate(q, TRIGRAM_THRESHOLD).select_related() + qset = Stop.objects.find_approximate(q, TRIGRAM_THRESHOLD).select_related('road', 'area') else: - qset = Stop.objects.all().select_related() + qset = Stop.objects.all().select_related('road', 'area') srid = int(request.GET.get("srid", 4326)) return render_to_json_response({ 'type': 'FeatureCollection', diff --git a/chaloBEST/mumbaitrains/__init__.py b/chaloBEST/mumbaitrains/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/chaloBEST/mumbaitrains/models.py b/chaloBEST/mumbaitrains/models.py new file mode 100644 index 0000000..395972e --- /dev/null +++ b/chaloBEST/mumbaitrains/models.py @@ -0,0 +1,32 @@ +from django.contrib.gis.db import models + + +LINE_CHOICES = ( + ('Western', 'Western'), + ('Central', 'Central'), + ('Harbour', 'Harbour'), +) + +class Train(models.Model): + number = models.CharField(max_length=128) + line = models.CharField(max_length=128, choices=LINE_CHOICES, db_index=True) + stations = models.ManyToManyField("Station", through='TrainStation') + + def __unicode__(self): + return self.number + +class Station(models.Model): + name = models.CharField(max_length=128) + point = models.PointField(null=True, blank=True) +# line = models.CharField(choices=LINE_CHOICES, db_index=True) + + def __unicode__(self): + return self.name + +class TrainStation(models.Model): + train = models.ForeignKey(Train) + station = models.ForeignKey(Station) + serial = models.IntegerField() + time = models.TimeField() + +# Create your models here. diff --git a/chaloBEST/mumbaitrains/parse.py b/chaloBEST/mumbaitrains/parse.py new file mode 100644 index 0000000..dca41b7 --- /dev/null +++ b/chaloBEST/mumbaitrains/parse.py @@ -0,0 +1,52 @@ +from pyquery import PyQuery as pq +from models import * +import datetime + +BASE_URL = 'http://mumbailifeline.com/' + +''' +eg.: +>>>parseURL('http://mumbailifeline.com/timetable.php?sel_route=central&sfrom=Mumbai_CST&sto=Masjid&time1=4:00%20am&time2=11:59%20PM', Central') +''' +def parseURL(url, line): + d = pq(url=url) + table = d('#gradient-style') + trs = table.find('tr') + for i in range(1,len(trs)): + thisTr = trs[i] + td = thisTr.getchildren()[0] + a = td.find('a') + trainNo = a.text.strip() + print "Saving %s ... " % trainNo + trainURL = BASE_URL + a.get('href').strip() + saveTrain(trainURL, trainNo, line) + + +def saveTrain(url, no, line): + train, created = Train.objects.get_or_create(number=no, line=line) + if not created: + print "Train no %s already exists in db, skipping" % no + return + train.save() + d = pq(url=url) + table = d.find('table')[3] + for tr in table.iterfind('tr'): + children = tr.getchildren() + serial = 0 + if len(tr.findall('td')) > 0: + td0 = children[0] + a = td0.find('a') + stationName = a.text.strip() + station, created = Station.objects.get_or_create(name=stationName) + timeString = children[1].find('strong').text + hour = int(timeString.split(":")[0].strip()) - 1 + mins = int(timeString.split(":")[1][0:2]) + ampm = timeString[-2:] + if ampm == 'pm': + hour = hour + 12 + stationTime = datetime.time(hour,mins) + st = TrainStation(train=train, station=station, time=stationTime, serial=serial) + st.save() + serial += 1 + print "Saved %s" % no + diff --git a/chaloBEST/mumbaitrains/tests.py b/chaloBEST/mumbaitrains/tests.py new file mode 100644 index 0000000..501deb7 --- /dev/null +++ b/chaloBEST/mumbaitrains/tests.py @@ -0,0 +1,16 @@ +""" +This file demonstrates writing tests using the unittest module. These will pass +when you run "manage.py test". + +Replace this with more appropriate tests for your application. +""" + +from django.test import TestCase + + +class SimpleTest(TestCase): + def test_basic_addition(self): + """ + Tests that 1 + 1 always equals 2. + """ + self.assertEqual(1 + 1, 2) diff --git a/chaloBEST/mumbaitrains/views.py b/chaloBEST/mumbaitrains/views.py new file mode 100644 index 0000000..60f00ef --- /dev/null +++ b/chaloBEST/mumbaitrains/views.py @@ -0,0 +1 @@ +# Create your views here. diff --git a/chaloBEST/settings.py b/chaloBEST/settings.py index 53a0719..a9692b7 100644 --- a/chaloBEST/settings.py +++ b/chaloBEST/settings.py @@ -98,6 +98,7 @@ MIDDLEWARE_CLASSES = ( 'django.middleware.csrf.CsrfViewMiddleware', 'django.contrib.auth.middleware.AuthenticationMiddleware', 'django.contrib.messages.middleware.MessageMiddleware', + 'debug_toolbar.middleware.DebugToolbarMiddleware' ) ROOT_URLCONF = 'chaloBEST.urls' @@ -120,7 +121,9 @@ INSTALLED_APPS = ( # Uncomment the next line to enable admin documentation: 'django.contrib.gis', 'django_extensions', + 'debug_toolbar', 'mumbai', + 'mumbaitrains', # 'django.contrib.admindocs', )