Date of writing: 2015.08.09
Thank you for always referring to your blog, Qita, Github, etc.
The environment is Python 3.5.1. There are many libraries that work with Twitter on Python. I've used the following three.
Mostly none
$ pip install twitter
I think you can usually install it like this.
Twitter bot
I think there are two types of (though there may be others)
This time I would like to create these two types of bots.
import twitter
auth = twitter.OAuth(consumer_key="",
consumer_secret="",
token="",
token_secret="")
t = twitter.Twitter(auth=auth)
#Tweet only
status="Hello,World" #Tweet to post
t.statuses.update(status=status) #Post to Twitter
#Tweet with image
pic="" #If you post an image, the image path
with open(pic,"rb") as image_file:
image_data=image_file.read()
pic_upload = twitter.Twitter(domain='upload.twitter.com',auth=auth)
id_img1 = pic_upload.media.upload(media=image_data)["media_id_string"]
t.statuses.update(status=status,media_ids=",".join([id=img1]))
If it is a bot that regularly tweets images and words, it is ok if you modify this a little!
There are several ways to run a script regularly, but using crontab is the best (I personally hate the notation).
Crontab is a tool that executes commands regularly when the set time is reached. -How to write crontab
Use Streaming API.
import twitter
auth = twitter.OAuth(consumer_key="",
consumer_secret="",
token="",
token=secret="")
t = twitter.Twitter(auth=auth)
#Use Userstream
t_userstream = twitter.TwitterStream(auth=auth,domain='userstream.twitter.com')
#Your timeline tweets and user information flow
for msg in twitter.userstream.user():
print(msg)
In the program, in the contents of msg Various information such as user information and tweet information will flow one by one, so please process it so that you can get the information you want.
I will post only the ones I use often.
parameter | contents |
---|---|
in_reply_to_screen_name | Reply destination user name |
text | Tweet content |
name | The name of the user who tweeted(≠screen_name) |
media_url | The URL of the media attached to the tweet |
I wonder if I haven't used the others so far ..
For example, if you want to create a bot that will reply when a reply comes
.
.
for msg in twitter.userstream.user():
#Reply to TL TWITTER_Branch if ID is included
if msg['in_reply_to_screen_name']=="TWITTER_ID":
tweet = "@"+msg['user']['screen_name']+" "+"Reply Thanks you"
t.statuses.update(status=tweet)
It can be created like this (example)
For those who want to know in detail what kind of information can be obtained from the flowing information (msg) [Data structure when acquired by Twitter API](http://biokids.org/?%A4%C9%A4%D6%A4%AA%2FTwitter%A4%C7%CD%B7%A4%DC%A4% Please see A6% A1% AA% 2F% A5% C7% A1% BC% A5% BF% B9% BD% C2% A4).
I will write about tweepy at a later date.
Actually, there may be a more efficient operation method, but for now it looks like this.
update_name Feeling like playing with twitter profile settings on CUI
import tweepy
auth = OAuthHandler(consumer_key="",consumer_secret="")
auth.set_access_token(token="",token=secret="")
api = API(auth)
api.update_profile(name='New name')
For other update_profiles, see POST account / update_profile | Twitter Developers.
For me personally, tweepy is easier to see and use than python-twitter. API Reference — tweepy 3.5.0 documentation So if you think you can do this, go to the twitter official and the reference of each library !!
-Kivantium activity diary Using Twitter with Python ~ Introduction of Tweepy ~
Recommended Posts