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 test_act_direct_message(self): moxer = Mox() api = _create_default_mocks(moxer) _build_standard_config(moxer) bundle = _create_actor_and_delegates(api, moxer) actor = bundle.actor direct_id = 1 direct = moxer.CreateMock(twitter.DirectMessage) user = moxer.CreateMock(twitter.User) direct.id = direct_id direct.sender_screen_name = "mikemattozzi" direct.text = "why is blood spattered all over your car?" _return_direct_messages(api, [direct]) post = moxer.CreateMockAnything() post.id = 101 _return_replies(api, ()) TwitterResponseAccessor.get_by_message_id(str(direct_id)) ArtifactAccessor.search("spattered").AndReturn(create_content_list(10)) # response api.PostDirectMessage(direct.sender_screen_name, IgnoreArg()).AndReturn(post) TwitterResponseAccessor.create(str(direct.id), response_id=str(post.id), user=direct.sender_screen_name) post.AsDict().AndReturn({}) moxer.ReplayAll() actor.act() moxer.VerifyAll()
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 test_retweet_not_previously_retweeted(self): moxer = Mox() api = _create_api(moxer) bundle = _create_actor_and_delegates(api, moxer) actor = bundle.actor analyzer = bundle.analyzer status_user = Record(screen_name=self.id()) status_ids = [5, 10, 15] timeline = (Record(id=id, text="epic, fail is epic %s" % id, user=status_user) for id in status_ids) api.GetFriendsTimeline(count=10).AndReturn(timeline) # print "timeline: %s" % [t for t in timeline] # I choose a status to be the one I will retweet not_already_retweeted_id = status_ids[2] moxer.StubOutWithMock(TwitterResponseAccessor, "get_by_message_id") moxer.StubOutWithMock(TwitterResponseAccessor, "create") print "not_already_retweeted_id: %s " % not_already_retweeted_id for status_id in status_ids: print "status_id: %s" % status_id print "%s != %s == %s" % (status_id, not_already_retweeted_id, status_id != not_already_retweeted_id) if status_id != not_already_retweeted_id: # returns a result which means "already retweeted". id is meaningless result = Record(message_id=status_id, response_id=status_id * 10) TwitterResponseAccessor.get_by_message_id( IsA(str)).AndReturn(result) print "returned %s for %s" % (result, status_id) continue # this status is the one I will retweet status_id_str = str(status_id) TwitterResponseAccessor.get_by_message_id(status_id_str) _should_retweet(analyzer) retweet_id = str(status_id + 1) retweet = Record(id=retweet_id, user=self.id()) api.PostRetweet(status_id).AndReturn(retweet) TwitterResponseAccessor.create(status_id_str, response_id=retweet_id, tweet_type=TwitterResponse.RETWEET, user=status_user.screen_name) moxer.ReplayAll() actor.retweet() moxer.VerifyAll()
def test_retweet_not_previously_retweeted(self): moxer = Mox() api = _create_api(moxer) bundle = _create_actor_and_delegates(api, moxer) actor = bundle.actor analyzer = bundle.analyzer status_user = Record(screen_name=self.id()) status_ids = [5, 10, 15] timeline = (Record(id=id, text="epic, fail is epic %s" % id, user=status_user) for id in status_ids) api.GetFriendsTimeline(count=10).AndReturn(timeline) # print "timeline: %s" % [t for t in timeline] # I choose a status to be the one I will retweet not_already_retweeted_id = status_ids[2] moxer.StubOutWithMock(TwitterResponseAccessor, "get_by_message_id") moxer.StubOutWithMock(TwitterResponseAccessor, "create") print "not_already_retweeted_id: %s " % not_already_retweeted_id for status_id in status_ids: print "status_id: %s" % status_id print "%s != %s == %s" % (status_id, not_already_retweeted_id, status_id != not_already_retweeted_id) if status_id != not_already_retweeted_id: # returns a result which means "already retweeted". id is meaningless result = Record(message_id=status_id, response_id=status_id * 10) TwitterResponseAccessor.get_by_message_id(IsA(str)).AndReturn(result) print "returned %s for %s" % (result, status_id) continue # this status is the one I will retweet status_id_str = str(status_id) TwitterResponseAccessor.get_by_message_id(status_id_str) _should_retweet(analyzer) retweet_id = str(status_id + 1) retweet = Record(id=retweet_id, user=self.id()) api.PostRetweet(status_id).AndReturn(retweet) TwitterResponseAccessor.create(status_id_str, response_id=retweet_id, tweet_type=TwitterResponse.RETWEET, user=status_user.screen_name) moxer.ReplayAll() actor.retweet() moxer.VerifyAll()
def retweet(self): statuses = self.__twitter.GetFriendsTimeline(count=10) retweet_source = None for candidate in statuses: logging.debug("candidate: %s" % candidate) # TODO: should query for type=retweet # otherwise I may retweet a mention # checks if status has been retweeted response = TwitterResponseAccessor.get_by_message_id( str(candidate.id)) # if msg was already retweeted, removed from set and let loop continue if not response: logging.debug("not previously retweeted: %s" % candidate.id) retweet_source = candidate else: logging.debug("found retweet for message %s" % response.message_id) continue if self.__analyzer.should_retweet(candidate): retweet_source = candidate break else: logging.debug("not retweeting: %s" % describe_status(candidate)) if not retweet_source: raise DataException("could not find anything to retweet") pretty_retweet = describe_status(retweet_source) logging.debug("retweeting '%s'" % pretty_retweet) retweet = self.__twitter.PostRetweet(retweet_source.id) # stores retweet TwitterResponseAccessor.create(str(retweet_source.id), response_id=str(retweet.id), tweet_type=TwitterResponse.RETWEET, user=retweet_source.user.screen_name) self.__reporter.retweeted(pretty_retweet) return retweet_source, pretty_retweet
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 __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 retweet(self): statuses = self.__twitter.GetFriendsTimeline(count=10) retweet_source = None for candidate in statuses: logging.debug("candidate: %s" % candidate) # TODO: should query for type=retweet # otherwise I may retweet a mention # checks if status has been retweeted response = TwitterResponseAccessor.get_by_message_id(str(candidate.id)) # if msg was already retweeted, removed from set and let loop continue if not response: logging.debug("not previously retweeted: %s" % candidate.id) retweet_source = candidate else: logging.debug("found retweet for message %s" % response.message_id) continue if self.__analyzer.should_retweet(candidate): retweet_source = candidate break else: logging.debug("not retweeting: %s" % describe_status(candidate)) if not retweet_source: raise DataException("could not find anything to retweet") pretty_retweet = describe_status(retweet_source) logging.debug("retweeting '%s'" % pretty_retweet) retweet = self.__twitter.PostRetweet(retweet_source.id) # stores retweet TwitterResponseAccessor.create(str(retweet_source.id), response_id=str(retweet.id), tweet_type=TwitterResponse.RETWEET, user=retweet_source.user.screen_name) self.__reporter.retweeted(pretty_retweet) return retweet_source, pretty_retweet
def _save_response(message, response): TwitterResponseAccessor.create(message.id_str, response_id=response.id_str, user=message.user.screen_name, tweet_type=TwitterResponse.MENTION)