Archive for the 'Programming' Category

twitpie: Twitter Commandline Client

Jun 16, 2008 in Internet, Programming, Python

A simple need, solved in a simple way - a twitter client for those wanting command-line access and auto-refresh + posting tweets from the same place. It does not implement anything fancy. Just the basic features.

You can choose to expand on the source code and/or leave comments so that I can update it and let everyone else also enjoy a better client.

#!/usr/bin/env python
#
#           twitpie
#
#           Twitter Command Line Client
#
#           A simplistic client useful to get a auto-refreshed feed of your
#           + friends timeline and to post tweets.
#
#
# Author:   Harshad Sharma
#           http://www.twitter.com/hiway
#           harshad (dot) sharma @ gmail (dot) com
#
#
# Usage:    Edit this file --> put in username and password
#           Start program "python twit.py" - it will automatically start showing
#           the latest tweets. To update or quit, press Ctrl+C
#           Type in a tweet and press enter to post the update.
#           simply type 'q' to quit.
#
# Dependency:
#           http://pypi.python.org/pypi/python-twitter/
#
# License:
#           This program is free software: you can redistribute it and/or modify
#           it under the terms of the GNU General Public License as published by
#           the Free Software Foundation, either version 3 of the License, or
#           (at your option) any later version.
#
#           This program is distributed in the hope that it will be useful,
#           but WITHOUT ANY WARRANTY; without even the implied warranty of
#           MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#           GNU General Public License for more details.
#
#           You should have received a copy of the GNU General Public License
#           along with this program.  If not, see .

import twitter
import time

# Add your username and password here…
api = twitter.Api(username=’twitterusername’, password=’twitterpassword’)     # <--- EDIT HERE

version = 0.1
finished = False

# Loop till finished is changed to True
while finished == False:
    try:
        # Create an empty list.
        tweets = []

        # iterate throgh the friends timeline and gather data.
        for i in api.GetFriendsTimeline():

            #-------- Choose your fave formatting: --------
            # keep only one line uncommented at a time.

            #tweets.append( "%s (%s) \n%s\n" %(i.user.name, i.relative_created_at, i.text))
            #tweets.append( "%s | %s (%s)\n" %(i.user.name, i.text, i.relative_created_at))
            #tweets.append( "(%s) %s" %(i.user.name, i.text))
            #tweets.append( "%s: %s" %(i.user.name, i.text))
            tweets.append( "%s\n%s\n" %(i.user.name, i.text))

        # Since we want the latest tweets to appear near the prompt instead of
        # scrolling up... (if you want it the other way round, comment the line)
        tweets.reverse()

        # Iterate over the list and print
        for tweet in tweets:
            print tweet

        # Wait for some time...
        time.sleep(3*60) # 3*60 = 3 minutes

    except KeyboardInterrupt: # If user presses Ctrl-C, do the following:

        # Get imput - either tweet or command to exit.
        tweet = raw_input("\n\nWhat are you doing (q=quit | r=refresh): ")

        if tweet.lower() == 'q':
            # Exit loop by setting the flag
            finished = True
        if tweet.lower() == 'r':
            pass
        else:
            # User wants to post a tweet... do it :-)
            api.PostUpdate(tweet)

Artificial Intelligence + Artificial Emotions

May 17, 2008 in Computers, Programming, Python, news

Back in 2003 I was fascinated by a story published in the Reader’s Digest book called “How did it Really Happen?”. It was of the Russian Grand Duchess, Anastasia Romanova. In short, princess Ana. When I researched on the Internet about who she was like, a lot of references came up, some documentation was available and a lot of fan sites were ready to tell how it must have been to know the young princess in person.

An idea took root - I was already creating small chat-bots, software which imitates human conversations, after learning a few tricks from ALICE. My addition to the trick was simple - instead of complex parsing mechanisms was using simple SQL statements to emulate the basic functioning of an AI chat-bot.

I wanted to go a step further and add emotion to my software; however artificial it may be. My attempts either failed or I gave up a bit too soon. This idea was a skeleton in my closet that just wouldn’t keep quiet. It was feeling sad that I had neglected it. Machines could be much more interesting if they had some emotions… I always wondered how it would feel to make a machine happy, angry or sad… and live with the consequences!

Fortunately inspiration came again when a bunch of Indian techies decided to gather up in an IRC channel to discuss things. We wanted a ‘bot’ to manage user permissions and to log parts of the conversation. That was fulfilled pretty soon, but I was getting the itch to write a bot that listened to people’s conversations and would chip in with helpful hints if someone asked a question that was frequently answered.

I made the software and was testing it… but I wasn’t happy… wanted to do something else with it. I realized that my old project was taking over me again. I decided to give in and challenged myself to start and create the first prototype of the software within 32 hours. Technically I failed again - When I was about 8 hours into the project, another project that I had worked on needed some urgent modifications. That resulted in 24 hours lost. Continued after that and managed to finish the project within next 24 hours.

Finally after almost five years of thinking about an idea, I had it in my hands! I was searching for a name fo rthe software, soemthing to identify it with. After going through baby-names lists, and checking username availability and trying to find the easiest name to spell and type, I finally settled for “Hema”, its an Indian name which means “golden”.

Now, Hema can understand when you are praising her, or when you call her stupid… and her mood changes accordingly. She even stops saying the cheerful “Hey!” if you say “Hi!” when she’s in a bad mood. Its fun to cheer her up and get her talking again.

I’m working on some documentation and introduction/demo that I can present to all of you. I currently have a lot of commitments that demand priority treatement. Like always, the pet project must wait a bit more.

Updates about Hema will be posted on this blog and on my twitter account, so if you are interested, watch these spaces.

Catch ya’ll soon…

Another Python Post

Jul 19, 2006 in Python

Congrats to python! As i type this, a program is being written. Once i finish writing it, i will save the file, open it up in the mobile phone’s file browser and select to install it as a Python script. Then exit the browser and fire up the Python runtime environment. Select to run a script and execute this program. It will connect to wordpress via xmlrpc and post this as a clog entry.

I find this exciting!

Tools used: python for series 60, easyedit, wordpresslib, few extra libraries from python 2.4 installation on my desktop, that were transferred to the mobile.

Now I need to write a gui frontend to this script soon…

Python Post

Jul 19, 2006 in Python

If you see this, posting to wordpress from Python works really easily ;)