Skip to content

Instantly share code, notes, and snippets.

Created February 16, 2015 04:26
Show Gist options
  • Save anonymous/f77b3da951681d3d2528 to your computer and use it in GitHub Desktop.
Save anonymous/f77b3da951681d3d2528 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/
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