Twitter posting client made with Flask with simple login function

Learning Flask

I touched both bottle and Django, so Flask. As usual, we will use the Twitter API again.

$ python3 -m venv flaskworks
$ ls flaskworks/
$ . flaskworks/bin/activate
(flaskworks)$ pip install flask
(flaskworks)$ pip install requests requests_oauthlib

It's very easy to create a directory like a bottle.

(flaskworks)$ cd flaskworks
(flaskworks)$ mkdir static
(flaskworks)$ mkdir templates

Make a template

Create two because it uses template inheritance.

base.html


<!DOCTYPE html>
<html lang="ja">
<head>
    <meta charset="UTF-8">
    <link rel="stylesheet" href="/static/css/style.css">
    <link rel="stylesheet" href="/static/css/bootstrap.min.css">
    <title>Tweet</title>
</head>
<body>
  <div class="container">
    <div class="row">
      <form method="post" action="/send">
        <input type="text" id="msg" name="msg">
        <input type="submit" value="Send Form">
      </fomr>
        {% block content %}
        {% endblock %}
    </div>
  </div>
</body>
</html>

index.html


{% extends "base.html" %}
{% block content %}
  {% if msg %}
  <p>「{{msg}}I posted</p>
  {% endif %}
{% endblock %}

Write an application for posting

For the time being, I decided to add a simple login function. The reality is almost the same as bottle, isn't it?

tweet.py


import os
from requests_oauthlib import OAuth1Session
import requests
from flask import Flask, render_template, request, redirect, url_for, session
app = Flask(__name__)

app.config['SECRET_KEY'] = os.urandom(24)

C_KEY = '+++++++++++++++++++++++++++++'
C_SECRET = '+++++++++++++++++++++++++++++'
A_KEY = '+++++++++++++++++++++++++++++'
A_SECRET = '+++++++++++++++++++++++++++++'

Post_API = 'https://api.twitter.com/1.1/statuses/update.json'
tw = OAuth1Session(C_KEY,C_SECRET,A_KEY,A_SECRET)

@app.route('/')
def index():
    if 'username' in session:
        return render_template('index.html')
    return '''
        <p>Please login</p>
    '''

@app.route('/login', methods=['GET', 'POST'])
def login():
    if request.method == 'POST':
        username = request.form['username']
        if username == 'admin':
            session['username'] = request.form['username']
            return redirect(url_for('index'))
        else:
            return '''<p>The user name is different</p>'''
    return '''
        <form action="" method="post">
            <p><input type="text" name="username">
            <p><input type="submit" value="Login">
        </form>
    '''

@app.route('/logout')
def logout():
    session.pop('username', None)
    return redirect(url_for('index'))

@app.route('/send', methods=['GET', 'POST'])
def send():
    if request.method == 'POST':
        msg = request.form['msg']
        url = Post_API
        params = {'status': msg,'lang': 'ja'}
        req = tw.post(url, params = params)

        return render_template('index.html', msg=msg)

if __name__ == '__main__':
    app.debug = True
    app.run()

Operation check

(flaskworks)$ python tweet.py
スクリーンショット 2017-05-18 10.47.35.png

That's it.

It's a thin book, but it's summarized https://www.amazon.co.jp/dp/B0714D1VGP

Recommended Posts

Twitter posting client made with Flask with simple login function
Twitter posting application made with Django
Twitter search client made with bottle
Your own Twitter client made with Django
Simple Slack API client made with Python
SNS made with Flask Flask (login process by flask_login)
Crawling with Python and Twitter API 1-Simple search function
Implement login function with django-allauth
SNS Flask (Ajax) made with Flask
I made a simple book application with python + Flask ~ Introduction ~
SNS Flask (Model) edition made with Flask
[LINE login] Verify state with Flask
SNS Python basics made with Flask
Creating a simple app with flask
SNS made with Flask Flask (Blueprint, bcrypt)
Image upload function with Vue.js + Flask
Run it in your browser. Twitter client made with bottle (POST only
A simple RSS reader made with Django
Easily implement login authentication function with Laravel
I made a simple blackjack with Python
Make a simple pixel art generator with Flask
I made a Twitter fujoshi blocker with Python ①
I made a simple Bitcoin wallet with pycoin