Twitter posts on Python 3 etc.

I want to use twitter even in Python3

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)

Introduction

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.

Installation

install.sh


pip install twitter

How to use

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.

Access to twitter api

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.

Timeline acquisition example

home_timeline.py


# Get your "home" timeline
t.statuses.home_timeline()

This will return the result of / statuses / home_timeline.

Get embedded status

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.

When posting

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

Explanation about the contents

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.

Application example-Get screen_name

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. ..

Application example-Get block_list

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

Twitter posts on Python 3 etc.
twitter on python3
[Memo] Tweet on twitter with python
Python on Windows
python on mac
Python on Windbg
Install Python3, numpy, pandas, matplotlib, etc. on Windows
Python conda on cygwin
Install python on WSL
PyOpenGL setup on Python 3
Character count on Twitter
Install Python on Pidora.
Install Scrapy on python3
Convenient Python methods, etc.
Install Python on Mac
Install Python 3 on Mac
Install Python3.4 on CentOS 6.6
Installing pandas on python2.6
python basic on windows ②
Install python on windows
Install Python 2.7.3 on CentOS 5.4
Search Twitter using Python
build Python on Ubuntu
Install Python 3.3 on Ubuntu 12.04
Install Python 3.4 on Mac
Install Python 3.6 on Docker
[Python] This is easy! Search for tweets on Twitter
Automatic follow on Twitter with python and selenium! (RPA)
Install Bash on Ubuntu on Windows, Ruby, Python, Jupyter, etc.
Set-enable Python virtualenv on Windows
Set up Python 3.4 on Ubuntu
Run Openpose on Python (Windows)
Install Python 3.8 on RHEL 8 (AppStream)
Install watchdog on Windows + Python 3.3
Python on Ruby and angry Ruby on Python
Twitter graphing memo with Python
Get Twitter timeline with python
Create a list in Python with all followers on twitter
Use Twitter API with Python
Install pygame on python3.4 on mac
Python + Kivy development on Windows
Install Python 3.8 on CentOS 7 (SCL)
Real-time graphs on Plotly (Python)
Sphinx-autobuild (0.5.2) on Windows7, Python 3.5.1, Sphinx 1.3.5
Post to Twitter using Python
Put Python 3.x on Ubuntu
Fastest Python installation on Windows
[Python] Notes on data analysis
Build Python environment on Windows
Handling of python on mac
Install pandas 0.14 on python3.4 [on Mac]
Install OpenCV on Ubuntu + python
Notes on installing Python on Mac
[python] super (), inheritance, __init__, etc.
Run Python CGI on CORESERVER
Run unix command on python
Build python environment on windows
Python --Install MySQLDB on EC2
Introducing TensorFlow on Ubuntu + Python 2.7
Install Python 3.8 on CentOS 8 (AppStream)
I ran python on windows