Exemple #1
0
    def act(self):
        current_source = cal_helper.randomizeSource(tweet_prob)
        self.select_source = current_source
        text_msg = ''
        attachment_url = ''
        if current_source == 'timeline':
            source_obj = source.HomeTimeLine(self.username)
            if not source_obj:
                return False
            source_data = source_obj.getSourceData()
            if not source_data:
                return False

            source_data = self._filter(source_data, (not self.whether_quote))
            if not source_data:
                return False

            tweet = cal_helper.randomizeObjs(source_data)
            tweet_id = tweet['id']
            tweet_owner_name = tweet['user']['screen_name']
            text_msg = '%s ' % (cal_helper.RandomizeAnTweetFromFile()
                                )  # get a randomly reply
            attachment_url = 'https://twitter.com/%s/status/%s' % (
                tweet_owner_name, tweet_id)
        elif current_source == 'trend':
            tweet_id = randomizeATweetIDFromTrends(self.username)
            if not tweet_id:
                return False
            source_obj = source.TweetObj(self.username, tweet_id)
            tweet = source_obj.getSourceData()
            if tweet:
                try:
                    text_msg = tweet['full_text']
                except:
                    text_msg = tweet['text']
        else:  #random_quotes
            source_obj = source.RandomQuotes()
            text_msg = source_obj.getSourceData()
            text_msg = self.cleanhtml(text_msg)

        if not text_msg:
            return False

        final_txt_msg = '%s%s' % (text_msg, attachment_url)
        cmd = self.composeCmd(final_txt_msg)
        # TODO: fix subprocess problem?
        os_cmd = ""
        for c in cmd:
            os_cmd += "%s " % c
        try:
            os.system(os_cmd)
        except:
            return False
        return True
Exemple #2
0
    def SaveCurrentHomeTimeline(self):
        try:
            print(self.username)
            source_obj = source.HomeTimeLine(self.username)
            source_data = source_obj.getSourceData()
            if not source_data:
                raise ValueError('cannot obtain hometimeline')
        except ValueError as e:
            debug.LogToDebug(
                '=======\nfailed to save hometimeline for bot %s at %s\n' %
                (self.username, datetime.now()))
            debug.LogToDebug(str(e))
        print('save htl 1')
        if source_data:

            dt = datetime.now()
            comm_htl = """
      INSERT INTO home_timeline(bot_id, checked_at) 
      VALUES(%s, %s) RETURNING id;
      """
            self.tl_id = self._execute(comm_htl, 'home_timeline created',
                                       (self.bot_id, dt), True, True)
            print('save htl 2')
            print('self.tl_id is %s' % self.tl_id)
            comm_htl_tw = """
        INSERT INTO home_timeline_tweets(htl_id, tw_id) 
         VALUES(%s, %s);
      """

            comm_tweets = """
        insert into tweet(tweet_obj, tweet_id, created_at)
                    values(%s, %s, %s);
      """

            for tweet in source_data:
                self._execute(
                    comm_tweets, 'tweet table update',
                    (json.dumps(tweet), tweet['id'],
                     time.strftime(
                         '%Y-%m-%d %H:%M:%S',
                         time.strptime(tweet['created_at'],
                                       '%a %b %d %H:%M:%S +0000 %Y'))), True)

                self._execute(comm_htl_tw,
                              'tweet table update in home_timeline_tweets',
                              (self.tl_id, tweet['id']), True)
Exemple #3
0
def randomizeATweetFromLikes(usr_name, return_key=None):
    """Randomize a tweet from friends' likes.

  The friends are fetched from the latest num_timeline_tweets tweets in the
  home timeline. Then we randomly return a tweet that is the latest
  M liked tweets liked num_latest_likes by each friends.

  Args:
    return_key: if return_key is none, it returns the tweet object.
  """
    source_obj = source.HomeTimeLine(usr_name)
    if not source_obj:
        return False
    source_data = source_obj.getSourceData()
    if not source_data:
        return False
    user_obj_lst = cal_helper.randomizeObjs(
        source_data,
        return_key='user',
        num_returns=num_latest_tweets_to_look_likes)
    user_ids = set()
    for user in user_obj_lst:
        if user['screen_name'] == usr_name:
            continue
        user_ids.add(user['id'])

    source_obj = source.LikedTweets(None)
    tweets = []
    for user_id in user_ids:
        source_obj.username = user_id
        tmp_tweets = source_obj.getSourceData()
        if tmp_tweets:
            tweets += tmp_tweets

    if not tweets:
        return False
    target_tweet = np.random.choice(tweets, 1)[0]
    if return_key:
        return target_tweet[return_key]
    else:
        return target_tweet
Exemple #4
0
 def act(self):
     current_source = cal_helper.randomizeSource(like_prob)
     self.select_source = current_source
     if current_source == 'trend':
         tweet_id = randomizeATweetIDFromTrends(self.username)
         if not tweet_id:
             return False
     elif current_source == 'like':
         tweet_id = randomizeATweetFromLikes(self.username, return_key='id')
     else:  # timeline
         source_obj = source.HomeTimeLine(self.username)
         if not source_obj:
             return False
         source_data = source_obj.getSourceData()
         if not source_data:
             return False
         tweet_id = cal_helper.randomizeObjs(source_data, return_key='id')
     if_suc = self.apiAct(tweet_id)
     if not if_suc:
         return False
     return tweet_id
Exemple #5
0
    def act(self):
        # check whether to follow or switch to unfollow
        whether_to_follow = self.WhetherFollow()
        if not whether_to_follow:
            current_action = Unfollow(self.username, unfollow_method)
            if_suc = current_action.act()
            if not if_suc:
                return if_suc
            else:
                return -if_suc

        current_source = cal_helper.randomizeSource(follow_prob)
        if_suc = False

        if not self.twitter_id:
            source_obj = source.UserObj(self.username)
            current_bot = source_obj.getSourceData()
            if not current_bot:
                return False
            self.twitter_id = current_bot['id']

        self.select_source = current_source
        if current_source == 'FoF':
            # 1. friend list
            source_obj = source.Friends(self.username)
            friend_id_lst = source_obj.getSourceData()
            if not friend_id_lst:
                return False
            # 2. Randomize N friends and get their friend lists(allow duplicate)
            sub_friends = cal_helper.randomizeObjs(
                friend_id_lst, num_friends_to_lookat_inFoF)
            sub_friends = list(set(sub_friends))
            candidate_friend_lst = []
            for ff in sub_friends:
                source_obj.user_id = ff
                f_lst = source_obj.getSourceData()
                if not f_lst:
                    continue
                candidate_friend_lst += f_lst
            if not candidate_friend_lst:
                return False
        elif current_source == 'timeline':  # == retweet based on Twitter API return
            source_obj = source.HomeTimeLine(self.username)
            if not source_obj:
                return False
            source_data = source_obj.getSourceData()
            if source_data:
                candidate_friend_lst = cal_helper.RetriveUserIds(source_data)
        elif current_source == 'follower':  # follower
            source_obj = source.Followers(self.username)
            candidate_friend_lst = source_obj.getSourceData()
            if not candidate_friend_lst:
                return False
        else:  #liked
            usr_obj = randomizeATweetFromLikes(self.username,
                                               return_key='user')
            if not usr_obj:
                return False
            selected_id = usr_obj['id']

        if current_source != 'liked':
            # Get the list of friend ids and my id.
            friend_obj = source.Friends(self.username,
                                        whether_user_entity=False)
            removing_id_lst = friend_obj.getSourceData()
            candidate_lst = []
            if removing_id_lst:
                removing_id_lst.append(self.twitter_id)
                for iid in candidate_friend_lst:
                    if iid not in removing_id_lst:
                        candidate_lst.append(iid)

            if not candidate_lst:
                return False
            selected_id = np.random.choice(candidate_lst, 1)[0]

        if_suc = self.apiAct(selected_id)
        if not if_suc:
            return if_suc
        return selected_id