Skip to content

Instantly share code, notes, and snippets.

@pvolyntsev
Created February 7, 2017 04:52
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save pvolyntsev/c43365c0e0f5834aa06c9d3a85ff4d72 to your computer and use it in GitHub Desktop.
Save pvolyntsev/c43365c0e0f5834aa06c9d3a85ff4d72 to your computer and use it in GitHub Desktop.
# -*- coding: UTF-8 -*-
from flask import Flask, request, render_template, jsonify
from flaskext.mysql import MySQL
from flask_cors import CORS, cross_origin
from pymysql.cursors import DictCursor
import json
app = Flask(__name__)
# Настройки доступа для крос-доменных запросов
CORS(app, supports_credentials = True, allow_methods = ['HEADER','GET','OPTIONS','POST','DELETE','PUT'], allow_headers = ['DNT','X-CustomHeader','Keep-Alive','User-Agent','X-Requested-With','If-Modified-Since','Cache-Control','Content-Type','Accept','Accept-Encoding','Accept-Language','Client-App'])
mysql = MySQL()
# MySQL configurations
# TODO config file
app.config['MYSQL_DATABASE_USER'] = 'icons8rw'
app.config['MYSQL_DATABASE_PASSWORD'] = 'aH5mwWnEQC4KjmsE'
app.config['MYSQL_DATABASE_DB'] = 'icons8'
app.config['MYSQL_DATABASE_HOST'] = 'localhost'
mysql.init_app(app)
# TODO connection pool / auto reconnect
conn = mysql.connect()
@app.route("/")
def main():
return render_template('index.html')
@app.route("/api/iconsets/v3/latest")
def iconsets_v3_latest():
amount = request.args.get('amount', 25)
offset = request.args.get('offset', 0)
pack = request.args.get('pack', '')
language = request.args.get('language', 'en-US')
cursor = mysql.connect().cursor(DictCursor)
queryArguments = {
'language': 'en',
'amount': int(amount),
'offset': int(offset),
}
queryText = """
SELECT
i.icon_id,
i.common_icon_id,
i.canonical_name,
i.created,
i.attr_style,
i.attr_case,
i.files_json,
i.platform_id,
i.first_icon_id
from icons_by_name_view_2 i
where i.lang = %(language)s
order by i.created desc, i.icon_id desc
limit %(amount)s
offset %(offset)s
"""
cursor.execute(queryText, queryArguments)
result = {
'parameters': {
'amount': amount,
'offset': offset,
'pack': pack,
'language': language,
},
'result': {
'latest': []
}
}
rows = cursor.fetchall()
for row in rows:
files = json.loads(row['files_json'])
item = {
'id': row['icon_id'],
'name': row['canonical_name'],
'platform': 'Color', #TODO platform name
'platform_code': 'color', #TODO platform code
'created': '2017-01-01 00:00:59+0000', # TODO date('c', strtotime($this->created)),
'filled': False, # TODO STYLE_FILLED
'url': '/web-app/1/cat', # TODO url
'common_icon_id': row['common_icon_id'],
}
item['category'] = { # TODO real category
'name': 'Very Basic',
'api_code': 'very_basic'
}
item['subcategory'] = { # TODO real subcategory
'name': 'Something',
'api_code': 'something'
}
item['svg'] = files['svg.plain']
result['result']['latest'].append(item)
return jsonify(result)
if __name__ == "__main__":
# app.run(host, port, debug, options)
app.run(host = '0.0.0.0')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment