Posts

Showing posts with the label twitter api

Python code to search tweets in twitter

Recently I wrote a code to search keywords in twitter and extract the tweets as a part of a larger program. It uses twitter search api . I guess some of you might find this code snippet useful. :) import requests import sys import time from datetime import datetime def store_tweets(keyword, results, max_id): tweet_list = [] for item in results: tweet_id = item['id_str'] tweet_text = item['text'].encode('utf-8', 'ignore') if len(tweet_text) > 255: tweet_text = tweet_text[0:255] from_user = item['from_user'].encode('utf-8', 'ignore') from_user_id = item['from_user_id'] from_user_name = item['from_user_name'].encode('utf-8', 'ignore') profile_image = item['profile_image_url'].encode('utf-8', 'ignore') created_at = datetime.strptime(item['created_at'][:-6], ...

Update twitter status using Python script

Recently I have written a Python script to update status message in twitter using their api . Let me share the code here: import urllib import urllib2 import base64 import httplib import socket ## authenticate module is used for http basic authentication. found in ' urllib2 missing manual ' def authenticate(username, password): password_mgr = urllib2.HTTPPasswordMgrWithDefaultRealm() top_level_url = 'http://twitter.com/' password_mgr.add_password(None, top_level_url, username, password) handler = urllib2.HTTPBasicAuthHandler(password_mgr) opener = urllib2.build_opener(handler) return opener ## updates twitter status given the status, user name (id or email) and password def update_status(status, user, password): data = {'status' : status} data = urllib.urlencode(data) url = 'http://twitter.com/statuses/update.xml' opener = authenticate(user, password) result = '' try: handle = opener.open(url, da...