Created
January 16, 2019 15:56
-
-
Save seeeturtle/c4cfa87591bbdbc149bb7fe750a4a599 to your computer and use it in GitHub Desktop.
흑우를 HTML로 짠다면?
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<import> | |
re | |
psycopg2 | |
model | |
[flask [Flask request jsonify]] | |
[flask_cors CORS] | |
</import> | |
<def | |
app = <Flask>__name__</Flask> | |
_ = <CORS>app</CORS> | |
DATABASE_URL = <get>os.environ "DATABASE_URL"</get> | |
conn = <psycopg2.connect dsn=DATABASE_URL sslmode="require"/> | |
cursor = <conn.cursor/> | |
db = <model.Database cursor=cursor/>/> | |
<@><app.route rule="/news" methods=["GET", "POST"]/> | |
<defn news> | |
<if> | |
<=>request.method "GET"</=> | |
<do> | |
<def | |
amount = <int><request.args.get key="amount"/></int> | |
category = <int><request.args.get key="category"/></int>/> | |
<jsonify><db.many_news amount=amount category=category/></jsonify> | |
</do> | |
<=>request.method "POST"</=> | |
<do> | |
<def content=<request.get_json cache=False/>/> | |
<db.add_news | |
title=<get>content "title"</get> | |
preview=<get_image content=<get>content "content"</get>/> | |
category=<get>content "category"</get> | |
content=<get>content "content"</get>/> | |
<conn.commit/> | |
"Success!" | |
</do> | |
</if> | |
</defn></@> | |
<@><app.route rule="/news/<int:category>/<string:title>"/> | |
<defn news_by_id category title> | |
<jsonify><db.news category=category title=title/></jsonify> | |
</defn></@> | |
<defn get_image content> | |
<def s = <re.findall pattern=r"!\[.*\]\(.*\)" string=content> | |
s = <get>s 0</get> | |
lparen = <s.find pattern="("/> | |
rparen = <s.find pattern=")"/> | |
s = <get>s <inc x=lparen/> rparen</get>/> | |
s | |
</defn> | |
<when> | |
<=>__name__ "__main__"</=> | |
<app.run host="0.0.0.0" port=<get>os.environ "PORT"</get>/> | |
</when> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import os | |
import re | |
import psycopg2 | |
import model | |
from flask import Flask, request, jsonify | |
from flask_cors import CORS | |
app = Flask(__name__) | |
CORS(app) | |
DATABASE_URL = os.environ['DATABASE_URL'] | |
conn = psycopg2.connect(DATABASE_URL, sslmode='require') | |
cursor = conn.cursor() | |
db = model.Database(cursor) | |
@app.route('/news', methods=['GET', 'POST']) | |
def news(): | |
if request.method == 'GET': | |
amount = int(request.args.get('amount')) | |
category = int(request.args.get('category')) | |
return jsonify(db.many_news(amount, category)) | |
elif request.method == 'POST': | |
content = request.get_json(cache=False) | |
db.add_news( | |
content['title'], | |
get_image(content['content']), | |
content['category'], | |
content['content'], | |
) | |
conn.commit() | |
return 'Success!' | |
@app.route('/news/<int:category>/<string:title>') | |
def news_by_id(category, title): | |
return jsonify(db.news(category, title)) | |
def get_image(content): | |
s = re.findall(r"!\[.*\]\(.*\)", content)[0] | |
return s[s.find("(")+1:s.find(")")] | |
if __name__ == '__main__': | |
app.run(host='0.0.0.0', port=os.environ['PORT']) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment