def on_data(self, data): if not data.strip(): return True print " [x] Got:", data # Decode JSON data item = json.loads(data) # Save data id = self.store.save(item) # Is this item a direct message? a_direct_message = utils.item_a_direct_message(item) # Is this item a mention? a_mention = utils.item_a_mention(item) # Continue processing further down the chain if a_direct_message or a_mention: print " [x] Received", utils.get_screen_name(item), ":", utils.get_text(item) self.channel.basic_publish(exchange='', routing_key=self.amqp_queue, body=str(id), properties=pika.BasicProperties( delivery_mode=2, # make message persistent )) return True
def on_item(self, ch, method, header, body): """ Fires when we receive a new item to decode. """ # Lookup data in store, body should actually be an ObjectId item = self.userstream_store.find_one({"_id": ObjectId(body)}) if utils.item_a_direct_message(item) or utils.item_a_mention(item): text, screen_name = (utils.get_text(item), utils.get_screen_name(item)) print " [x] Received %r from %r" % (text, screen_name) # Any Spotify tracks? tracks = spotify.lookup_tracks(text) if len(tracks) > 0: # Save to playlist for track in tracks: id = self.playlist_store.save({'track':track, 'status':'new', 'source':'twitter', 'from':utils.get_sender(item)}) # Send each track to the broadcaster's 'receive' queue, so it can be broadcast # to all connected clients print " [x] Sending %r to broadcaster" % (track['track']['name'],) self.amqp_primary_channel.basic_publish(exchange='', routing_key=self.amqp_out_queue, body=str(id), properties=pika.BasicProperties( delivery_mode=2, # make message persistent )) # Confirm delivery ch.basic_ack(delivery_tag=method.delivery_tag)