tweepy is no longer available in python 3.x. I have to use another library because the author of tweepy has already done it. Here I use sixohsix's library, which seems to be major to some extent (There seems to be bear's main python-twitter, but I decided that there would be no big difference in usability between them)
https://apps.twitter.com/ Please obtain the application registration, consumer key, consumer secret key, etc. at. Without this, you can't hit twitter api in the first place. What is the consumer key & consumer secret key cannot be shaken in this article.
install.sh
pip install twitter
https://github.com/sixohsix/twitter It's really everything that's written in ... it's all, but maybe it's tough for beginners.
It will be explained in order. If you hit it with a script, you should pick up an oauth token etc. There is a simple api for picking up auth_token on the command line.
token.py
import os
from twitter import *
#consumer key, consumer secret key from https://apps.twitter.com/
CONSUMER_KEY = "YOUR_CONSUMER_KEYAAAAAAAA"
CONSUMER_SECRET="YOUR_CONSUMER_SECRETAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA"
MY_TWITTER_CREDS = os.path.expanduser('~/.my_app_credentials')
if not os.path.exists(MY_TWITTER_CREDS):
oauth_dance("My App Name", CONSUMER_KEY, CONSUMER_SECRET, MY_TWITTER_CREDS)
oauth_token, oauth_secret = read_token_file(MY_TWITTER_CREDS)
twitter = Twitter(auth=OAuth(oauth_token, oauth_secret, CONSUMER_KEY, CONSUMER_SECRET))
# Now work with Twitter
twitter.statuses.update(status='Hello, world!')
This will save the token and secret in a file called ~ / .my_app_credintials. If the file already exists, the code will be read from the file, so if you write this to the extent that it is not magical, you can get a token on the command line for the time being.
https://github.com/sixohsix/twitter#the-twitter-class It's all written here, but the point is, "Create an instance of the Twitter class and hit it with a variable name or function name that looks like it according to the url of the REST API !!". As you can see by reading the source, the author's intention is "I can only write it that way", so it is not unfriendly at all.
For those who think "... I don't know what you're saying ()", I'll write in a little more detail. Please read this while looking at the reference example of the above URL and Description of REST API.
home_timeline.py
# Get your "home" timeline
t.statuses.home_timeline()
This will return the result of / statuses / home_timeline.
oembed.py
# to pass in the GET/POST parameter `id` you need to use `_id`
t.statuses.oembed(_id=1234567890)
This will return the result of passing the id to / statuses / oembed.
updata.py
# Update your status
t.statuses.update(
status="Using @sixohsix's sweet Python Twitter Tools.")
If it's a post that everyone wants to do, it looks like this
As an internal process, when you try to get the property of Twitter class, method runs in getattr, and each property is an instance of class called TwitterCall, so it is callable, so "()" If it works, the method defined by call will be executed.
See also Dive Into Python3 for getattr and call.
The variable name you want to pass to the api can be passed as it is with a name, but "id" is specified by "_id" to cover the reserved word.
So as an application
screen_name.py
print(twitter.account.settings()["screen_name"])
Although it is not written in the reference example, if you get your own screen_name, you can get it like this.
The reason is that there is a REST API called account / settings and there is a variable called screen_name in the return json. ..
block_list.py
print(twitter.blocks.list())
This way you can also get / blocks / list.
So there is no such thing as "call this function to do a search" or "call this function to get a friends list" in this library.
Did you get it? If the explanation is difficult to understand, I will write it in a little more detail.
Recommended Posts