background

Bash w aplecie rybki

Prosty skrypt do wyświetlania losowego cytatu z basha w aplecie rybki. Wystarczy zapisać do pliku i dodać jako polecenie w preferencjach.

import urllib2, re
from BeautifulSoup import BeautifulSoup

def main():
    handle = urllib2.Request("http://bash.org.pl/random/")
    handle.add_header("User_agent",
"Mozilla/5.0 (X11; U; Linux i686) Gecko/20071127 Firefox/2.0.0.11")
    handle = urllib2.urlopen(handle)
    content =  handle.read()
    handle.close()
    soup = BeautifulSoup(content, convertEntities=BeautifulSoup.HTML_ENTITIES)
    id = soup.find("a",{"class": re.compile(r".*qid.*")})
    id = id.getText()
    print "ID: %s" % id
    for element in soup.findAll("div", {'class': re.compile('.*quote.*')}):
        quote = re.sub(r"",r"\n",str(element))
        quote = re.sub(r"(<div>)|(<\/div>)",r"",quote)
        quote = quote.splitlines(True)
        for line in quote:
            words = line.strip()
            if words:
                w = words.split()
                leng = 0
                t_line = ""
                for word in w:
                    if leng<60:
                        t_line+=(word)
                    else:
                        leng = 0
                        t_line+=("\n")
                        t_line+=(word)
                    leng += len(word)
                    t_line +=" "
                print t_line
main()