6月26日 接口3.0

接口2.0上线

添加了几个新的数据表,并对之前的表结构做了修改优化

app.py

from flask import request
from flask import Flask, jsonify, render_template
import utils

import json
from flask_sqlalchemy import SQLAlchemy

app_1 = Flask(__name__)
app_1.config['SQLALCHEMY_DATABASE_URI'] = 'mysql+pymysql://test:123456@47.98.141.4:3306/innotrain?charset=utf8mb4'
app_1.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False
db = SQLAlchemy(app_1)


class rawdata(db.Model):
    __tablename__ = 'raw_data'
    id = db.Column(db.Integer, primary_key=True)
    context = db.Column(db.String())
    ds = db.Column(db.String())
    # ds = db.Column(db.Date())
    topic = db.Column(db.String())

    def __init__(self, context, ds, topic):

        self.ds = ds
        self.topic = topic
        self.context = context
        # self.id = id

    def __repr__(self):
        return '<rawdata:%s %s %s %s>' % (self.context, self.ds, self.topic,
                                          self.platform)


class emotion_val(db.Model):
    __tablename__ = 'emotion_val'
    id = db.Column(db.Integer, primary_key=True)
    predict = db.Column(db.Integer())
    emotion_val = db.Column(db.String())
    ds = db.Column(db.String())
    # ds = db.Column(db.Date())
    topic = db.Column(db.String())

    def __init__(self, emotion_val, ds, topic, predict):
        self.emotion_val = emotion_val
        self.predict = predict
        self.ds = ds
        self.topic = topic

    def __repr__(self):
        return '<rawdata:%s %s %s %s>' % (self.emotion_val, self.ds,
                                          self.topic, self.predict)


class toutiao(db.Model):
    __tablename__ = 'toutiao'
    id = db.Column(db.Integer, primary_key=True)
    title = db.Column(db.String())
    ds = db.Column(db.String())
    # ds = db.Column(db.Date())
    topic = db.Column(db.String())
    content = db.Column(db.TEXT)

    def __init__(self, title, ds, content, topic):
        self.title = title
        self.ds = ds
        self.topic = topic
        self.content = content
        # self.id = id

    def __repr__(self):
        return '<toutiao:%s %s %s %s>' % (self.title, self.ds, self.content, self.topic)


class zhihu_a(db.Model):
    __tablename__ = 'zhihu_a'
    id = db.Column(db.Integer, primary_key=True)
    question_name = db.Column(db.String())
    answer_content = db.Column(db.String())
    voteup_count = db.Column(db.String())
    comment_count = db.Column(db.String())
    created_time = db.Column(db.String())
    updated_time = db.Column(db.String())
    topic = db.Column(db.String())

    def __init__(self, question_name, answer_content,
                 voteup_count, comment_count, created_time,
                 updated_time, topic):
        self.answer_content = answer_content
        self.question_name = question_name
        self.voteup_count = voteup_count
        self.comment_count = comment_count
        self.created_time = created_time
        self.updated_time = updated_time
        self.topic = topic

    def __repr__(self):
        return '<zhihu_a:%s %s %s %s %s %s %s>' % (self.answer_content, self.question_name,
                                                   self.voteup_count, self.comment_count,
                                                   self.created_time, self.updated_time,
                                                   self.topic)


class zhihu_ques(db.Model):
    __tablename__ = 'zhihu_ques'
    id = db.Column(db.Integer, primary_key=True)
    question = db.Column(db.String())
    description = db.Column(db.String())
    follower_count = db.Column(db.String)
    comment_count = db.Column(db.String)
    answer_count = db.Column(db.String)
    visits_count = db.Column(db.String)
    updated_time = db.Column(db.String)
    topic = db.Column(db.String())

    def __init__(self, question, description,follower_count,comment_count,
                 answer_count,visits_count,updated_time, topic):
        self.updated_time = updated_time
        self.question = question
        self.description = description
        self.follower_count = follower_count
        self.comment_count = comment_count
        self.answer_count = answer_count
        self.visits_count = visits_count
        self.topic = topic


    def __repr__(self):
        return '<zhihu_ques:%s %s %s %s %s %s %s>' % (self.question, self.description,
                                                      self.follower_count,self.comment_count,
                                                      self.answer_count,self.visits_count,
                                                      self.updated_time,self.topic)


@app_1.route('/')
def hello_world():
    name = request.form.get('email1')
    if name == None:
        return render_template("index.html")
    else:
        # 这里调用爬虫
        return render_template("demo.html")


# 获取预测数据的接口
@app_1.route('/get_predict')
def get_predict():
    para = request.args.get('topic')
    data = utils.get_predict(para)
    ds = []
    emotion_val = []
    topic = []
    for each in data:
        ds.append(each[0])
        emotion_val.append(each[1])
        topic.append(each[2])

    return jsonify({"ds": ds, "emotion_val": emotion_val, "topic": topic})


# 获取预测数据和二次处理后真实数据两者的接口
@app_1.route('/get_emotionval')
def get_emotionval():
    para = request.args.get('topic')
    data = utils.get_emotion_val(para)
    ds = []
    emotion_val = []
    topic = []
    predict = []
    for each in data:
        ds.append(each[0])
        emotion_val.append(each[1])
        topic.append(each[2])
        predict.append(each[3])
    return jsonify({"ds": ds, "emotion_val": emotion_val,
                    "topic": topic, "predict": predict})


# 获取爬虫爬取数据数据的接口,杨涛用
@app_1.route('/get_rawdata')
def get_rawdata():
    para = request.args.get('topic')
    data = utils.get_rawdata(para)
    # print(data)
    ds = []
    platform = []
    context = []
    topic = []
    for each in data:
        # print(each)
        ds.append(each[1])
        context.append(each[0])
        topic.append(each[2])
    return jsonify({"ds": ds, "context": context, "topic": topic})


# 获取二次处理后真实数据的接口,杨秀辉用
@app_1.route('/get_midrawdata')
def get_midrawdata():
    para = request.args.get('platform')
    data = utils.get_midrawdata(para)
    ds = []
    emotion_val = []
    topic = []
    for each in data:
        ds.append(each[0])
        emotion_val.append(each[1])
        topic.append(each[2])
    return jsonify({"ds": ds, "emotion_val": emotion_val, "topic": topic})


# # 获取头条数据的接口
# @app.route('/get_toutiao')
# def get_toutiao():
#     para = request.args.get('topic')
#     data = utils.get_toutiao(para)
#     title = []
#     ds = []
#     content = []
#     topic = []
#     for each in data:
#         title.append(each[0])
#         ds.append(each[1])
#         content.append(each[2])
#         topic.append(each[3])
#
#     return jsonify({"title": title, "ds": ds, "content": content, "topic": topic})
#

# 获取头条数据的接口
@app_1.route('/get_zhihu_a')
def get_zhihu_a():
    para = request.args.get('topic')
    data = utils.get_zhihu_a(para)
    question_name = []
    answer_content = []
    voteup_count = []
    comment_count = []
    created_time = []
    updated_time = []
    topic = []
    for each in data:
        question_name.append(each[0])
        answer_content.append(each[1])
        voteup_count.append(each[2])
        comment_count.append(each[3])
        created_time.append(each[4])
        updated_time.append(each[5])
        topic.append(each[6])

    return jsonify({"question_name": question_name, "answer_content": answer_content,
                    "voteup_count": voteup_count, "comment_count": comment_count,
                    "created_time": created_time, "updated_time": updated_time,
                    "topic": topic})


# 获取头条数据的接口
@app_1.route('/get_zhihu_q')
def get_zhihu_q():
    para = request.args.get('topic')
    data = utils.get_zhihu_q(para)
    question = []
    description = []
    follower_count = []
    comment_count = []
    answer_count = []
    visits_count = []
    updated_time = []
    topic = []
    for each in data:
        question.append(each[0])
        description.append(each[1])
        follower_count.append(each[2])
        comment_count.append(each[3])
        answer_count.append(each[4])
        visits_count.append(each[5])
        updated_time.append(each[6])
        topic.append(each[7])

    return jsonify({"question": question, "description": description,
                    "follower_count":follower_count,"comment_count":comment_count,
                    "answer_count":answer_count,"visits_count":visits_count,
                    "updated_time":updated_time,"topic": topic})


# 上传爬虫爬取数据的接口,李哲荀用
@app_1.route('/insert_rawdata', methods=['GET', 'POST'])
def insert_rawdata():
    if not request.data:
        return ('no data!')
    print("This is a " + request.method + " method!")
    # 先将收到json数据的编码格式从bytes改成utf-8
    rawdata_json = request.data.decode('utf-8')
    rawdata_dict = json.loads(rawdata_json)
    for raw in rawdata_dict['data']:
        db.session.add(rawdata(context=raw['context'], ds=raw['ds'],
                               topic=raw['topic']))
    db.session.commit()
    return "ok"


# 上传二次处理数据和预测得到数据的接口,杨涛,杨秀辉用
@app_1.route('/insert_emotionval', methods=['GET', 'POST'])
def insert_emotionval():
    if not request.data:
        return ('no data!')
    print("This is a " + request.method + " method!")
    # 先将收到json数据的编码格式从bytes改成utf-8
    emoval_json = request.data.decode('utf-8')
    # print(emoval_json)
    # json.loads()将json格式的数据解码为python的dict格式数据
    # json.dumps()将python的dict格式的数据编码为json格式数据
    emoval_dict = json.loads(emoval_json)
    # print(type(emoval_dict))
    # print ((rawdata_dict['ds']))
    # print(ds)
    # rawdata=request.data
    for raw in emoval_dict['data']:
        # print(raw)
        db.session.add(
            emotion_val(emotion_val=raw['emotion_val'], ds=raw['ds'], topic=raw['topic'], predict=raw['predict']))
    db.session.commit()
    return "ok"

# @app_1.route('/insert_toutiao', methods=['GET', 'POST'])
# def insert_toutiao():
#     if not request.data:
#         return ('no data!')
#     print("This is a " + request.method + " method!")
#     # 先将收到json数据的编码格式从bytes改成utf-8
#     toutiaodata_json = request.data.decode('utf-8')
#     toutiaodata_dict = json.loads(toutiaodata_json)
#     for raw in toutiaodata_dict['data']:
#         db.session.add(toutiao(title=raw['title'],ds=raw['ds'],content=raw['content'],topic=raw['topic']))
#     db.session.commit()
#     return "ok"
@app_1.route('/insert_zhihu_a', methods=['GET', 'POST'])
def insert_zhihu_a():
    if not request.data:
        return ('no data!')
    print("This is a " + request.method + " method!")
    # 先将收到json数据的编码格式从bytes改成utf-8
    zhihu_adata_json = request.data.decode('utf-8')
    zhihu_adata_dict = json.loads(zhihu_adata_json)
    for raw in zhihu_adata_dict['data']:
        db.session.add(zhihu_a(question_name=raw['question_name'], answer_content=raw['answer_content'],
                               voteup_count=raw['voteup_count'], comment_count=raw['comment_count'],
                               created_time=raw['created_time'],updated_time=raw['updated_time'],
                               topic=raw['topic']))
    db.session.commit()
    return "ok"
@app_1.route('/insert_zhihu_q', methods=['GET', 'POST'])
def insert_zhihu_q():
    if not request.data:
        return ('no data!')
    print("This is a " + request.method + " method!")
    # 先将收到json数据的编码格式从bytes改成utf-8
    zhihu_qdata_json = request.data.decode('utf-8')
    zhihu_qdata_dict = json.loads(zhihu_qdata_json)
    for raw in zhihu_qdata_dict['data']:
        print(raw)
        zhihu = zhihu_ques(question=raw['question'], description=raw['description'],
                               follower_count=raw['follower_count'],comment_count=raw['comment_count'],
                               answer_count=raw['answer_count'],visits_count=['visits_count'],
                               updated_time=['updated_time'],topic=raw['topic'])
        # db.session.add(zhihu_ques(question=raw['question'], description=raw['description'],
        #                        follower_count=raw['follower_count'],comment_count=raw['comment_count'],
        #                        answer_count=raw['answer_count'],visits_count=['visits_count'],
        #                        updated_time=['updated_time'],topic=raw['topic']))
        db.session.add(zhihu)
        print(zhihu.updated_time)
    db.session.commit()
    return "ok"

@app_1.route("/time")
def get_time():
    return utils.get_time()


if __name__ == '__main__':
    app_1.run(host="0.0.0.0",port=80)

utils.py

import time
import pymysql
import csv

def get_time():
    time_str =  time.strftime("%Y{}%m{}%d{} %X")
    return time_str.format("年","月","日")

def get_conn():
    """
    :return: 连接,游标
    """
    # 创建连接
    conn = pymysql.connect(host="47.98.141.4",
                           # port=3306,
                           user="test",
                           password="123456",
                           db="innotrain",
                           charset="utf8mb4")
    # 创建游标
    cursor = conn.cursor()# 执行完毕返回的结果集默认以元组显示
    return conn, cursor

def close_conn(conn, cursor):
    cursor.close()
    conn.close()

def query(sql,*args):
    """
    封装通用查询
    :param sql:
    :param args:
    :return: 返回查询到的结果,((),(),)的形式
    """
    conn, cursor = get_conn()
    cursor.execute(sql)
    # cursor.execute(sql,args)
    res = cursor.fetchall()
    close_conn(conn, cursor)
    return res

def get_predict(plat):
    sql = "select ds,emotion_val,topic, platform from emotion_val " \
          "where predict=1 and platform =%s order by ds"%plat
    res = query(sql)
    return res

# def get_toutiao(topic):
#     sql = "select title,ds,content,topic from toutiao " \
#           "where topic =%s order by ds"%topic
#     res = query(sql)
#     return res

def get_zhihu_a(topic):
    sql = "select question_name,answer_content,voteup_count," \
          "comment_count,created_time,updated_time,topic" \
          " from zhihu_a where topic =%s order by updated_time"%topic
    res = query(sql)
    return res

def get_zhihu_q(topic):
    sql = "select question,description," \
          "follower_count,comment_count,answer_count," \
          "visits_count,updated_time,topic from zhihu_ques " \
          "where topic =%s order by topic"%topic
    res = query(sql)
    return res

def get_emotion_val(topic):
    sql = "select ds,emotion_val,topic,predict from emotion_val where topic =%s order by predict"%topic
    res = query(sql)
    return res

def get_rawdata(topic):
    sql = "select context,ds,topic from raw_data where topic =%s "%topic
    res = query(sql)
    return res

def get_midrawdata(topic):
    sql = "select ds,emotion_val,topic from emotion_val where predict=0 and topic =%s order by ds"%topic
    res = query(sql)
    return res


# def get_l1_data():
#
# 	sql = "select ds,confirm,suspect,heal,dead from history"
# 	res = query(sql)
# 	return res
#
# def get_l2_data():
#
# 	sql = "select ds,confirm_add,suspect_add from history"
# 	res = query(sql)
# 	return res
#将CSV文件处理为dict在转换为json格式数据
# def dataprocess(file):



if __name__ == "__main__":
    print()
展开阅读全文
©️2020 CSDN 皮肤主题: 大白 设计师: CSDN官方博客 返回首页
实付0元
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、C币套餐、付费专栏及课程。

余额充值