diff --git a/server/config.py b/server/config.py new file mode 100644 index 0000000..351214e --- /dev/null +++ b/server/config.py @@ -0,0 +1,6 @@ +twitter_conf = { +'TW_CONSUMER_KEY' : 'env', +'TW_CONSUMER_SECRET' : 'env', +'TW_ACCESS_TOKEN' : 'env', +'TW_ACCESS_TOKEN_SECRET' : 'env', +} diff --git a/server/server.py b/server/server.py new file mode 100644 index 0000000..b2aa277 --- /dev/null +++ b/server/server.py @@ -0,0 +1,104 @@ +#!/usr/bin/python + +import os, sys +import logging +import argparse +import eventlet +import tweepy +import config +from telnetsrv.evtlet import TelnetHandler, command +#from str import join + +for key in config.twitter_conf: + if config.twitter_conf[key] == 'env': + try: + os.environ[key] + except KeyError: + print "Please, set the environment variable %s or set it in the config" % key + sys.exit(1) + +logging.getLogger('').setLevel(logging.DEBUG) + +TELNET_IP_BINDING = '' #all + +parser = argparse.ArgumentParser( description='Run a fttp server.') +parser.add_argument( 'port', metavar="PORT", type=int, help="The port on which to listen on." ) +console_args = parser.parse_args() + +TELNET_PORT_BINDING = console_args.port + +class TwitterApi(): + '''Twitter Api''' + + auth = tweepy.OAuthHandler(config.twitter_conf['TW_CONSUMER_KEY'], config.twitter_conf['TW_CONSUMER_SECRET']) + auth.set_access_token(config.twitter_conf['TW_ACCESS_TOKEN'], config.twitter_conf['TW_ACCESS_TOKEN_SECRET']) + api = tweepy.API(auth) + + def send(tweet): + api.update_status(tweet) + + def get_tweets_by_tag(tag): + print "NotImplemented" + + +class TestTelnetHandler(TelnetHandler): + + # -- Override items to customize the server -- + + WELCOME = 'You have connected to the fttp server.' + PROMPT = "fttp% " + authNeedUser = False + authNeedPass = False + + twitter = TwitterApi() + + def session_start(self): + '''Called after the user successfully logs in.''' + + def session_end(self): + '''Called after the user logs off.''' + + def writeerror(self, text): + '''Called to write any error information (like a mistyped command). + Add a splash of color using ANSI to render the error text in red. + see http://en.wikipedia.org/wiki/ANSI_escape_code''' + TelnetHandler.writeerror(self, "\x1b[91m%s\x1b[0m" % text ) + + # -- Twitter Commands -- + + @command(['tweet', 'send']) + def command_tweet(self, params): + ''' + Sends the message as a tweet from @USR + ''' + message = " ".join(params) + print type(message) + self.twitter.send(message) + + +if __name__ == '__main__': + Handler = TestTelnetHandler + + class EvtletStreamServer(object): + def __init__(self, addr, handle): + self.ip = addr[0] + self.port = addr[1] + self.handle = handle + + def serve_forever(self): + eventlet.serve( + eventlet.listen((self.ip, self.port)), + self.handle + ) + + # Multi-eventlet server + server = EvtletStreamServer( + (TELNET_IP_BINDING, TELNET_PORT_BINDING), + Handler.streamserver_handle + ) + + logging.info("Starting fttp server at port %d. (Ctrl-C to stop)" % (TELNET_PORT_BINDING) ) + try: + server.serve_forever() + except KeyboardInterrupt: + logging.info("Server shut down.")