Skip to content

Instantly share code, notes, and snippets.

@jcbozonier
Forked from anonymous/gist:f77b3da951681d3d2528
Last active August 29, 2015 14:15
Show Gist options
  • Save jcbozonier/7da348d57a1dcb8acbfe to your computer and use it in GitHub Desktop.
Save jcbozonier/7da348d57a1dcb8acbfe to your computer and use it in GitHub Desktop.
TCP server that streams currently tweeting twitter handles of those around NYC for use via Apache Spark. Code HEAVILY borrowed from http://kmkeen.com/socketserver/ Made to work with Spark by Justin Bozonier
import SocketServer, subprocess, sys
from threading import Thread
from TwitterAPI import TwitterAPI
import json, unidecode
api = TwitterAPI(consumer_key, consumer_secret, access_token_key, access_token_secret)
HOST = 'localhost'
PORT = 9999
class SingleTCPHandler(SocketServer.BaseRequestHandler):
"One instance per connection. Override handle(self) to customize action."
def handle(self):
# self.request is the client connection
print "New handler!"
#data = self.request.recv(1024) # clip input at 1Kb
r = api.request('statuses/filter', {'locations':'-74,40,-73,41'}) # NYC Lat/Long
for item in r:
if 'user' in item and 'screen_name' in item['user']:
message = unidecode.unidecode(item['user']['screen_name']).strip()
self.request.send(message + '\n')
print "Sent!"
self.request.close()
class SimpleServer(SocketServer.ThreadingMixIn, SocketServer.TCPServer):
# Ctrl-C will cleanly kill all spawned threads
daemon_threads = True
# much faster rebinding
allow_reuse_address = True
def __init__(self, server_address, RequestHandlerClass):
SocketServer.TCPServer.__init__(self, server_address, RequestHandlerClass)
if __name__ == "__main__":
server = SimpleServer((HOST, PORT), SingleTCPHandler)
# terminate with Ctrl-C
try:
server.serve_forever()
except KeyboardInterrupt:
sys.exit(0)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment