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

olek’s posterous

random

Python: bot gadu-gadu

Nie ma to jak trochę wolnego czasu. Taki sobie pseudo framework bota Gadu-Gadu w postaci plugina do ekg. Słownik callbacks zawiera funkcje, które obsługuje bot. Ciało tych funkcji musi zawierać sie nad nim by nie było błędów. Jeżeli wartość zwracana przez funkcje ma być wielo linijkowa, użyj lambdy ret aby połączyć liste zawierającą odpowiedź w 1 wiadomość zwrotną.

#!/usr/bin/python
# -*- coding: utf-8 -*-
import ekg, re, urllib2

# zewnetrzne moduly uzyte w funkcjach
import shortener

ekg.config.uin = numerek
ekg.config.password = 'haslo'


ret = lambda result: '\n'.join(result)

def tnij(msg):
    '''Skracanie linkow, po przecinku mozna dawac kilka'''
    result = []

    for longUrl in msg:
        result.append(shortener.shorten(longUrl))


    result.append('Aby sprawdzic ile bylo odwiedzin na danym linku dodaj /hits d
o adresu')
    return ret(result)

def reverse(msg):
    '''Odwraca podany ciag znakow, bezuzyteczne'''

    result = ' '.join(msg)

    return result[::-1]

def get_help(msg):
    '''Wlasnie to ogladasz'''
    result = []

    for k, v in callbacks.iteritems():
        result.append(str(k) + ' : ' + str(v.__doc__).strip())


    return ret(result)

callbacks = {
             'pomoc': get_help,
             'odwroc': reverse,
             'tnij' : tnij,
            }

def init():
    return 1


def deinit():
    return 1

def handle_msg(uin, name, msgclass, text, time, secure):
    msg = re.split('\s', text)
    if not callbacks.has_key(msg[0]):
        ekg.command('msg %d Sorry, nie wiem o co Ci chodzi. Moze wpisz pomoc' %

uin)
        return 2

    result = callbacks[msg[0]](msg[1:])
    ekg.command('msg %d %s' % (uin, result))

    return 2

Filed under  //   gadu-gadu   python  
Posted April 30, 2009
// 1 Comment

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()

Filed under  //   python  
Posted April 19, 2009
// 1 Comment