<filename>twitter-producer/twitter-producer.py
#!/usr/bin/python
import json, os
import cfworker
from twython import TwythonStreamer
import pika

#amqp_uri = json.loads(os.environ['VCAP_SERVICES'])['p-rabbitmq'][0]['credentials']['uri']

amqp_uri = os.getenv('RABBITMQ_URI')

connection = pika.BlockingConnection( pika.URLParameters(amqp_uri) )
channel = connection.channel()
channel.queue_declare(queue=os.getenv('TASK_QUEUE'), durable=True)

class TwitterProducer(TwythonStreamer):

    def on_success(self, data):
	message = data['text'].encode('utf-8')
	print "[twitter-producer] Sending %s" % message
        channel.basic_publish(exchange='',routing_key=os.getenv('TASK_QUEUE'), body=message, 
		properties=pika.BasicProperties(
			delivery_mode = 2 #make message persist
		))

    def on_error(self, status_code, data):
        print status_code


if __name__=='__main__':

	""" Leave this as is if deploying to CF, otherwise choose
	a port for local deployment (e.g. 8080) """
	worker = cfworker.cfworker( port=int(os.getenv('PORT')) )
	worker.start()

	""" These variables can be set in your environment (ideal), or
	hardcoded into the function call """
	stream = TwitterProducer(os.getenv('APP_KEY'), os.getenv('APP_SECRET'),
       	            os.getenv('OAUTH_TOKEN'), os.getenv('OAUTH_TOKEN_SECRET') )

	""" You can also set an environment variable to choose what to track, or 
	hard code a list here """
	stream.statuses.filter( track=os.getenv('TRACK') )

	""" stops the HTTP server """
	worker.stop()

<filename>twitter-producer/twitter-producer.py
#!/usr/bin/python
import json, os
import cfworker
from twython import TwythonStreamer
import pika

#amqp_uri = json.loads(os.environ['VCAP_SERVICES'])['p-rabbitmq'][0]['credentials']['uri']

amqp_uri = os.getenv('RABBITMQ_URI')

connection = pika.BlockingConnection( pika.URLParameters(amqp_uri) )
channel = connection.channel()
channel.queue_declare(queue=os.getenv('TASK_QUEUE'), durable=True)

class TwitterProducer(TwythonStreamer):

    def on_success(self, data):
	message = data['text'].encode('utf-8')
	print "[twitter-producer] Sending %s" % message
        channel.basic_publish(exchange='',routing_key=os.getenv('TASK_QUEUE'), body=message, 
		properties=pika.BasicProperties(
			delivery_mode = 2 #make message persist
		))

    def on_error(self, status_code, data):
        print status_code


if __name__=='__main__':

	""" Leave this as is if deploying to CF, otherwise choose
	a port for local deployment (e.g. 8080) """
	worker = cfworker.cfworker( port=int(os.getenv('PORT')) )
	worker.start()

	""" These variables can be set in your environment (ideal), or
	hardcoded into the function call """
	stream = TwitterProducer(os.getenv('APP_KEY'), os.getenv('APP_SECRET'),
       	            os.getenv('OAUTH_TOKEN'), os.getenv('OAUTH_TOKEN_SECRET') )

	""" You can also set an environment variable to choose what to track, or 
	hard code a list here """
	stream.statuses.filter( track=os.getenv('TRACK') )

	""" stops the HTTP server """
	worker.stop()

#!/usr/bin/python #amqp_uri = json.loads(os.environ['VCAP_SERVICES'])['p-rabbitmq'][0]['credentials']['uri'] #make message persist Leave this as is if deploying to CF, otherwise choose 	a port for local deployment (e.g. 8080) These variables can be set in your environment (ideal), or 	hardcoded into the function call You can also set an environment variable to choose what to track, or  	hard code a list here stops the HTTP server