Unix fortune cookies on Twitter.

I just wrote a new script called twfortune. It chooses a random fortune, as the fortune(8) program in the BSD-games package does, and send it to your twitter account as a message of the day (motd)

You can download the complete script and a sample fortunes file from this link.
http://velvnet.com/c

In order for this script to work you need to install 2 python packages.

Installation

I have installed this script on my home server into ~/bin in my home directory. I also created a ~/.twfortunerc file, containing my twitter information and the path to my fortunes file.
$ cat ~/.twfortunerc
[fortune]
username = accountname
password = accountpassword
fortunes_path = ~/Documents/fortunes

$ cat ~/.twfortunerc
[fortune]
username = accountname
password = accountpassword
fortunes_path = ~/Documents/fortunes

Running

You can automatically post your fortune cookie every day at midnight using cron.
$ crontab -e
@daily     ~/bin/twfortune

$ crontab -e
@daily ~/bin/twfortune


You can edit the fortunes data file and add your own epigrams. Once you are done editing the fortune file you need to create the index file by running the command /usr/local/bin/fortune -u fortunes

The script

This script as well as a fortunes file can be downloaded from http://velvnet.com/c
#!/usr/bin/env python
# twfortune.py -- chooses a random fortune, as the fortune(8) program
#       in the BSD-games package does and post it on tweeter
#       http://twitter.com/

import twitter
import fortune
import ConfigParser
import os.path
import sys

USAGE = '''Usage twfortune
twfortune.py -- chooses a random fortune, as the fortune(8) program
in the BSD-games package does and posts it on twitter -> http://twitter.com/

You need to create a .twfortunerc file in your home directory
containing your twitter information as well as the path to a fortune
cookie file.

A skeletal .twfortunerc file:

[fortune]
username = your twitter username
password = your twitter password
fortune_path = /usr/share/games/fortune/fortunes

'''

class FortuneRc(object):
    def __init__(self):
        self._config = None
        
    def GetFortuneFile(self):
        filename = self._GetOption('fortunes_path')
        if filename.startswith('~'):
            filename = os.path.expanduser(filename)
        return filename
        
    def GetUsername(self):
        return self._GetOption('username')
        
    def GetPassword(self):
        return self._GetOption('password')
        
    def _GetOption(self, option):
        try:
            return self._GetConfig().get('fortune', option)
        except:
            return None

    def _GetConfig(self):
        if not self._config:
            self._config = ConfigParser.ConfigParser()
            self._config.read(os.path.expanduser('~/.twfortunerc'))
        return self._config

def Usage():
    print >>sys.stderr, USAGE
    sys.exit(2)

def main():
    rcfile = FortuneRc()
    username = rcfile.GetUsername()
    password = rcfile.GetPassword()
    fortunefile = rcfile.GetFortuneFile()

    if not username or not password or not fortunefile:
        Usage()

    if not os.path.exists(fortunefile):
        print >>sys.stderr, "File %s not found" % fortunefile
        Usage()

    cookie = fortune.get_random_fortune(fortunefile)
    while len(cookie) > 134:
        cookie = fortune.get_random_fortune(fortunefile)

    try:
        twapi = twitter.Api(username=username, password=password)
        twapi.PostUpdate("MOTD: " + cookie)
    except HTTPError, err:
        print >>sys.stderr, str(err)
        sys.exit(1)
        
if __name__ == "__main__":
    main()

#!/usr/bin/env python
# twfortune.py -- chooses a random fortune, as the fortune(8) program
# in the BSD-games package does and post it on tweeter
# http://twitter.com/

import twitter
import fortune
import ConfigParser
import os.path
import sys

USAGE = '''Usage twfortune
twfortune.py -- chooses a random fortune, as the fortune(8) program
in the BSD-games package does and posts it on twitter -> http://twitter.com/

You need to create a .twfortunerc file in your home directory
containing your twitter information as well as the path to a fortune
cookie file.

A skeletal .twfortunerc file:

[fortune]
username = your twitter username
password = your twitter password
fortune_path = /usr/share/games/fortune/fortunes

'''

class FortuneRc(object):
def __init__(self):
self._config = None

def GetFortuneFile(self):
filename = self._GetOption('fortunes_path')
if filename.startswith('~'):
filename = os.path.expanduser(filename)
return filename

def GetUsername(self):
return self._GetOption('username')

def GetPassword(self):
return self._GetOption('password')

def _GetOption(self, option):
try:
return self._GetConfig().get('fortune', option)
except:
return None

def _GetConfig(self):
if not self._config:
self._config = ConfigParser.ConfigParser()
self._config.read(os.path.expanduser('~/.twfortunerc'))
return self._config

def Usage():
print >>sys.stderr, USAGE
sys.exit(2)

def main():
rcfile = FortuneRc()
username = rcfile.GetUsername()
password = rcfile.GetPassword()
fortunefile = rcfile.GetFortuneFile()

if not username or not password or not fortunefile:
Usage()

if not os.path.exists(fortunefile):
print >>sys.stderr, "File %s not found" % fortunefile
Usage()

cookie = fortune.get_random_fortune(fortunefile)
while len(cookie) > 134:
cookie = fortune.get_random_fortune(fortunefile)

try:
twapi = twitter.Api(username=username, password=password)
twapi.PostUpdate("MOTD: " + cookie)
except HTTPError, err:
print >>sys.stderr, str(err)
sys.exit(1)

if __name__ == "__main__":
main()


You can see this script running on http://twitter.com/faxol512. Look at the messages starting with the tag MOTD:

http://fred.velvnet.com/twfortune.png



 

Comments

Posted by: Guillaume Dec 04, 2008 @ 00:08

Can you make it into a Web service you can subscribe to? the user wouldn't follow the Twitter account, but would get to see any fortune cookie replied to the registered user, so that you don't have to follow the account and get all the fortune cookies of all the users.

Posted by: fred Dec 04, 2008 @ 08:18

Yes I can make a web service who automatically send fortune cookies. But I don't understand the reply thing you want.

Posted by: Silversleeves Apr 30, 2009 @ 15:24

I suggest avoiding pico as an editor when creating your own fortune cookie files.

My first and only such strfile'd with a blank line as one random entry.

After half an hour of Google-ing and applying a little horse-sense, I discovered (from counting the lines in a Windows GUI text editor) that the blank line was at the very end, right after the last perc ("%") delimiter. Pico, as has been my experience, loves adding those blanks. (Anticipating comments on the order of "Why not use [insert favorite shell editor here]": I can say that after the third time of not being able to quit out of vim or emacs in OS X's Terminal app, pico seemed like a breath of fresh air; for all it's attendant quirks and foibles, I still prefer it to the others.)

BZT

Posted by: chat Nov 23, 2009 @ 03:20

Tabbed browsing is indeed the most important but its old now and is supported by many browsers. The best unique part of Firefox is add-ons. I can't even think of browsing without AdBlock Plus or [HTML_REMOVED]Web Developer[HTML_REMOVED] toolbar or Mail Notifier. Chrome seems like a good browser but they can't win me over till they have the add-on support as Firefox.

Posted by: sohbet Nov 23, 2009 @ 03:23

thanks you

Posted by: free games Nov 27, 2009 @ 00:22

Wonderful post! THanks

Posted by: online games Nov 27, 2009 @ 23:42

Yes I can make a web service who automatically send fortune cookies. But I don't understand the reply thing you want.

Posted by: gmt master ii Dec 07, 2009 @ 23:59

Wonderful post! Thanks

Posted by: sell ugg ugg boots online ugg boots Dec 16, 2009 @ 18:51

1 Chestnut UGG Bailey Button Boots
2 Rose UGG Classic Cardy Boots 3 Sand UGG Classic Mini Boots 4 UGG Classic Argyle Knit whit 5879 5 UGG 5822 Stripe Cable Knit Chocolate White 6 Chestunt Genuine Australia classic Tall 7 Sand UGG Ultra Shotr Boots 8 Chestnut UGG Sundance Boots



Posted by: links of london Dec 20, 2009 @ 23:09

Early morning time, the mist is dim. Curved river bank, willow obviously elegant like smoke. Far does not see the mountain, nearly does not see the ship, only a little selects the lights, embellishes in the faintly recognizable mist. The young young schoolchild, sings in this mist at sunset; In a hurry the pedestrian, the moist hair clothing which fills by the mist. Most beautiful, when is in the fog the park, pavilion Taiwan pavilion, porch bridge park, fuzzy, partly visible. Let the human think that imitates, if places oneself in unreal, is considering, is in the world, plays in the heavenly palace?

Posted by: film izle Dec 23, 2009 @ 17:20

thanks for info

Posted by: flowers san antonio Dec 27, 2009 @ 01:46

this is great

Posted by: fake rolex watches Jan 04, 2010 @ 23:31

Welcome to our website,and you will enjoy the sunshine service. Let's

abandon all the worry,and enjoy the happy shopping travel.All the

replica watches paypal and replica watches are most in fashion

boutique this year.rolex replica and rolex replica paypal and the

replica rolex paypal show your chaming temperament. In addtin, the

fake watches paypal and the other fake rolex paypal make you mold

your unique glamour this winter.The swiss rolex replica have the high

quality but low price. replica watch paypal and replica rolex have

all the style and cheapest price replica rolex watches. Enjoying the

fun of net purchase! replica rolex We will supply you the perfect

service and after sale support system. Our honest will bring you

absolute satisfaction.

Posted by: ugg boots Jan 07, 2010 @ 04:12

ugg bootsugg bootsugg bootsugg bootsugg bootsugg boots

Posted by: china wholesale Jan 14, 2010 @ 00:15

Here is the richest in the world and you can find your needs bring you harvest yes there

is no other better place than here here you are with my natural beauty but also naturally

pure green world our homes a share in the bar.God bless you! www.trademic.com

Posted by: film izle Jan 17, 2010 @ 04:44

Thanks for your good website and for sharing your experiences.

Posted by: divx film izle Jan 17, 2010 @ 04:45

Thank you for sharing your friends. Hope to see you another day.

Posted by: links of london Jan 19, 2010 @ 00:19

rolex replica and rolex replica paypal and the

replica rolex paypal show your chaming temperament.

Posted by: replica watches Jan 19, 2010 @ 19:19

Highest quality replica rolex will to meet your expectation.replica rolex watches are made from the highest quality parts and crafted with such attention to detail.We offer discount price for you. When you order replica rolex watches at our site

Posted by: uvip33 Feb 05, 2010 @ 22:14

That’s something,That's what I was thinking.Brilliant idea.

Posted by: evening dresses Feb 19, 2010 @ 06:03

I am sure a lot of people will benefit from it. Thanks!

Posted by: Nike Air Max Feb 22, 2010 @ 23:11

Very creative, one of the nicer sites I have seen today. Keep up the great work.

Posted by: Nike Air Max Mar 04, 2010 @ 00:17

So beautiful sharing!Thank you very much.

Posted by: tag heuer watch Mar 04, 2010 @ 23:13

can show a man not only his wealthy but also his high taste.tag heuer watch As the society developed, many people are pay more attention to one's wearing than before, especially a watch. tag heuer watch If you are wearing a famous brand watch, people will look up you, also will love to do business with you. You will be a successful man with a such luxury fashion watch.

Posted by: Nike Air Max Mar 11, 2010 @ 18:58

Best wishes!Your blog is very good!

Posted by: omega moon watch Mar 11, 2010 @ 20:31

I recently came across your blog and have been reading along. I thought I would leave my first comment. I don't know what to say except that I have enjoyed reading. Nice blog. I will keep visiting this blog very often.

Leave a message

(Required)
(Required and not displayed)
(Optional)
obfuscated letters Enter the text shown in the image