#!/usr/bin/python # -*- coding: iso-8859-1 -*- __version__ = "$Revision: 0.4$" __author__ = "Pierrick Terrettaz, Nicolas Seriot, Cédric Luthi" __date__ = "2005-07-19" """ 2005.07.19 : PIK : - première version 2005.07.20 : NST : - corrigé la regexp pour les titres - ajouté des options pour : - choisir les chaînes - afficher à partir de l'heure courante 2005.07.28 : NST : - ajouté la gestion du timeout 2006.02.06 : CLI : - corrigé une regexp pour la nouvelle formule du site """ import socket import re from urllib import * from time import * import sys socket.setdefaulttimeout(4) # secondes time = localtime(time()) current_time = ( int(strftime("%H", time)), int(strftime("%M", time)) ) def display((time, show)): print time_to_str(time), '|', show def fill_decimal(x): if int(x) < 10: x = "0" + str(x) return str(x) def time_to_str(time): ret = map(fill_decimal, time) ret = reduce(lambda x,y:x+":"+y, ret) return ret def time_from_str(time): time = time.split(':') return tuple(map(int, time)) def current_time_index(time_list): i = 0 for t in time_list: if current_time > t: i = i + 1 if time_list[i] < time_list[i-1]: break else: break return i - 1 #proxies={'http':'http://proxy-1.eivd.ch:8080'} proxies={} regexp = {"time" :".*
(.*)
.*", \ #"title":".*
(.*)
.*"} "title":"((^|.*true\)\">)(.*).*)|(.*
(.*)
)"} base_url = "http://www.tsr.ch/tsr/index.html?siteSect=601000&idChaine=" # commenter les chaînes qu'on ne veut pas voir chaines = [] #chaines.append(dict(name = "TSR1", id = "1")) chaines.append({'name':"TSR1", 'id':"1"}) chaines.append({'name':"TSR2", 'id':"2"}) chaines.append({'name':"Arte", 'id':"10"}) chaines.append({'name':"Canal", 'id':"1"}) chaines.append({'name':"Eurosport", 'id':"12"}) chaines.append({'name':"France2", 'id':"13"}) chaines.append({'name':"France3", 'id':"14"}) chaines.append({'name':"France5", 'id':"15"}) chaines.append({'name':"M6", 'id':"16"}) chaines.append({'name':"RTL9", 'id':"17"}) chaines.append({'name':"SF1", 'id':"18"}) chaines.append({'name':"SF2", 'id':"19"}) chaines.append({'name':"TF1", 'id':"20"}) chaines.append({'name':"TMC", 'id':"21"}) chaines.append({'name':"TSI", 'id':"22"}) chaines.append({'name':"TSI2", 'id':"23"}) chaines.append({'name':"TV5", 'id':"24"}) # lire les options et les chaînes demandées options = [] wanted = [] try: args = sys.argv[1:] for a in args: if a.startswith('-'): options.append(a) else: wanted.append(a) if not options: options.append("-all") # default mode except IndexError: pass if "-h" in options or "-help" in options: print " Affiche le programme TV" print " USAGE: python " + sys.argv[0] + " [+] [-now | -next]" sys.exit(0) # en cas de demande, ne garder que les chaînes demandées if wanted: chaines = filter(lambda x:x['name'] in wanted, chaines) for c in chaines: try: page = urlopen(base_url + c['id'], proxies = proxies) except IOError: print c['name'] + "\t" + "timeout" continue time = {"re":regexp["time"], "list":[]} title = {"re":regexp["title"],"list":[]} #print title print "\n---- " + c["name"] + " ----" for line in page.readlines(): m = re.match(regexp["time"], line) if m: time["list"].append(m.group(1)) m = re.match(regexp["title"], line) if m: t = m.group(3) if not t: t = m.group(5) title["list"].append(t) time["list"] = map(time_from_str, time["list"]) i = current_time_index(time["list"]) records = zip(time["list"], title["list"]) slices = {} slices["-now"] = slice(i,i+1) slices["-next"] = slice(i+1,None) slices["-all"] = slice(None) map(display, records[slices[options[0]]]) if not "-now" in options: if c != chaines[-1:][0]: print "" raw_input()