Created
December 21, 2012 11:37
-
-
Save mharju/4352299 to your computer and use it in GitHub Desktop.
Birthday greeter generator. Useful for giving good birthday greetings on Facebook
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# -*- encoding: utf-8 -*- | |
from BeautifulSoup import BeautifulSoup | |
from random import sample, choice | |
import httplib2 | |
import sys | |
import pickle | |
import os.path | |
class Quotes(object): | |
"Gets random finnish quotes from wikiquote" | |
quote_file = '/tmp/quote_cache' | |
wiki_url = 'http://fi.wikiquote.org/wiki/Suomalaisia_sananlaskuja' | |
def __init__(self): | |
self.quotes = list() | |
self.__load() | |
def __load(self): | |
"""Loads quotes either from disk or the internet. If they are loaded from | |
the internet, parse and store them to disk""" | |
if os.path.exists( self.quote_file ): | |
with open(self.quote_file, 'rt') as f: | |
self.quotes = pickle.load(f) | |
else: | |
headers, page = httplib2.Http().request(self.wiki_url) | |
self.data = page | |
self.__parse() | |
self.__store() | |
def __parse(self): | |
"Parses the list of quotes received from Wikiquote" | |
soup = BeautifulSoup(self.data) | |
for item in soup.findAll('li'): | |
for quote in item.findAll('i'): | |
self.quotes.append( quote.contents[0].strip('.') ) | |
def __store(self): | |
"Stores the quotes in this instance to disk. Overwrites existing file." | |
with open(self.quote_file, 'wb') as f: | |
pickle.dump(self.quotes, f, protocol=2) | |
def get_random(self, count=1): | |
"""Gets the specified number of random quotes and returns them as a list. | |
The number of returned elements is at maximum the length of the list""" | |
if len(self.quotes) <= count: | |
count = self.quotes | |
return sample(self.quotes, count) | |
# Birthday specific stuff | |
GREETINGS = ( | |
u'Oikein hyvää syntymäpäivää!', | |
u'Tuhannet onnittelut syntymäpäivän johdosta!', | |
u'Mahtavaa synttäriä!', | |
u'Tosi hyvää synttäripäivää, toivottavasti kaikki sujuu hyvin :-)', | |
u'Loistavaa syntymäpäivää', | |
u'Ihan tajutonta synttäriä!', | |
u'Pidä itses kunnossa, vanhus!', | |
u'Uhmaikä takana, toinen edessä. Mahtavaa synttäriä!', | |
u'Syntymäpäiväonnittelut!', | |
u'Toivottavasti ei jää kauheat traumat. Hyvää syntymäpäivää!', | |
u'Aurinkoista syntymäpäivää!', | |
u'Ja rapatessa roiskuu, mahtavaa synttäriä, vanhus!', | |
) | |
# If run from command line, gets n random quotes and combines them with a | |
# birthday greeting at the end. | |
if __name__ == '__main__': | |
num = min(len(sys.argv) > 1 and int(sys.argv[1]) or 1, len(GREETINGS)) | |
print "\n".join( ['"%s". %s' % (quote, greeting,) for quote, greeting in | |
zip(Quotes().get_random(num), sample(GREETINGS, num))] ) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment