def _mix_random(api, moxer): moxer.StubOutWithMock(Mixer, "mix_random_limit_sources") sources = [ MockEntity(key_name=str(i), name="source/%d" % i) for i in xrange(2) ] content = "so, wtf is going on here" Mixer.mix_random_limit_sources(2, degrade=True).AndReturn( (sources, content))
def mixer_stub(moxer): mixer_mock = moxer.CreateMock(Mixer) # moxer.StubOutWithMock(Mixer, '__init__') # Mixer.__init__(mox.IgnoreArg()) moxer.StubOutWithMock(Mixer, '__new__') Mixer.__new__(Mixer, mox.IgnoreArg()).AndReturn(mixer_mock) return mixer_mock
def __direct_response(self, directs): direct = None for d in directs: logging.info("found direct message %s [%s %s] %s" % (d.id, d.created_at, d.sender_screen_name, d.text)) response = TwitterResponseAccessor.get_by_message_id(str(d.id)) if not response: direct = d break else: logging.debug("found response to direct message %s" % d.id) sent_message = None if direct: speaker = self.__select_speaker() sources, response_text = Mixer(speaker).mix_response(direct.text, min_results=1) logging.info("responding to direct message %s %s" % (direct.id, response_text)) sent_message = self.__twitter.PostDirectMessage( direct.sender_screen_name, response_text) self.__reporter.posted(response_text) TwitterResponseAccessor.create(str(direct.id), response_id=str(sent_message.id), user=direct.sender_screen_name) return sent_message
def __mix(self): speaker = self.__select_speaker() sources, content = Mixer(speaker).mix_random_limit_sources( 2, degrade=True) logging.debug("mixing random tweet: {%s} %s" % (";".join([s.name for s in sources]), content)) return sources, content
def test_mix_response_mix_contents_fails(self): m = mox.Mox() m.StubOutWithMock(ArtifactAccessor, "search") # m.StubOutWithMock(Mixer, "random_resource") m.StubOutWithMock(Mixer, "mix_random_limit_sources") inquiry = "elephants" # content should be short enough to make SentenceSpeaker fail ArtifactAccessor.search("elephants").AndReturn(tuple(e for e in MockQuery(xrange(1), create_call=lambda id: _content(id, 0)))) Mixer.mix_random_limit_sources(1).AndReturn(self.__default_mix_results()) #Mixer.random_resource().AndReturn(MockEntity(key_name="failblog", url="http://failblog.org")) m.ReplayAll() sources, text = new_default_mixer().mix_response(inquiry) m.VerifyAll()
def test_mix_response_no_search_results(self): m = mox.Mox() m.StubOutWithMock(ArtifactAccessor, "search") # m.StubOutWithMock(Mixer, "random_resource") m.StubOutWithMock(Mixer, "mix_random_limit_sources") inquiry = "elephants" # no search results returned ArtifactAccessor.search("elephants").AndReturn(()) Mixer.mix_random_limit_sources(1).AndReturn(self.__default_mix_results()) # Mixer.random_resource().AndReturn(MockEntity(key_name="failblog", url="http://failblog.org")) m.ReplayAll() sources, text = new_default_mixer().mix_response(inquiry) m.VerifyAll()
def test_mix_response_no_search_results(self): m = mox.Mox() m.StubOutWithMock(ArtifactAccessor, "search") # m.StubOutWithMock(Mixer, "random_resource") m.StubOutWithMock(Mixer, "mix_random_limit_sources") inquiry = "elephants" # no search results returned ArtifactAccessor.search("elephants").AndReturn(()) Mixer.mix_random_limit_sources(1).AndReturn( self.__default_mix_results()) # Mixer.random_resource().AndReturn(MockEntity(key_name="failblog", url="http://failblog.org")) m.ReplayAll() sources, text = new_default_mixer().mix_response(inquiry) m.VerifyAll()
def speak(request, raw_sources): sources = None if raw_sources: sources = sources.split(";") if ";" in raw_sources else (raw_sources,) q = request.REQUEST.get("q", None) if q: mix_sources, mix_content = Mixer.mix_after_search(urllib.unquote(q)) else: if sources: mix_sources, mix_content = Mixer.mix_sources(*sources) else: mix_sources, mix_content = Mixer.mix_random() logging.debug("mix_content: %s" % mix_content) c = Context(dict(sources=mix_sources, content=mix_content)) t = loader.get_template("speak.html") return HttpResponse(t.render(c))
def test_mix_response_mix_contents_fails(self): m = mox.Mox() m.StubOutWithMock(ArtifactAccessor, "search") # m.StubOutWithMock(Mixer, "random_resource") m.StubOutWithMock(Mixer, "mix_random_limit_sources") inquiry = "elephants" # content should be short enough to make SentenceSpeaker fail ArtifactAccessor.search("elephants").AndReturn( tuple(e for e in MockQuery(xrange(1), create_call=lambda id: _content(id, 0)))) Mixer.mix_random_limit_sources(1).AndReturn( self.__default_mix_results()) #Mixer.random_resource().AndReturn(MockEntity(key_name="failblog", url="http://failblog.org")) m.ReplayAll() sources, text = new_default_mixer().mix_response(inquiry) m.VerifyAll()
def get(self, **kw): helper = RequestHelper(self) message = self.request.get("q", None) if not message: helper.error(400, "q must be provided") return message = urllib.unquote(message) try: sources, response = Mixer(new_speaker()).mix_response(message) logging.debug("sources:%s, response:%s" % (sources, response)) result = dict(response=response) helper.write_json(result) except DataException, ex: helper.error(503, Exceptions.format(ex)) logging.error(ex)
def get(self, source_name, **kw): helper = RequestHelper(self) try: speaker_name = self.request.get("s", None) if speaker_name: speaker = new_speaker(speaker_name)[1] mixer = Mixer.new(speaker) else: speaker_name, speaker = new_random_speaker() logging.debug("speaker: %s" % str(speaker)) mixer = Mixer(speaker) # direct message message = self.request.get("q", None) if message: message = urllib.unquote(message) sources, content = mixer.mix_response(message) else: if not source_name: sources, content = mixer.mix_random_limit_sources( 2, degrade=True) else: source_name = urllib.unquote(source_name) logging.debug("get source_name: %s" % source_name) if ";" in source_name: # multiple sources sources_split = set(source_name.split(";")) sources, content = mixer.mix_sources(*sources_split) else: # single source sources, content = mixer.mix_sources(source_name) logging.debug("sources: %s" % str(sources)) source_hash_list = [s.name for s in sources] mix_hash = { "sources": source_hash_list, "speaker": { "name": speaker_name, "id": hash(speaker), "details": str(speaker) }, "body": content } helper.write_json(mix_hash) except NotFoundException, ex: helper.error(404, Exceptions.format(ex)) logging.error(ex)
def __public_response(self, messages): message = None for m in messages: pretty_message = "%s [%s %s] %s" % (m.id, m.created_at, m.user.screen_name, m.text) logging.info("found public message: %s" % pretty_message) if not self.__analyzer.should_respond(m): logging.info("not responding") continue response = TwitterResponseAccessor.get_by_message_id(str(m.id)) if not response: message = m break else: logging.debug("found response to public message %s" % m.id) sent_message = None if message: # TODO: search for username also username = message.user.screen_name parsed_tweet = parse_tweet(message.text) plain_tweet = parsed_tweet.plain_text speaker = self.__select_speaker() sources, mix = Mixer(speaker).mix_response(plain_tweet, min_results=1, max_length=130 - len(username)) response_text = "@%s %s" % (username, mix) logging.info("responding to public message %s: %s" % (message.id, response_text)) sent_message = self.__twitter.PostUpdate(response_text, message.id) TwitterResponseAccessor.create(str(message.id), response_id=str(sent_message.id), user=username, tweet_type=TwitterResponse.MENTION) self.__reporter.posted(response_text) return sent_message
def get(self, source_name, **kw): helper = RequestHelper(self) try: speaker_name = self.request.get("s", None) if speaker_name: speaker = new_speaker(speaker_name)[1] mixer = Mixer.new(speaker) else: speaker_name, speaker = new_random_speaker() logging.debug("speaker: %s" % str(speaker)) mixer = Mixer(speaker) # direct message message = self.request.get("q", None) if message: message = urllib.unquote(message) sources, content = mixer.mix_response(message) else: if not source_name: sources, content = mixer.mix_random_limit_sources(2, degrade=True) else: source_name = urllib.unquote(source_name) logging.debug("get source_name: %s" % source_name) if ";" in source_name: # multiple sources sources_split = set(source_name.split(";")) sources, content = mixer.mix_sources(*sources_split) else: # single source sources, content = mixer.mix_sources(source_name) logging.debug("sources: %s" % str(sources)) source_hash_list = [s.name for s in sources] mix_hash = {"sources": source_hash_list, "speaker": {"name": speaker_name, "id": hash(speaker), "details": str(speaker)}, "body": content} helper.write_json(mix_hash) except NotFoundException, ex: helper.error(404, Exceptions.format(ex)) logging.error(ex)
def _mix_random(api, moxer): moxer.StubOutWithMock(Mixer, "mix_random_limit_sources") sources = [MockEntity(key_name=str(i), name="source/%d" % i) for i in xrange(2)] content = "so, wtf is going on here" Mixer.mix_random_limit_sources(2, degrade=True).AndReturn((sources, content))
def new_default_mixer(): # return Mixer.new(new_speaker()[1]) return Mixer(PhraseSpeaker())
def new_default_mixer(): return Mixer(new_speaker())