Automatic follow and random tweets as some functions of bot

Yes, my own account is not in a situation that requires this feature, but as a note of what worked. Tweepy must be installed in the default python environment

bot.py



#Only the follow-back part is described purely
#OAuth token processing etc. are omitted

# @The omitted user name""Surrounded by
#In the API specification
# Parameters: id OR screen_name OR user_id, cursor
#There seems to be an option, but I wonder if this is the easiest to understand
user = "hogehoge"

#Get followers
follower = api.followers_ids(user)
#Get already followed
friends = api.friends_ids(user)

# (Follower) - (Already followed)Take the difference with and list the unfollowed
set_apr = set(follower) - set(friends)
list_apr = list(set_apr)

#From beginning to end of unfollowed list in user account
#Follow

for user in list_apr:
   api.create_friendship(user)

It may be better to perform the follow operation if ~ else ~ depending on whether the list is None.

I haven't tried this time, but I think that it is possible to further select unfollowed accounts in the difference by acquiring tweets of accounts in the list as anti-spam measures.

2013/06/15 Re-edit API1.1 Operation check OK. Typo correction.

 prof = api.update_profile().description

I can get the description of the app / API user himself, but I can't get the description of anyone other than myself even if I enter something in () or try it. And the name will be rewritten to it. .. .. .. If I can get the description, I can sort it by a specific term such as affiliate or business and not follow back. .. .. .. .. I wonder if update_profile () itself is for the person only. ..

See here by the time of the next fix http://dx.24-7.co.jp/twitterapi1-1-rest-api/ https://dev.twitter.com/docs/api/1.1 Reference site http://packages.python.org/tweepy/html/api.html#tweepy-api-twitter-api-wrapper http://yuutookun.hatenablog.com/entry/20120316/1331863793

Random tweets

Random tweets as part of the bot feature

Yes, I used to throw in all the lines like bot.py for random tweets, but since I succeeded in differentiation, I made a rough note

Tesuto 0
Tesuto 1
Street fighter 2
Test 3

It is a file that contains lines. The character code is utf-8. '' "" \ ,. You don't need to separate them with lines, just write one line.

bot.py



import random

#Please change the width of the random number according to the number of dialogue patterns.
x = random.randint(0,3)

def randomtweet():
   ra = open('tweet')
   line = ra.readlines()
   ra.close()
#Remove the line break that comes at the end
   rtw = line[x].replace('\n','')
#The posting part is omitted this time as well, but in this function the posting operation
#If you throw rtw into the dialogue, it will work.

Depending on the number of lines, it may not move at the scheduled time because it will be caught in the posting limit. If I took the previous record and it was the same, I thought about re-lottery, but I have not executed it. You can avoid the limitation of the same line by adding the value of microsecond in datetime to the end of the line. Perhaps. ..

Somehow it's too half-finished, so I'd like to summarize it a little more this month.

Recommended Posts

Automatic follow and random tweets as some functions of bot
Formulas and functions (updated as appropriate)