Skip to content

Instantly share code, notes, and snippets.

@nbergont
Last active January 3, 2016 06:58
Show Gist options
  • Save nbergont/8425970 to your computer and use it in GitHub Desktop.
Save nbergont/8425970 to your computer and use it in GitHub Desktop.
Affiche les horaires du RER (comme les vieux afficheurs en gare)
import requests
from bs4 import BeautifulSoup
import pygame
from pygame.locals import *
import time
def readHtml():
payload = {'origine': 'Massy Palaiseau', 'origineCode': 'MPU', 'origineData' : '', 'destination' : '', 'destinationCode' : '', 'destinationData' : ''}
r = requests.post('http://www.transilien.mobi/train/result', data=payload)
print "STATUS CODE = " + str(r.status_code)
return r.text
class horaire:
def __init__(self):
self.heure = ""
self.dest = ""
self.code = ""
self.voie = ""
def reduce_dest(self):
if len(self.dest) > 21:
return self.dest[:21] + "."
return self.dest
def __str__(self):
return self.heure + " - " + self.dest + " (" + self.code + ") voie " + self.voie
def readHorairesRER():
soup = BeautifulSoup(readHtml())
#resultats = soup.select('li.resultat_gare')
results = []
result = horaire()
for node in soup.find_all("div", ["heure_train", "garearrivee", "train_mission", "voie"]):
if node['class'] == ['heure_train']:
result.heure = node.contents[0]
elif node['class'] == ['garearrivee']:
result.dest = node.contents[0][6:]
elif node['class'] == ['train_mission']:
result.code = node.contents[0][1:-1]
elif node['class'] == ['voie']:
result.voie = node.contents[0][5:]
results.append(result)
result = horaire()
else:
raise "PARSE ERROR"
#print '[%s]' % ', '.join(map(str, results))
return results
def heureCourante():
return time.strftime("%H:%M", time.localtime())
#**************************************
def main():
#print readHtml()
#return 0
pygame.init()
size = width, height = pygame.display.list_modes()[0]
background = 33, 67, 146
yellow = 226, 232, 32
white = 255, 255, 255
black = 0, 0, 0
screen = pygame.display.set_mode(size, pygame.FULLSCREEN)
clock = pygame.time.Clock()
pygame.font.init()
normalFont = pygame.font.Font(None, int(width*0.06))
smallFont = pygame.font.Font(None, int(width*0.03))
offsety_header = int(height*0.1)
offsety_info = int(height-height*0.3)
offsetx_nom = int(width*0.01)
offsetx_dest = int(width*0.15)
offsetx_heure = int(width*0.7)
offsetx_voie = int(width*0.92)
font_height = int(width*0.05)
line_height = 10
last_update = time.time()
results = readHorairesRER()
while 1:
#Met a jour les horaires toutes les 2 minutes
if time.time() - last_update >= 60*2:
print "Met a jour les horaires..."
results = readHorairesRER()
last_update = time.time()
for event in pygame.event.get():
if event.type == QUIT or (event.type == KEYDOWN and event.key == K_ESCAPE):
pygame.quit()
return 0
screen.fill(background)
#Draw text headers
screen.blit(pygame.font.Font.render(normalFont, heureCourante(), 1, yellow, black), (width-int(font_height*2.5), 15))
screen.blit(pygame.font.Font.render(normalFont, "Nom", 1, yellow), (offsetx_nom, offsety_header))
screen.blit(pygame.font.Font.render(normalFont, "Destination", 1, yellow), (offsetx_dest, offsety_header))
screen.blit(pygame.font.Font.render(normalFont, "Heure", 1, yellow), (offsetx_heure, offsety_header))
screen.blit(pygame.font.Font.render(smallFont, "Voie", 1, yellow), (offsetx_voie, offsety_header+int(font_height/4)))
screen.blit(pygame.font.Font.render(smallFont, 'Les horaires affiches sont les horaires theoriques', 1, yellow), (font_height*3, offsety_info+50))
#Draw lines
pygame.draw.line(screen, yellow, (offsetx_dest-line_height, offsety_header), (offsetx_dest-line_height, offsety_info), 4)
pygame.draw.line(screen, yellow, (offsetx_heure-line_height, offsety_header), (offsetx_heure-line_height, offsety_info), 4)
pygame.draw.line(screen, yellow, (offsetx_voie-line_height, offsety_header), (offsetx_voie-line_height, offsety_info), 4)
pygame.draw.line(screen, yellow, (0, offsety_header + font_height), (width, offsety_header + font_height), 4)
#Draw results
offset = offsety_header + font_height + 10
for result in results:
screen.blit(pygame.font.Font.render(normalFont, result.code, 1, white), (offsetx_nom, offset))
screen.blit(pygame.font.Font.render(normalFont, result.reduce_dest(), 1, white), (offsetx_dest, offset))
screen.blit(pygame.font.Font.render(normalFont, result.heure, 1, white), (offsetx_heure, offset))
screen.blit(pygame.font.Font.render(normalFont, result.voie, 1, white), (offsetx_voie, offset))
offset = offset + font_height
if offset > offsety_info - font_height:
break
pygame.display.flip()
clock.tick(20) #2Fps
if __name__ == '__main__':
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment