All pastes #1725464 Raw Edit

Twitter-Mention

public python v1 · immutable
#1725464 ·published 2009-12-24 00:16 UTC
rendered paste body
import urllib, jsonimport datetimeimport timeimport codecsfrom pprint import pprint## Constantresult_store = 'stat.txt'keyword_store = 'text.txt'search_base = 'http://search.twitter.com/search.json'rpp = '&rpp=' + '100'FILE = codecs.open(keyword_store,"r","utf-8")keywords = FILE.readlines()for keyword in keywords:	keyword = keyword.rstrip('\n').encode('utf-8')	key_query = '?q=' + keyword	## Compose URL + get search result	doloop = 1	pagenum = 1	count = 0	## Matching Loop	while (doloop == 1):		page = '&page=' + str(pagenum)		url = search_base + key_query + rpp + page#		print url		json_file = urllib.urlopen(url).read()		json_data = json.loads(json_file)#		print ("%d %d" % (pagenum, count))		if ('results' in json_data):			search_result = json_data['results']			before_time = time.strptime("23 Dec 2009 14:00:00", "%d %b %Y %H:%M:%S")			after_time = time.strptime("23 Dec 2009 16:00:00", "%d %b %Y %H:%M:%S")			for item in search_result:				item_time = item['created_at']				item_time = time.strptime(item_time[0:25], "%a, %d %b %Y %H:%M:%S")				if (item_time > before_time) & (item_time < after_time):					count = count + 1				if (item_time < before_time):					doloop = 0			pagenum = pagenum + 1		else:			doloop = 0	FILE = open(result_store, "a")	FILE.writelines('%s\t%d\n' % (keyword, count))