Skip to content

Instantly share code, notes, and snippets.

@ravikiranj
Last active August 29, 2015 13:55
Show Gist options
  • Save ravikiranj/8772848 to your computer and use it in GitHub Desktop.
Save ravikiranj/8772848 to your computer and use it in GitHub Desktop.
Twitter Search Demo
get_twitter_data.py
===================
#!/usr/bin/env python
import argparse
import urllib
import urllib2
import json
import os
import oauth2
class TwitterData:
#start __init__
# Uncomment if you need to init something
#def __init__(self):
#end
# Try reading config.json for oauth credentials
def parse_config(self):
config = {}
# from file args
if os.path.exists('config.json'):
with open('config.json') as f:
config.update(json.load(f))
else:
# may be from command line
parser = argparse.ArgumentParser()
parser.add_argument('-ck', '--consumer_key', default=None, help='Your developper `Consumer Key`')
parser.add_argument('-cs', '--consumer_secret', default=None, help='Your developper `Consumer Secret`')
parser.add_argument('-at', '--access_token', default=None, help='A client `Access Token`')
parser.add_argument('-ats', '--access_token_secret', default=None, help='A client `Access Token Secret`')
args_ = parser.parse_args()
def val(key):
return config.get(key)\
or getattr(args_, key)\
or raw_input('Your developper `%s`: ' % key)
config.update({
'consumer_key': val('consumer_key'),
'consumer_secret': val('consumer_secret'),
'access_token': val('access_token'),
'access_token_secret': val('access_token_secret'),
})
# should have something now
return config
def oauth_req(self, url, http_method="GET", post_body=None,
http_headers=None):
config = self.parse_config()
consumer = oauth2.Consumer(key=config.get('consumer_key'), secret=config.get('consumer_secret'))
token = oauth2.Token(key=config.get('access_token'), secret=config.get('access_token_secret'))
client = oauth2.Client(consumer, token)
resp, content = client.request(
url,
method=http_method,
body=post_body or '',
headers=http_headers
)
return content
#start getTwitterData
def getData(self, keyword, params = {}):
maxTweets = 50
url = 'https://api.twitter.com/1.1/search/tweets.json?'
data = {'q': keyword, 'lang': 'en', 'result_type': 'recent', 'count': maxTweets, 'include_entities': 0}
#Add if additional params are passed
if params:
for key, value in params.iteritems():
data[key] = value
url += urllib.urlencode(data)
response = self.oauth_req(url)
jsonData = json.loads(response)
tweets = []
if 'errors' in jsonData:
print "API Error"
print jsonData['errors']
else:
for item in jsonData['statuses']:
tweets.append(item['text'])
return tweets
#end
#end class
demo.py
========
#!/usr/bin/env python
from get_twitter_data import TwitterData
from BaseHTTPServer import BaseHTTPRequestHandler,HTTPServer
import urlparse
tData = TwitterData()
PORT_NUMBER = 9999
#This class will handles any incoming request from
#the browser
class myHandler(BaseHTTPRequestHandler):
#Handler for the GET requests
def do_GET(self):
try :
# Parse URL and query string
parsed_path = urlparse.urlparse(self.path)
# Protect against bad requests
if parsed_path.path.endswith("html"):
params = urlparse.parse_qs(parsed_path.query)
# Extract keyword to search
keyword = ""
if 'q' in params and len(params['q']) > 0:
keyword = params['q'][0]
css = "<style type='text/css'>.result{padding: 10px; border: 1px solid #C0C0C0; border-radius: 8px; width: 800px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap;}</style>"
html = "<html><head>"+css+"<title>Twitter Search Demo</title><body><h1>Twitter Search Demo</h1><div>"
html = html + "<form action='index.html' method='get'><input id='q' name='q' type='text' placeholder='Enter keyword' /><input type='submit' value='Submit' /></form>"
# Get twitter data
tweets = []
if keyword:
tweets = tData.getData(keyword)
html = html + "<h2>Search results for: " + keyword + "</h2>"
# Write out the results to HTML
for t in tweets:
html = html + "<p class='result'>" + t + "</p>"
html = html + "</div></body></html>"
self.send_response(200)
self.send_header('Content-type','text/html')
self.end_headers()
# Send the html message (utf-8 because tweeps use smileys and what not)
self.wfile.write(html.encode('utf-8'))
except Exception as ex:
import sys, os
exc_type, exc_obj, exc_tb = sys.exc_info()
fname = os.path.split(exc_tb.tb_frame.f_code.co_filename)[1]
print(exc_type, fname, exc_tb.tb_lineno)
return
try:
#Create a web server and define the handler to manage the
#incoming request
server = HTTPServer(('', PORT_NUMBER), myHandler)
print 'Started httpserver on port ' , PORT_NUMBER
#Wait forever for incoming http requests
server.serve_forever()
except KeyboardInterrupt:
print '^C received, shutting down the web server'
server.socket.close()
config.json
===========
{
"consumer_key": "YOUR_CONSUMER_KEY",
"consumer_secret": "YOUR_CONSUMER_SECRET",
"access_token": "YOUR_ACCESS_KEY",
"access_token_secret": "YOUR_ACCESS_TOKEN_SECRET"
}
How to run
==========
python demo.py (install modules using easy_install if necessary)
To fill config.json, go to https://dev.twitter.com/apps and create a new application
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment