-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.py
71 lines (51 loc) · 1.72 KB
/
app.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
import os
from flask import Flask
from flask import url_for
from flask import request
from flask import redirect
from flask import send_file
from flask import render_template
from flask import send_from_directory
from database.db import db
from database.models import Tweet
from common import get_tweet
from common import generate_image
from datetime import datetime
app = Flask(__name__)
app.config.from_object('config')
db.init_app(app)
port = 8001
@app.route('/', methods=['GET'])
def index_get():
existing_items = Tweet.query.order_by(Tweet.created.desc()).limit(6)
existing_items = [x.tweet_id for x in existing_items]
return render_template('index.html', existing_items=existing_items, request=request)
@app.route('/', methods=['POST'])
def index_post():
tweet_id = request.form['tweet_id']
if not tweet_id:
return render_template('index.html')
tweet_entry = Tweet.query.get(int(tweet_id))
if tweet_entry:
now = datetime.now()
tweet_entry.created = now
db.session.commit()
return redirect(url_for('index_get', id=tweet_id))
tweet = get_tweet(tweet_id)
db.session.add(Tweet(tweet))
db.session.commit()
return redirect(url_for('index_get', id=tweet_id))
@app.route('/generate/<tweet_id>', methods=['GET'])
def generate_get(tweet_id):
tweet = get_tweet(tweet_id)
file_io = generate_image(tweet)
return send_file(file_io, attachment_filename='image.jpeg')
@app.route('/favicon.ico')
def favicon():
return send_from_directory(
os.path.join(app.root_path, 'static'),
'favicon.ico', mimetype='image/vnd.microsoft.icon')
if __name__ == '__main__':
with app.test_request_context():
db.create_all()
app.run(port=port)