Skip to content

Instantly share code, notes, and snippets.

@simonvc
Last active December 30, 2015 17:29
Show Gist options
  • Select an option

  • Save simonvc/7861341 to your computer and use it in GitHub Desktop.

Select an option

Save simonvc/7861341 to your computer and use it in GitHub Desktop.
python server page hack to make a train display board. See it working here. http://imgur.com/tW9Bugl
<html>
<head>
<title>Python Server Pages (PSP)</title>
<meta http-equiv="refresh" content="60; url=http://192.168.1.2:8000/">
<body>
<%
import json
import time
import requests
from BeautifulSoup import BeautifulSoup
import re
import datetime
linematch=re.compile('\[[0-9],\".*\",\"(.*)\",([0-9]*)\]')
tod={'0': "Good Night",
'01': "Sleep Well",
'02': "Good Night",
'03': "You're up late",
'04': "Going Somewhere",
'05': "blagh morning",
'06': "Good Morning",
'07': "Good Morning",
'08': "Good Morning",
'09': "Good Morning",
'10': "Good Morning",
'11': "Good Morning",
'12': "Good Day",
'13': "Good Day",
'14': "Good Day",
'15': "Good Afternoon",
'16': "Good Afternoon",
'17': "Good Afternoon",
'18': "Good Evening",
'19': "Good Evening",
'20': "Good Night",
'21': "Good Night",
'22': "Good Night",
'23': "Good Night"
}
%>
<pre><%=tod[time.strftime("%H")] %> Sara and Simon
The time is: <%=time.strftime("%H:%M:%S %Y-%m-%d,")%>
<%
r=requests.get('http://countdown.api.tfl.gov.uk/interfaces/ura/instant_V1?StopCode1=75362#')
lines=r.text.split('\r\n')
services=[linematch.match(i) for i in lines ]
services=[i.groups() for i in services if i]
services=[int(i[1])/1000 for i in services if i[0] == u'26']
services=[i - int(time.time()) for i in services]
services.sort()
services=[i / 60 for i in services]
bustimes = ", ".join([str(i) for i in services])
rtube=requests.get("http://api.tubeupdates.com/?method=get.status&lines=central&format=json")
centraljson=json.loads(rtube.text)
centralline=centraljson['response']['lines'][0]['status']
weather=json.loads(requests.get('http://api.openweathermap.org/data/2.5/weather?q=London,uk').text)
headline=weather['weather'][0]['main']
curtemp=int(weather['main']['temp'] - 273.15)
maxtemp=int(weather['main']['temp_max'] - 273.15)
mintemp=int(weather['main']['temp_min'] - 273.15)
weatherstring="%s %s C (%s - %s)" % (headline, curtemp, mintemp, maxtemp)
nrhref='http://ojp.nationalrail.co.uk/service/ldbboard/dep/LOF/LST/To'
traintable=BeautifulSoup(requests.get(nrhref).text)('table')[0]
trains=[]
for train in traintable('tr'):
if train('td'):
tds=train('td')
trains.append("%s %s" % (tds[0].text, tds[2].text) )
trainsblob="\n".join(trains)
%>
Trains to Liverpool St
<%=trainsblob%>
Next 26 Bus: <%=bustimes%> mins
Central Line: <%=centralline%>
Weather: <%=weatherstring%>
</pre>
</body>
</html>
@simonvc

simonvc commented Dec 9, 2013

Copy link
Copy Markdown
Author

Example Output

Good Day Sara and Simon
The time is: 13:02:21 2013-12-09,

Trains to Liverpool St
13:01 13:032minslate
13:16 On time
13:31 On time
13:46 On time
Next 26 Bus: 9, 18, 29 mins
Central Line: good service
Weather: Clear 11 C (9 - 13)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment