懒人记时 代码仓库
Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.
 
 
 
 
 

102 řádky
3.5 KiB

# python对多文件编程及其不友好, 白写了这么多文件到处报错, 最后还是要写在一个文件里解决了
import os
import sys
from datetime import datetime # 处理时间
from flask import Flask
from flask_cors import CORS, cross_origin
from flask_sqlalchemy import SQLAlchemy
app = Flask(__name__)
cors = CORS(app)
app.config['CORS_HEADERS'] = 'Content-Type'
prefix = "sqlite:///"
path = '../appTime/AppTime-Realease/data.db'
app.config['SQLALCHEMY_DATABASE_URI'] = prefix + path
app.config['SQLALCHEMY_COMMIT_ON_TEARDOWN'] = True
# 在扩展类实例化前加载配置
db = SQLAlchemy(app)
# 数据库模型
class App(db.Model):
id = db.Column(db.Integer, primary_key=True) # App id
process = db.Column(db.Text) # appName
text = db.Column(db.Text) # app窗口名称
tagId = db.Column(db.Integer, unique=True) # 标签id
class Period(db.Model):
timeStart = db.Column(db.DateTime, primary_key=True) # 开始时间
timeEnd = db.Column(db.DateTime) # 结束时间
winId = db.Column(db.Integer) # 活动id
class Tag(db.Model):
id = db.Column(db.Integer, primary_key=True) # 标签id
text = db.Column(db.Text) # 标签文本
class Win(db.Model):
id = db.Column(db.Integer, primary_key=True) # 活动id
appId = db.Column(db.Integer) # App id
text = db.Column(db.Text, nullable=True) # 活动窗口名称
tagId = db.Column(db.Integer, nullable=True) # 标签id
# 后端API
# 获取每日屏幕使用总时间
@app.route('/getDayScreenUseTime', methods=['GET'])
@cross_origin()
def getDayScreenUseTime():
datetimeStr = '2022-09-05'
struct_time = datetime.strptime(datetimeStr, "%Y-%m-%d").date() # 获取请求日期, struct_time数据类型为dateTime
activityTimeList = Period.query.all()
selectedTimeList = [] # 选中的对应今天日期的时间李彪
for time in activityTimeList:
if time.timeStart.date() == struct_time:
selectedTimeList.append(time)
timeAmount = 0
for time in selectedTimeList:
time_diff = time.timeEnd - time.timeStart
timeAmount = timeAmount + time_diff.total_seconds()
timeAmount = int(timeAmount)
m, s = divmod(timeAmount, 60)
h, m = divmod(m, 60)
return '{:d}小时{:02d}分{:02d}秒'.format(h, m, s)
# 获取每日第一次屏幕使用时刻
@app.route('/getFirstScreenTime', methods=['GET'])
@cross_origin()
def getFirstScreenTime():
datetimeStr = '2022-09-05'
struct_time = datetime.strptime(datetimeStr, "%Y-%m-%d").date() # 获取请求日期, struct_time数据类型为dateTime
activityTimeList = Period.query.all()
selectedTimeList = [] # 选中的对应今天日期的时间李彪
for time in activityTimeList:
if time.timeStart.date() == struct_time:
selectedTimeList.append(time)
break;
firstScreenTime = selectedTimeList[0].timeStart.strftime('%H点%M分%S秒')
return firstScreenTime
# 获取每日最后一次屏幕使用时刻
@app.route('/getLastScreenTime', methods=['GET'])
@cross_origin()
def getLastScreenTime():
datetimeStr = '2022-09-05'
struct_time = datetime.strptime(datetimeStr, "%Y-%m-%d").date() # 获取请求日期, struct_time数据类型为dateTime
activityTimeList = Period.query.all()
selectedTimeList = [] # 选中的对应今天日期的时间李彪
for time in activityTimeList:
if time.timeStart.date() == struct_time:
selectedTimeList.append(time)
lastScreenTime = selectedTimeList[-1].timeEnd.strftime('%H点%M分%S秒')
return lastScreenTime
# 持续时间