Posterous
olek is using Posterous to post everything online. Shouldn't you?
A
 

olek’s posterous

random

« Back to blog

python listing

pajtonowa klasa do przeglądania programu tv, lista odświeżana przy tworzeniu instancji obiektu ;-)


import re, urllib, sys

class TvProgram:
    def __init__(self):
        self.url = 'http://tv.o2.pl/program/'
        self.tv_list = {}
        
        html = urllib.urlopen(self.url).read()
         pat = re.compile('<option value=(\d{1,2}) > (.*)')

        for m in pat.finditer(html):
            self.tv_list[m.group(2).lower().strip()] = m.group(1)
    
    def get_channel(self, channel, day = 0):
         try:
            id_channel = self.tv_list[channel]
        except KeyError:
            raise
        
        query = { 'id_stacja' : id_channel,
                  'dzien' : day }
         
        html = urllib.urlopen(self.url, urllib.urlencode(query)).read()
        pat = re.compile(r'(?P<time>\d{2}:\d{2})(.|\n)+?<b>(?P<title>.*)</b>')
        
        for m in pat.finditer(html):
             yield m.group('time'), m.group('title')
        

def main():
    tv_prog = TvProgram()
    
    try:
        for time, title in tv_prog.get_channel('tvn'):
            print time, title
     except KeyError:
        print 'Nie znaleziono stacji'
    

if __name__ == '__main__':
    main()

 
Got an account with one of these? Login here, or just enter your comment below.
Posterous-login    Connect    twitter