def main(p, mediaOnly=None): """ Coordinates a new twitter stream connection""" # Logging config logFile = os.path.join(p.errorPath, p.connErrorFile) logging.basicConfig(filename=logFile, format="%(levelname)s:: %(asctime)s %(message)s", level=p.logLevel) # The mongo bits try: c, dbh = mdb.getHandle(host=p.dbHost, port=p.dbPort, db=p.db, user=p.dbUser, password=p.dbPassword) evCollHandle = dbh[p.eventsCollection] except: logging.critical("Failed to connect to db and authenticate.", exc_info=True) sys.exit() # Here's the redis queue for managing the tweets as they come in try: q = RedisQueue(p.redisName, host=p.redisHost, password=p.redisPassword, port=p.redisPort, db=0) except: logging.critical("REDIS: Failed to connect in connectionClient.py. ", exc_info=True) sys.exit() # Connection placeholder in case the exception catches the drop out connection = True while connection == True: # Get the existing tags and add the current try: tags = cf.getCurrentTags(evCollHandle) except: tags = None logging.error("Failed to get current tags from db.", exc_info=True) # Build the building boxes try: bboxes = cf.getCurrentBBoxes(evCollHandle) except: bboxes = None logging.error("Failed to get current BBOXes from db.", exc_info=True) if not tags and not bboxes: logging.warning("Currently no tags or bboxes in the db.") sys.exit() try: print tags, bboxes with tweetstream.FilterStream(p.sourceUser, p.sourcePassword, track=tags, locations=bboxes) as stream: for tweet in stream: if mediaOnly: try: q.put(json.dumps(tweet)) except: logging.critical("Failed to put tweet on redis. This tweet: \n%s" % (tweet), exc_info=True) except tweetstream.ConnectionError: logging.critical("Disconnected from twitter", exc_info=True)
class AlertHelper(): q = None def __init__(self, config, rhost=None, rport=None, rdb=None, chatq=None, prefix=None): if config: rhost = config.get("redis", "redis.server").strip('"').strip("'") rport = config.get("redis", "redis.port") rdb = config.get("redis", "redis.db") chatq = config.get("alert_bot", "msg_bot.redis_mq").strip('"').strip("'") prefix = config.get("alert_bot", "msg_bot.redis_prefix").strip('"').strip("'") self.q = RedisQueue(chatq, prefix, rhost, rport, rdb) def post_msg(self, msg): self.q.put(msg) def flush_all(self): i = 0 while not self.q.empty(): self.q.get() i+=1 return i
def enqueueForDownload(url): q = RedisQueue("youlinks") q.put(url) return "URL Enqueued"
class AlertMsgBot(sleekxmpp.ClientXMPP): """ A basic SleekXMPP bot that will log in, send a message, and then log out. """ config = None rsq= None recipients = None quit = False tlock = None def __init__(self, config): self.config = config host = config.get("redis", "redis.server").strip('"').strip("'") port = config.get("redis", "redis.port") db = config.get("redis", "redis.db") qname = config.get("alert_bot", "msg_bot.redis_mq").strip('"').strip("'") qprefix = config.get("alert_bot", "msg_bot.redis_prefix").strip('"').strip("'") self.rsq = RedisQueue(qname, qprefix, host, port, db) logging.info('Connect to redis on server->%s:%s db->%s qname->%s:%s' % (host, port, db, qprefix, qname)) jid = config.get("alert_bot", "msg_bot.jid").strip('"').strip("'") password = config.get("alert_bot", "msg_bot.pass").strip('"').strip("'") sleekxmpp.ClientXMPP.__init__(self, jid, password) self.recipients = eval(config.get("alert_bot", "msg_bot.recipients").strip('"').strip("'")) self.tlock = Lock() # The session_start event will be triggered when # the bot establishes its connection with the server # and the XML streams are ready for use. We want to # listen for this event so that we we can initialize # our roster. self.add_event_handler("session_start", self.start, threaded=True) def start(self, event): """ Process the session_start event. Typical actions for the session_start event are requesting the roster and broadcasting an initial presence stanza. Arguments: event -- An empty dictionary. The session_start event does not provide any additional data. """ self.send_presence() self.get_roster() self.rsq.put("Greetings! The alert bot has just started!") t = threading.Thread(target = self.process_alerts(), args=()) t.start() # self.send_message(mto=self.recipient, # mbody=self.msg, # mtype='chat') def process_alerts(self): self.quit = False while self.quit <> True: if not self.rsq.empty(): msg = self.rsq.get() logging.debug('process_alerts: received msg: {%s}' % msg) self.send_msg(msg) sleep(1) # Using wait=True ensures that the send queue will be # emptied before ending the session. self.disconnect(wait=True) def send_msg(self, msg): self.tlock.acquire() try: for r in self.recipients: self.send_message(r, msg, mtype='chat') finally: self.tlock.release()
def main(p, mediaOnly=None): ''' Coordinates a new twitter stream connection''' # Logging config logFile = os.path.join(p.errorPath, p.connErrorFile) logging.basicConfig(filename=logFile, format='%(levelname)s:: %(asctime)s %(message)s', level=p.logLevel) # The mongo bits try: c, dbh = mdb.getHandle(host=p.dbHost, port=p.dbPort, db=p.db, user=p.dbUser, password=p.dbPassword) evCollHandle = dbh[p.eventsCollection] except: logging.critical('Failed to connect to db and authenticate.', exc_info=True) sys.exit() # Here's the redis queue for managing the tweets as they come in try: q = RedisQueue(p.redisName, host=p.redisHost, password=p.redisPassword, port=p.redisPort, db=0) except: logging.critical("REDIS: Failed to connect in connectionClient.py. ", exc_info=True) sys.exit() # Connection placeholder in case the exception catches the drop out connection = True while connection == True: # Get the existing tags and add the current try: tags = cf.getCurrentTags(evCollHandle) except: tags = None logging.error('Failed to get current tags from db.', exc_info=True) # Build the building boxes try: bboxes = cf.getCurrentBBoxes(evCollHandle) except: bboxes = None logging.error('Failed to get current BBOXes from db.', exc_info=True) if not tags and not bboxes: logging.warning('Currently no tags or bboxes in the db.') sys.exit() try: print tags, bboxes with tweetstream.FilterStream(p.sourceUser, p.sourcePassword, track=tags, locations=bboxes) as stream: for tweet in stream: if mediaOnly: try: q.put(json.dumps(tweet)) except: logging.critical( "Failed to put tweet on redis. This tweet: \n%s" % (tweet), exc_info=True) except tweetstream.ConnectionError: logging.critical("Disconnected from twitter", exc_info=True)
__author__ = 'leena' from redisQueue import RedisQueue q = RedisQueue('test') q.put('hello world')