• stephen@mathsgrinds.ie

Maths Grinds ie

  • Grinds
    • Junior Cert Maths Grinds
    • Leaving Cert Maths Grinds
    • Applied Maths Grinds
    • Leaving Cert Physics Grinds
    • University Maths Grinds
    • University Statistics Grinds
    • University Physics Grinds
  • Classes
    • Christmas Revision
    • Leaving Cert Applied Maths
    • Leaving Cert Maths
    • Leaving Cert Physics
  • Corporate
  • Thesis
  • Blog
  • Contact
  • Login
 
 

Dublin Bus

Posted May 27, 12:31 pm

When I leave college I have a choice of many buses from 4 different bus stops. It really bothered me that I had to open 4 different tabs to see what bus I should get and from where! So, I wrote a php page that collects all the information and puts it together in a nice clear table. You can see it at www.dublinbus.co.nr, anyway, I'm writing to work with the dublinbus page again but this time I want to use Python instead of php. I wrote a little code that can tell me when the next bus is from a given stop. Here is the code:

The Code:

# -*- coding: utf-8 -*-
#- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
# IMPORT REQUIRED MODULES
#- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
import os
import urllib2
from bs4 import BeautifulSoup
import datetime
#- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
# DOWNLOAD WEBPAGE
#- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
def get_webpage(url):
     user_agent = 'Mozilla/5.0 (Macintosh; U; Intel Mac OS X 10_6_4; en-US) AppleWebKit/534.3 (KHTML, like Gecko) Chrome/6.0.472.63 Safari/534.3'
     headers = { 'User-Agent' : user_agent }
     req = urllib2.Request(url, None, headers)
     response = urllib2.urlopen(req)
     return response.read()
#- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
# GET NEXT BUS TIME
#- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
def get_next_bus_time(route, stop):
     STOP = str(stop)
     ROUTE = " "+str(route)+" "
     url = "http://dublinbus.ie/en/RTPI/Sources-of-Real-Time-Information/?searchtype=view&searchquery="+STOP
     html = get_webpage(url)
     soup = BeautifulSoup(html)
     for row in soup.find('table', id='rtpi-results'):
          ROW=str(' '.join(str(row).split()))
          if ROUTE in ROW:
               for col in ROW.split(""):
                         if ":" in col:
                              return col.strip(' ').lstrip('').rstrip('').strip(' ')
                         elif "Due" in col:
                              return str(datetime.datetime.now().time())[:5]
#- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
# MAIN
#- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
time = get_next_bus_time(11,10)
print "The next Number 11 Bus, from Stop Number 10, will arrive at "+time+"."



Author
Stephen Easley-Walsh


 

© 2008-2015 Stephen Easley-Walsh. ALL RIGHTS RESERVED. This website contains material protected under International Copyright Laws and Treaties. Any unauthorized use of this material is prohibited. No part of this website may be reproduced or transmitted in any form or by any means, electronic or mechanical, including photocopying, recording, or by any information storage and retrieval system without express written permission from the author.You can read the Privacy Policy here.

HTML5 Powered

HTML 5 VALID   |   CSS 3 VALID   |   SITEMAP   |   SERVER