Python3.4 Bottle IntelliJ Do something on twitter
python3.4, Intellij environment construction Study basic usage of bottle Easy to use jinja2 template modularization
From the result, it failed
There are a lot of Twitter libraries, but the Web API only sends JSON. Only Auth is done in the library, and the rest is to make an HTTP request directly Unlike the Twitter library, it can also be used for other Auth such as Facebook and Google+
So I decided to use Requests-OAuthlib
As usual,
import requests_oauthlib
And import it with IntelliJ Not found and cannot be resolved!
I tried various things, but eventually gave up using it with IntelliJ To install with pip and start it from the command line
First, register the Twitter API and consumer_key, consumer_secret, access_token, access_token_secret Please get and set
python
import os
import bottle
import jinja2
import requests_oauthlib
from requests_oauthlib import OAuth1Session
from bottle import route, post, request, run
from bottle import TEMPLATE_PATH, jinja2_template as template
base_url = "https://api.twitter.com/1.1/"
update_url="statuses/update.json"
consumer_key="hoge"
consumer_secret="hoge"
access_token="hoge"
access_token_secret="hoge"
TEMPLATE_PATH.append("./template")
class Twitter:
def __init__(self, consumer_key, consumer_secret, access_token, access_token_secret):
self.twitter_ = OAuth1Session(consumer_key, consumer_secret, access_token, access_token_secret)
def update(self, msg):
param="{'status:': '" + msg + "'}"
return(self.twitter_.post(base_url+update_url, param))
@route('/')
def HomeHandler_():
return template("home.j2", posts="Get timeline soon")
@post('/1/twitter/post')
def UpdateHandler_():
global twitter_
msg = request.forms.get('status')
print("post:"+msg)
req = twitter_.update(msg)
return template("home.j2", posts="Get timeline soon")
twitter_ = Twitter(secret.consumer_key, secret.consumer_secret, secret.access_token, secret.access_token_secret)
if __name__ == "__main__":
run(host='localhost', port=1046, debug=True, reloader=True)
home.j2
{% extends "base.j2" %}
{% block content %}
<div id="postform">
<form method="POST" action="/1/twitter/post">
<table>
<tr><td><textarea cols="70" rows="3" name="status"></textarea></td></tr>
<tr><td align="right"><input type="submit" name="doit" value="Update"></td></tr>
</table>
</form>
</div>
<h2>Timeline</h2>
<i>aaa</i><br>
{% for post in posts %}
{{ post }}
{% endfor %}
{% endblock %}
I feel like the code is done properly. It doesn't work. ..
I tried my best for about 2 hours, but I couldn't tweet well, so I give up
Tweepy You can easily change the library. I met a lot, but somehow I decided on a library called Tweepy. I do not have a reason By the way, it will be modularized
The module name will be Twitter.py main is
import Twitter
Add
Add the module name namespace to the Twitter.hoge part Change to Twitter.Twitter.hoge
Twitter.py
# -*- encoding: utf-8 -*-
import tweepy
__author__ = 'miyatake_y'
base_url = "https://api.twitter.com/1.1/"
update_url="statuses/update.json"
class Twitter:
def __init__(self, consumer_key, consumer_secret, access_token, access_token_secret):
self.auth_ = tweepy.OAuthHandler(consumer_key,consumer_secret)
self.auth_.set_access_token(access_token,access_token_secret)
self.twitter_ = tweepy.API(self.auth_)
def update(self, msg):
print("Twitter.update:" + msg)
return(self.twitter_.update_status(status=msg))
You can now POST from the form! Moreover, it can be operated from IntelliJ! But Japanese is garbled. Next time I will deal with that
It was very easy. All you have to do is call decode () on request
msg = request.forms.decode().get('status')
Now you can post in Japanese!
Getting the timeline is easy Immediately with Tweepy
twitter_.home_timeline()
Then a list of status objects will be returned. Arguments are how many tweets are fetched, from what number, etc. By default, 20 will be brought from the latest
print(twitter_.home_timeline()[0].text)
If so, the latest Tweet text will be displayed In addition to text, the status object also contains other data such as time.
This time I get a list of timelines Pass the list to the jinja2 template engine To loop in the template
Added method to Twitter class. Now that I think about it, I thought it would be easier to use Tweepy live.
Twitter.py
def home_timeline(self):
return(self.twitter_.home_timeline())
Give a status list as an argument to the template
main.py
return template("home.j2", posts=twitter_.home_timeline())
And turn the template with for
home.j2
{% for post in posts %}
{{ post.text }} <br><hr>
{% endfor %}
Now you can see the timeline! !!
View your POST. template I want to increase the API, so it's a friend. Get ACCESS TOKEN programmatically
Recommended Posts