#!/bin/env python # -*- coding: utf-8 -*- import sys if sys.platform == 'linux': sys.path.append('/opt/huawei/kg/appspace/cloudword') from flask import Flask, request, jsonify from flask_sqlalchemy import SQLAlchemy from sqlalchemy import func from sqlalchemy import Column, String, TIMESTAMP, text from sqlalchemy.dialects.mysql import INTEGER
app = Flask(__name__) app.config.from_object('conf') db = SQLAlchemy(app, use_native_unicode='utf8')
@app.route('/wisebrain/input/dict/v1/diff', methods=['POST']) def fetch_words_api(): assert request.method == 'POST' open_id, dict_version = request.form['openId'], request.form['dictVersion'] """ verified = verifyAPI(openId) if not verified: return """ dict_vers = VerDict.query.filter_by(dict_version=dict_version).all() if not len(dict_vers): return jsonify(dictList=[]) vers_lstime = dict_vers[0].last_time vers_latest = db.session.query(func.max(VerDict.last_time)).all() if not len(vers_latest): return jsonify(dictList=[]) vers_latestime = vers_latest[0][0] time_period = (vers_lstime, vers_latestime) info_query = db.session.query(DatabaseDict.word, DatabaseDict.pinyin_space, DatabaseDict.word_frequency, DatabaseDict.last_time, DatabaseDict.oper) info_filter = info_query.filter(DatabaseDict.last_time.between(*time_period)).all() # info_filter = info_query.filter(DatabaseDict.last_time.between('1547799998725','1547799998726')).all() res = [] for i in info_filter: char = ','.join([str(x) for x in i]) res.append(char) return jsonify(dictList=res)
class DatabaseDict(db.Model): __tablename__ = 'database_dict_v2' word_id = Column(String(128), primary_key=True) word = Column(String(128), nullable=False, index=True) pinyin_space = Column(String(512), nullable=False) pinyin_head = Column(String(128), nullable=False) pinyin_shengmu = Column(String(512), nullable=False) pinyin_yunmu = Column(String(512), nullable=False) word_frequency = Column(INTEGER(16), nullable=False) oper = Column(String(3), nullable=False, index=True) status = Column(String(3), nullable=False, index=True) create_time = Column(TIMESTAMP, nullable=False, server_default=text("CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP")) update_time = Column(TIMESTAMP, nullable=False, server_default=text("'0000-00-00 00:00:00'")) last_time = Column(String(15), nullable=False, index=True)
class UpdateWordDict(db.Model): __tablename__ = 'update_word_dict' id = Column(INTEGER(11), primary_key=True) start_verson = Column(INTEGER(10), nullable=False, index=True) end_verson = Column(INTEGER(10), nullable=False, index=True) word_id = Column(String(256), nullable=False) word = Column(String(128), nullable=False, index=True) pinyin_space = Column(String(512), nullable=False) pinyin_head = Column(String(128), nullable=False) pinyin_shengmu = Column(String(512), nullable=False) pinyin_yunmu = Column(String(512), nullable=False) word_frequency = Column(INTEGER(16), nullable=False) oper = Column(String(3), nullable=False) status = Column(String(3), nullable=False, index=True) create_time = Column(TIMESTAMP, nullable=False, server_default=text("CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP")) update_time = Column(TIMESTAMP, nullable=False, server_default=text("'0000-00-00 00:00:00'")) last_time = Column(String(15), nullable=False, index=True)
class VerDict(db.Model): __tablename__ = 'ver_dict_v2' dict_version = Column(String(15), primary_key=True, server_default=text("''")) word_count = Column(String(128), nullable=False) word_update = Column(String(128), nullable=False) status = Column(String(3), nullable=False) create_time = Column(TIMESTAMP, nullable=False, server_default=text("CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP")) update_time = Column(TIMESTAMP, nullable=False, server_default=text("'0000-00-00 00:00:00'")) last_time = Column(String(15), nullable=False, index=True)
if __name__ == '__main__': config = dict( host='localhost', port=8001, debug=True ) app.run(**config)