from flask import Flask,request,jsonify
|
from flask_cors import CORS
|
|
import sys
|
# sys.path.append('D:\\z\workplace\\VsCode\\pyvenv\\venv')
|
sys.path.append('../')
|
|
import src.Crawling as Crawling
|
import src.auto_login as login
|
import pandas as pd
|
from sqlalchemy import create_engine
|
import copy
|
|
# 写入四张表
|
import src.write_to_MySql as w_t_MySql
|
# 写入分钟历史表
|
import src.write_to_minute_table as w_t_minute
|
|
app = Flask(__name__)
|
# r'/*' 是通配符,让本服务器所有的 URL 都允许跨域请求
|
CORS(app,resource=r'/*')
|
|
session = -1
|
|
@app.route('/getData',methods=['POST'])
|
def get_data():
|
if request.method == 'POST':
|
data=request.get_json()
|
if(session != -1) :
|
# if(len(session.cookies.get_dict()) == 0):
|
# # session失效
|
# return '-2'
|
result,all_data=Crawling.pass_login(session,data.get('beginTime'),data.get('endTime'),data.get('selectedShopNames'))
|
print('\n\n爬取的所有的allData\n',all_data)
|
duplicate_data,new_data=is_duplicate(all_data)
|
# if(len(duplicate_data)==0):
|
# duplicate_data.append('无重复数据')
|
print('重复的数据为:',duplicate_data)
|
jso ={
|
# 反馈信息
|
'info':result,
|
# 所有数据
|
'allData':all_data,
|
# 重复的数据
|
'duplicate':duplicate_data,
|
# 新的数据
|
'newData':new_data
|
}
|
else :
|
# 未先登陆
|
return '-1'
|
return jsonify(jso)
|
|
# 写入数据库 写入4张表中
|
@app.route('/store',methods=['POST'])
|
def write_new():
|
if request.method =='POST':
|
data = request.get_json()
|
# print('data为:',data.get('allData'))
|
# print('要存入的数据条数为:',len(data.get('allData')))
|
# 写入数据库
|
w_t_MySql.write(data.get('allData'))
|
return '写入完成!'
|
|
# 写入数据库 只写入分钟数据表
|
@app.route('/minute',methods=['POST'])
|
def write_dup():
|
if request.method =='POST':
|
data = request.get_json()
|
# print('data为:',data.get('allData'))
|
# print('要存入的数据条数为:',len(data.get('allData')))
|
# 写入数据库
|
w_t_minute.write(data.get('allData'))
|
return '写入完成!'
|
|
|
|
# 自动登陆油烟网站 返回session对象
|
@app.route('/autologin',methods=['get'])
|
def auto_login():
|
global session
|
session = login.login_fume_web()
|
return '登陆成功'
|
|
|
|
|
# lst为要和数据库已存的数据进行比较,lst元素只需要3个字段。 返回值是重复的数据
|
def is_duplicate(lst):
|
temp=copy.deepcopy(lst)
|
# print('temp',temp)
|
# print('\n')
|
# 只保存3个字段
|
after_address=[]
|
for item in temp:
|
# print('item',item)
|
# print('\n')
|
a=[]
|
# 店铺名和设备编号
|
a.append(item[1])
|
# print('item1',item[1])
|
# print('\n')
|
|
a.append(item[2])
|
# 归属时间
|
time=str(item[11])+':00'
|
a.append(time)
|
# print('a',a)
|
# print('\n')
|
after_address.append(a)
|
|
engine = create_engine("mysql+mysqlconnector://root:1234@localhost:3306/fume?charset=utf8")
|
con_read = engine.connect()
|
|
df = pd.read_sql('SELECT b.DI_Name,a.MV_Stat_Code,a.MV_Data_Time,a.MV_Isduplication FROM fd_t_minutevalue AS a JOIN ea_t_device_info AS b ON a.MV_Stat_Code = b.DI_Code',con=con_read)
|
# 将Timestamp时间对象转为正常时间字符串
|
df['MV_Data_Time'] = df['MV_Data_Time'].dt.strftime('%Y-%m-%d %H:%M:%S')
|
con_read.close() #关闭链接
|
|
existing_data = df.values.tolist() #DateFrame按照行转成list类型,res存放的是设备信息表中的数据
|
|
# print('已处理:',after_address)
|
# print('已存在',existing_data)
|
|
# 保存重复值
|
duplicate_data=[]
|
# 保存新的数据值
|
new_data=[]
|
for index,value in enumerate(after_address):
|
# print('value是1',value)
|
flag,duplication_num = compare1(value,existing_data)
|
if flag:
|
duplicate_data.append(lst[index])
|
# 重复数据时,重复字段值在原来基础上加1
|
duplicate_data[-1][-1] = duplication_num+1
|
else:
|
new_data.append(lst[index])
|
return duplicate_data,new_data
|
|
|
# 比较 new_data仅仅为网页的表格的一行数据 也就是列表中的某一个元素
|
def compare1(new_data,old_data):
|
# 取到重复字段的最大值才返回
|
# 保存的是重复字段最大值
|
max=-1
|
for item in old_data:
|
if(new_data == item[0:3]):
|
if(item[3]>max):
|
max = item[3]
|
# 还返回了原始数据的重复字段值
|
# return True,item[3]
|
|
# max值改变了,那么一段是是相等的情况
|
if(max!= -1):
|
return True,max
|
else:
|
return False,max
|
|
|
app.run(debug=True,port=8089)
|