Skip to content
This repository
branch: master
Fetching contributors…

Octocat-spinner-32-eaf2f5

Cannot retrieve contributors at this time

executable file 45 lines (33 sloc) 1.124 kb
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44
from BeautifulSoup import BeautifulSoup
import urllib, sys
import simplejson as json

class Countdown(object):

def __init__(self):
print "init countdown"

def getCountdownJSON(self, *stop_number):
if(len(stop_number) == 0):
doc = urllib.urlopen("http://accessible.countdown.tfl.gov.uk/arrivals/50980")
else:
doc = urllib.urlopen("http://accessible.countdown.tfl.gov.uk/arrivals/" + str(stop_number[0]))
soup = BeautifulSoup(doc)
doc.close()
rawTable = soup.body.div.tbody

texts = [textOf(n) for n in soup.findAll('td')]
cleanTexts = []
for tagText in texts[:]:
#cleanStr = str(tagText).strip().strip(" ")
cleanStr = str(tagText).strip()
cleanTexts.append(cleanStr)
    
textGroups = groupByThree(cleanTexts)
    
json_data = json.dumps(textGroups)

return json_data

def textOf(soup):
  return u''.join(soup.findAll(text=True))

def groupByThree(lst):
"""
Given input [1,2,3,4,5,6,7,8,9]
outputs: [(1,2,3),(4,5,6),(7,8,9)]
"""
return zip(*[lst[x::3] for x in (0, 1, 2)])


if __name__ == '__main__':
print Countdown().getCountdownJSON(49062)
Something went wrong with that request. Please try again.