This post focuses on providing very basic information about twitter client API for python. I have written a python application that retrieves my tweets and submits my tweet after authentication.
you can use normal http python library 'urllib' to achieve this task. for example
conn_string = "http://%s:%s@twitter.com/statuses/update.json" % (usr,pwd)
tweet = urllib.urlencode({"status":tweet})
f = urllib.urlopen(constring, tweet)
or you can use 'twitter' python package provided here http://code.google.com/p/python-twitter/
Following is a very simple code that demostrate my initial use case.
import twitter
import sys
def getTwitterClient(option, args={}):
if ( option == 'simple' ):
return twitter.Api()
else:
return twitter.Api(username=args['username'], password=args['password'])
if ( sys.argv[1]=='simple' ):
username = sys.argv[2]
client = getTwitterClient('simple');
latest_posts = client.GetUserTimeline( username )
#prints your latest posts on twitter
print [x.text for x in latest_posts]
elif ( sys.argv[1] == 'upd' ):
username = sys.argv[2]
passwd = sys.argv[3]
client = getTwitterClient('auth', {'username': username, 'password': passwd})
update = client.PostUpdate('Today, neuGrid first demo went fine. Our services performed well as expected')
print update
after saving this code in a anyname.py file. you can call it by using following sample commands.
#to retrieve only the posts
python anyname.py simple <USERNAME>
change <USERNAME> with your twitter username.
#to update your twitter status
python anyname.py upd YOUR_USERNAME YOUR_PASSWORD
you can use normal http python library 'urllib' to achieve this task. for example
conn_string = "http://%s:%s@twitter.com/statuses/update.json" % (usr,pwd)
tweet = urllib.urlencode({"status":tweet})
f = urllib.urlopen(constring, tweet)
or you can use 'twitter' python package provided here http://code.google.com/p/python-twitter/
Following is a very simple code that demostrate my initial use case.
import twitter
import sys
def getTwitterClient(option, args={}):
if ( option == 'simple' ):
return twitter.Api()
else:
return twitter.Api(username=args['username'], password=args['password'])
if ( sys.argv[1]=='simple' ):
username = sys.argv[2]
client = getTwitterClient('simple');
latest_posts = client.GetUserTimeline( username )
#prints your latest posts on twitter
print [x.text for x in latest_posts]
elif ( sys.argv[1] == 'upd' ):
username = sys.argv[2]
passwd = sys.argv[3]
client = getTwitterClient('auth', {'username': username, 'password': passwd})
update = client.PostUpdate('Today, neuGrid first demo went fine. Our services performed well as expected')
print update
after saving this code in a anyname.py file. you can call it by using following sample commands.
#to retrieve only the posts
python anyname.py simple <USERNAME>
change <USERNAME> with your twitter username.
#to update your twitter status
python anyname.py upd YOUR_USERNAME YOUR_PASSWORD
No comments:
Post a Comment