def _parse_stream(self, bot): stream = JSONStream(self.flow_user_api_key) gen = stream.fetch([self.channel], active=True) for data in gen: process_message = type(data) == dict and (data['event'] == "message" or data['event'] == "comment") if process_message and ("user" in data and self.user != data["user"]): self.spoken = False bot_input = BotInput() if type(data['content']) is dict: bot_input.message = data["content"]['text'] elif "content" in data: bot_input.message = data["content"] else: break if "user" in data and int(data["user"]) > 0: try: bot_input.nick = self.get_user_by_id(data["user"])["nick"] self.user_id = data["user"] if random.random() < (self.chattiness / 100): logger.log("Randomly sending message to %s" % bot_input.nick) self.private_message(data["user"], random.choice(self.responses["private_messages"])) except Exception as e: logger.error(e) self.say(bot.responses["stranger"]) elif "external_name" in data: bot_input.nick = data["external_name"] else: bot_input.nick = "anonymous" bot_input.bot = bot self.user_nick = bot_input.nick marvin.process(bot_input, self)
try: # Raises StopIteration quote = qdb_parser.getRandomQuote() except: try: # Who knows what could happen here? Connection issues, parser issues... qdb_parser.parse(requests.get(random_qdb)) quote = qdb_parser.getRandomQuote() except BaseException as e: quote = 'Failed to find something funny. Whoops.' print 'QDB Error: <%s>: %s' % (type(e).__name__, e) return quote stream = JSONStream(api_token) gen = stream.fetch(fetch_flows) for data in gen: if isinstance(data, dict): if data.get('event','') == 'message': thread_id = data.get('thread_id', None) flow_name = 'main' user_name = 'FlowBot' command = '' command_args = '' message = '' try: command = data.get('content','')[1:]
def main(): stream = JSONStream(FLOW_USER_API_KEY) gen = stream.fetch(Flows_to_fetch) for data in gen: if type(data) == dict and data['event'] == "message": process_data(data)