Created
June 14, 2017 12:03
-
-
Save cnorthwood/5573b0c2a210a86915de38d8dad6282e to your computer and use it in GitHub Desktop.
Metrolink scraper
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def format_tram_wait(wait): | |
if wait == 'Departing': | |
return 0 | |
else: | |
return int(wait) | |
def get_tram_stop_info(stop): | |
departure_board = BeautifulSoup( | |
requests.get('https://beta.tfgm.com/public-transport/stations/{}-tram?layout=false'.format(stop)).text, | |
"html.parser" | |
).find(id='departure-items') | |
if departure_board: | |
return [ | |
{ | |
'destination': departure.find("td", {"class":"departure-destination"}).text, | |
'carriages': departure.find("td", {"class": "departure-carriages"}).find("span").text, | |
'wait': format_tram_wait( | |
departure.find("td", {"class": "departure-wait"}).find("span", {"class":"figure"}).text | |
), | |
} | |
for departure in departure_board.find_all('tr') | |
] | |
else: | |
return [] | |
def get_tram_line_info(): | |
return requests.get('http://beta.tfgm.com/api/statuses/tram').json().get('items', []) | |
def format_tram(tram): | |
carriages = tram['carriages'].lower() | |
destination = tram['destination'] | |
wait = tram['wait'] | |
if wait == 0: | |
wait_text = 'now' | |
elif wait == 1: | |
wait_text = 'in 1 minute' | |
else: | |
wait_text = 'in {} minutes'.format(wait) | |
if destination == 'See Tram Front': | |
destination = 'an unknown destination' | |
return '{} tram to {} leaving {}'.format(carriages, destination, wait_text) | |
def mcuk_tram(**kwargs): | |
try: | |
mcuk_trams = list( | |
filter( | |
lambda t: not t['destination'].startswith('Eccles'), | |
get_tram_stop_info('mediacityuk') | |
) | |
) | |
harbour_city_trams = list( | |
filter( | |
lambda t: not (t['destination'].startswith('Eccles') or t['destination'] == 'MediaCityUK'), | |
get_tram_stop_info('harbour-city') | |
) | |
) | |
except: | |
LOGGER.exception('Tram lookup failed') | |
yield "Sorry, I couldn't get tram info right now" | |
else: | |
if len(mcuk_trams) == 0: | |
msg = "There are currently no trams on the departure board at MediaCityUK" | |
if len(harbour_city_trams): | |
msg += ', but the next tram from Harbour City is a {}'.format(format_tram(harbour_city_trams[0])) | |
yield msg | |
elif len(mcuk_trams) == 1: | |
yield ":metrolink: The next tram from MediaCityUK is a {}".format(format_tram(mcuk_trams[0])) | |
else: | |
yield ":metrolink: The next tram from MediaCityUK is a {}, followed by a {}".format( | |
format_tram(mcuk_trams[0]), format_tram(mcuk_trams[1]) | |
) | |
if len(mcuk_trams) > 0: | |
for harbour_city_tram in harbour_city_trams: | |
if harbour_city_tram['wait'] > 5 and (harbour_city_tram['wait'] < mcuk_trams[0]['wait'] or mcuk_trams[0]['wait'] < 5): | |
yield ":metrolink: There is also a {} from Harbour City".format(format_tram(harbour_city_tram)) | |
break | |
def tram_lines(**kwargs): | |
try: | |
lines = get_tram_line_info() | |
except: | |
LOGGER.exception('Tram line lookup failed') | |
yield "Sorry, I couldn't get tram info right now" | |
else: | |
yield "\n".join("{} {}: {}".format( | |
':derailedtram:' if line['severity'] == 'danger' else ':metrolink:', | |
line['name'], | |
line['status']) for line in lines) | |
yield "For full tram information, see http://beta.tfgm.com/public-transport/tram" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment