from flask import Flask, request, jsonify
from flask_cors import CORS
import beike, lianjia, ziru, job51, lagou, fang, liepin, suning, jingdong, zhaopinBaidu
import FangScatter, FangMap, JobsWordsCloud, JobHist, GoodsSort
import os

app = Flask(__name__)

# 解决跨域
CORS(app, supports_credentials=True)

@app.route('/')
def hello():
    return 'Hello'

@app.route('/house', methods=['GET'])
def house():
    platform = request.args['platform']
    city = request.args['city']
    typeh = request.args['type']
    page = int(request.args['page'])
    try:
        if platform == 'beike':
            return beike.run(city, typeh, page)
        if platform == 'lianjia':
            return lianjia.run(city, typeh, page)
        # 只有二手房
        if platform == 'ziru':
            return ziru.run(city, page)
        # 只有二手房和租房两种类型
        if platform=='fang':
            return fang.run(city,typeh,page)
    except Exception as e:
        return jsonify({'message': str(e)})


@app.route('/job', methods=['GET'])
def job():
    platform = request.args['platform']
    city = request.args['city']
    job = request.args['job']
    page = int(request.args['page'])
    try:
        if platform == 'job51':
            return job51.run(city, job, page)
        if platform == 'lagou':
            return lagou.run(city, job, page)
        if platform == 'liepin':
            return liepin.run(city, job, page)
        if platform == 'baiduZhaopin':
            return zhaopinBaidu.run(city, job, page)
    except Exception as e:
        return jsonify({'code': 404, 'message': str(e)})


@app.route('/goods', methods=['GET'])
def goods():
    platform = request.args['platform']
    keyword = request.args['keyword']
    page = int(request.args['page'])
    try:
        if platform == 'jingdong':
            return jingdong.run(keyword, page)
        if platform == 'suning':
            return suning.run(keyword, page)          
    except Exception as e:
        return jsonify({'code': 404, 'message': str(e)})

@app.route('/houseDiagram', methods=['GET'])
def HouseDiagram():
    try:
        diagramType = request.args['diagramType']
        if diagramType == 'map':
            # 更新html
            # FangMap.MapShow('')
            return jsonify({'message': 'success'})
        else:
            platform = request.args.get('platform')
            field = request.args.get('field')
            city = request.args.get('city')
            typeh = request.args.get('type')
            filename = city + ' ' + typeh + '.csv'
            filepath = os.path.join('../data/houses/' + platform, filename)
            fields = FangScatter.Fang_Scatter(platform + '/' + city + ' ' + typeh, filepath, field)
            return jsonify({'message': 'success'})
    except Exception as e:
        return jsonify({'code': 404, 'message': str(e)})

@app.route('/jobDiagram', methods=['GET'])
def JobDiagram():
    try:
        dtype = request.args.get('type', '')
        platform = request.args.get('platform')
        city = request.args.get('city')
        jobName = request.args.get('job').capitalize()
        print(platform, jobName)
        if dtype == '':
            filename = city + ' ' + jobName + '.csv'
            filepath = os.path.join('../data/jobs/' + platform, filename)
            JobsWordsCloud.generateCloud(filepath, platform + ' ' + city + ' ' + jobName)
        elif dtype == 'hist':
            JobHist.JobHist('../data/jobs/' + platform, platform)
        return jsonify({'message': 'success'})
    except Exception as e:
        print(e)
        return jsonify({'code': 500, 'message': str(e)})

@app.route('/goodsDiagram', methods=['GET'])
def GoodsDiagram():
    try:
        platform = request.args['platform']
        print(platform)
        GoodsSort.GoodsSort('../data/goods/' + platform + '.csv', platform)
        return jsonify({'message': 'success'})
    except Exception as e:
        print(e)
        return jsonify({'code': 500, 'message': str(e)})

if __name__ == '__main__':
    # 不加这个 部署的时候会报错 address alread in use
    app.run(host='0.0.0.0', port=8080, debug=True)
    # FangMap.MapShow('../data', 'FangMapShow')
