コード例 #1
0
ファイル: socialbot.py プロジェクト: Cloudxtreme/SocialBot-2
def reply_target(follower_id, t2):
    """
    Find a valid tweet from target, modify and
    Omegle it, then tweet a reply to target.
    """
    global tweet_ids

    #--- check for invalid target_id ---
    #if target_id == None or target_id == 0:
    #    print 'Invalid target id, retweet failed...\n'
    #    return False
    if follower_id == None or follower_id == 0:
        print 'Invalid follower id, mention failed...\n'
        return False
    #--- grab target's tweet ---
    #print 'Getting Tweet from Target Set'
    #tweet = get_tweet( str(target_id), t2 )
    print 'Getting Tweet from Follower Set'
    ftweet = get_tweet(str(follower_id), t2)
    #if tweet == '' or tweet == None:
    #    return False
    if ftweet == '' or ftweet == None:
        return False
    else:
        #--- save tweet id, so we won't accidentally go at it again ---
        #tweet_ids.append( tweet['id_str'] )
        tweet_ids.append(ftweet['id_str'])
        tweet_ids_file = open(FILE_TWEET_IDS, 'a')
        #tweet_ids_file.write( tweet['id_str'] + '\n' )
        tweet_ids_file.write(ftweet['id_str'] + '\n')
        tweet_ids_file.close()

        #--- retweet (1/20 chance) or omegle (19/20 chance) the tweet ---
        if get_probability(CHANCE_OMEGLE):
            #--- display console info ---
            print 'Tweet by Follower: @' + ftweet['user'][
                'screen_name'] + ':', ftweet['text'].encode('utf-8'), '\n'
            #--- post tweet on Omegle, modify response and tweet ---
            modified_tweet = modifyTweet(ftweet, DEFAULT_USER).encode('utf-8')
            print 'Send to Omegle:', modified_tweet
            print datetime.now(), '\n'
            omegle_reply = ''
            omegle_reply = GetAnswerTo(modified_tweet)
            omegle_reply = stripUrls(omegle_reply)
            if str(omegle_reply) == '':
                print 'Error: not supposed to receive empty string from Omegle...\n'
                return False
            reply = '@' + str(
                ftweet['user']['screen_name']) + ' ' + str(omegle_reply)
            try:
                reply_tweet = t2.statuses.update(
                    status=reply, in_reply_to_status_id=ftweet['id'])
            except:
                print 'Error tweeting, possibly duplicated response by Cleverbot, skipping...\n'
                return False
            print '\n', 'Replying tweet by @' + str(
                ftweet['user']['screen_name']) + ':'
            print '<<<===', ftweet['text'].encode('utf-8')
            print '===>>>', reply_tweet['text'].encode('utf-8')
            print datetime.now(), '\n'
        else:
            #--- display console info ---
            print 'Tweet by @' + ftweet['user']['screen_name'] + ':', ftweet[
                'text'].encode('utf-8'), '\n'
            #--- retweet only, how boring... ---
            try:
                t2.statuses.retweet(id=ftweet['id'])
                print 'Very low chance, but bot retweeted target tweet.\n'
            except TwitterError:
                print '\n', 'Wota! Another TwitterHTTPError...\n'

        return True
コード例 #2
0
ファイル: socialbot.py プロジェクト: carriercomm/SocialBot-2
def reply_target( follower_id, t2 ):
    """
    Find a valid tweet from target, modify and
    Omegle it, then tweet a reply to target.
    """
    global tweet_ids
    
    #--- check for invalid target_id ---
    #if target_id == None or target_id == 0:
    #    print 'Invalid target id, retweet failed...\n'
    #    return False
    if follower_id == None or follower_id == 0:
        print 'Invalid follower id, mention failed...\n'
        return False
    #--- grab target's tweet ---
    #print 'Getting Tweet from Target Set'
    #tweet = get_tweet( str(target_id), t2 )
    print 'Getting Tweet from Follower Set'
    ftweet = get_tweet( str(follower_id), t2 )
    #if tweet == '' or tweet == None:
    #    return False
    if ftweet == '' or ftweet == None:
        return False
    else:
        #--- save tweet id, so we won't accidentally go at it again ---
        #tweet_ids.append( tweet['id_str'] ) 
        tweet_ids.append( ftweet['id_str'] ) 
        tweet_ids_file = open( FILE_TWEET_IDS, 'a' )
        #tweet_ids_file.write( tweet['id_str'] + '\n' )
        tweet_ids_file.write( ftweet['id_str'] + '\n' )
        tweet_ids_file.close()
        
        #--- retweet (1/20 chance) or omegle (19/20 chance) the tweet ---
        if get_probability( CHANCE_OMEGLE ):
            #--- display console info ---
            print 'Tweet by Follower: @' + ftweet['user']['screen_name'] + ':', ftweet['text'].encode('utf-8'), '\n'
            #--- post tweet on Omegle, modify response and tweet ---
            modified_tweet = modifyTweet( ftweet, DEFAULT_USER ).encode('utf-8')
            print 'Send to Omegle:', modified_tweet
            print datetime.now(), '\n'
            omegle_reply = ''
            omegle_reply = GetAnswerTo( modified_tweet )
            omegle_reply = stripUrls( omegle_reply )
            if str( omegle_reply ) == '':
                print 'Error: not supposed to receive empty string from Omegle...\n'
                return False
            reply = '@' + str( ftweet['user']['screen_name'] ) + ' ' + str( omegle_reply )
            try:
                reply_tweet = t2.statuses.update( status = reply, in_reply_to_status_id = ftweet['id'] )
            except:
                print 'Error tweeting, possibly duplicated response by Cleverbot, skipping...\n'
                return False 
            print '\n', 'Replying tweet by @' + str( ftweet['user']['screen_name'] ) + ':'
            print '<<<===', ftweet['text'].encode('utf-8')
            print '===>>>', reply_tweet['text'].encode('utf-8')
            print datetime.now(), '\n'
        else:
            #--- display console info ---
            print 'Tweet by @' + ftweet['user']['screen_name'] + ':', ftweet['text'].encode('utf-8'), '\n'
            #--- retweet only, how boring... ---
            try:
                t2.statuses.retweet( id = ftweet['id'] )
                print 'Very low chance, but bot retweeted target tweet.\n'
            except TwitterError:
                print '\n', 'Wota! Another TwitterHTTPError...\n'

        return True
コード例 #3
0
ファイル: socialbot.py プロジェクト: Cloudxtreme/SocialBot-2
def check_mentions(id, t2):
    """
    Grab bot's own list of mentions, iterate thru the list, modify each
    mention and post it on Omegle. Grab result from Omegle and reply
    back on Twitter to user that mentioned it.
    """
    global tweet_ids

    try:
        mentions = t2.statuses.mentions(since_id=id,
                                        include_rts=1,
                                        include_entities=1,
                                        count=MENTION_COUNT)
    except:
        print 'TwitterHTTPError, mentions not received...\n'
        return

    #--- check for new mentions ---
    if len(mentions) == 0:
        print 'No new mentions at this time...\n'
    else:
        print 'Number of new mentions:', len(mentions), '\n'

    if DEBUG_MODE:
        print 'tweet ids before loop:'
        print '(', tweet_ids, ')', '\n'

    #--- iterate list of mentions, omegle, and reply accordingly ---
    count = len(mentions) - 1
    while count >= 0:
        #--- check the id list again to see if any other cron job process added an id to the list. Update array.
        tweet_ids = []
        set_tweet_ids()

        if DEBUG_MODE:
            print 'tweet ids after reset:'
            print '(', tweet_ids, ')', '\n'

        #--- verify if already replied this mention ---
        if mentions[count]['id_str'] not in tweet_ids:

            #--- store tweet id so we won't run thru it again in future ---
            tweet_ids.append(mentions[count]['id_str'])

            if DEBUG_MODE:
                print 'tweet ids after appending:'
                print '(', tweet_ids, ')', '\n'

            tweet_ids_file = open(FILE_TWEET_IDS, 'a')
            tweet_ids_file.write(mentions[count]['id_str'] + '\n')
            tweet_ids_file.close()

            #--- write and update last_mention_id ---
            last_mention_id_file = open(FILE_LAST_MENTION_ID, 'w')
            last_mention_id_file.write(mentions[count]['id_str'])
            last_mention_id_file.close()

            #--- modify mention string ---
            original_mention = mentions[count]['text'].encode('utf-8')
            print 'Original:', original_mention, '(from @' + mentions[count][
                'user']['screen_name'] + ')'
            mod_mention = modifyTweet(mentions[count], DEFAULT_USER)
            print 'Modified: ' + mod_mention.encode('utf-8') + '\n'
            print 'Sending request to Omegle on:'
            print datetime.now(), '\n'

            if DEBUG_MODE:
                raw_input("wait...")

            #--- post on Omegle and grab response ---
            omegle_reply = GetAnswerTo(mod_mention.encode('utf-8'))
            if str(omegle_reply) == '':
                print 'Error: not supposed to receive empty string from Omegle...\n'
                return

            #--- reply to user ---
            reply = '@' + str(mentions[count]['user']
                              ['screen_name']) + ' ' + str(omegle_reply)
            try:
                tweet = t2.statuses.update(
                    status=reply, in_reply_to_status_id=mentions[count]['id'])
            except:
                print 'Error tweeting, possibly duplicated response by Cleverbot, trying again...\n'
                return
            print '\n', '<<<===', original_mention
            print '===>>>', tweet['text'].encode('utf-8')
            print datetime.now(), '\n'
        else:
            print 'Already replied this mention, next...\n'

            #--- write and update last_mention_id ---
            last_mention_id_file = open(FILE_LAST_MENTION_ID, 'w')
            last_mention_id_file.write(mentions[count]['id_str'])
            last_mention_id_file.close()

        count = count - 1
コード例 #4
0
ファイル: socialbot.py プロジェクト: carriercomm/SocialBot-2
def check_mentions( id, t2 ):
    """
    Grab bot's own list of mentions, iterate thru the list, modify each
    mention and post it on Omegle. Grab result from Omegle and reply
    back on Twitter to user that mentioned it.
    """
    global tweet_ids
    
    try:
        mentions = t2.statuses.mentions( since_id = id, 
                                     include_rts = 1, include_entities=1, count = MENTION_COUNT )
    except:
        print 'TwitterHTTPError, mentions not received...\n'
        return
        
    #--- check for new mentions ---
    if len( mentions ) == 0:
        print 'No new mentions at this time...\n'
    else:
        print 'Number of new mentions:', len( mentions ), '\n'
    
    if DEBUG_MODE:
        print 'tweet ids before loop:'
        print '(', tweet_ids, ')', '\n'
    
    #--- iterate list of mentions, omegle, and reply accordingly ---
    count = len( mentions ) - 1
    while count >= 0:
        #--- check the id list again to see if any other cron job process added an id to the list. Update array.
        tweet_ids = []
        set_tweet_ids()
        
        if DEBUG_MODE:
            print 'tweet ids after reset:'
            print '(', tweet_ids, ')', '\n'
        
        #--- verify if already replied this mention ---
        if mentions[count]['id_str'] not in tweet_ids:
            
            #--- store tweet id so we won't run thru it again in future ---
            tweet_ids.append( mentions[count]['id_str'] )
            
            if DEBUG_MODE:
                print 'tweet ids after appending:'
                print '(', tweet_ids, ')', '\n'
            
            tweet_ids_file = open( FILE_TWEET_IDS, 'a' )
            tweet_ids_file.write( mentions[count]['id_str'] + '\n' )
            tweet_ids_file.close()
            
            #--- write and update last_mention_id ---
            last_mention_id_file = open( FILE_LAST_MENTION_ID, 'w')
            last_mention_id_file.write( mentions[count]['id_str'] )
            last_mention_id_file.close()
            
            #--- modify mention string ---
            original_mention = mentions[count]['text'].encode('utf-8')
            print 'Original:', original_mention, '(from @' + mentions[count]['user']['screen_name'] + ')'
            mod_mention = modifyTweet( mentions[count], DEFAULT_USER )
            print 'Modified: ' + mod_mention.encode('utf-8') + '\n'
            print 'Sending request to Omegle on:'
            print datetime.now(), '\n'
            
            if DEBUG_MODE:
                raw_input("wait...")
            
            #--- post on Omegle and grab response ---
            omegle_reply = GetAnswerTo( mod_mention.encode('utf-8') )
            if str( omegle_reply ) == '':
                print 'Error: not supposed to receive empty string from Omegle...\n'
                return
            
            #--- reply to user ---
            reply = '@' + str( mentions[count]['user']['screen_name'] ) + ' ' + str( omegle_reply )
            try:
                tweet = t2.statuses.update( status = reply, in_reply_to_status_id = mentions[count]['id'] )
            except:
                print 'Error tweeting, possibly duplicated response by Cleverbot, trying again...\n'
                return
            print '\n', '<<<===', original_mention
            print '===>>>', tweet['text'].encode('utf-8')
            print datetime.now(), '\n'
        else:
            print 'Already replied this mention, next...\n'
            
            #--- write and update last_mention_id ---
            last_mention_id_file = open( FILE_LAST_MENTION_ID, 'w')
            last_mention_id_file.write( mentions[count]['id_str'] )
            last_mention_id_file.close()
            
        count = count - 1