1 Star 2 Fork 0

心易/PlugLink_您的自动化机器人

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
Web_Actions.py 23.45 KB
一键复制 编辑 原始数据 按行查看 历史
zhengqia 提交于 2024-05-19 21:13 . Resolve merge conflicts
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743
<<<<<<< HEAD
from flask import Flask, request, jsonify, Blueprint, render_template,Response
import sqlite3,os,json,requests,glob,random,string,re,sys,webbrowser,platform,subprocess,chardet,shutil
from datetime import datetime, timedelta
from bs4 import BeautifulSoup
from Ini_sys import *
from Ini_DB import *
web_blueprint = Blueprint('web', __name__)
"""<快捷方式操作网址相关函数>"""
# 插入数据到数据库的函数
def insert_website(WType, UserID, URL, Title, Des, CreDate, UbDate, Utimes):
try:
conn = sqlite3.connect(db_path)
cursor = conn.cursor()
cursor.execute("INSERT INTO MyFast (WType, UserID, URL, Title, Des, CreDate, UbDate, Utimes) VALUES (?, ?, ?, ?, ?, ?, ?, ?)", (WType, UserID, URL, Title, Des, CreDate, UbDate, Utimes))
conn.commit()
except sqlite3.Error as e:
print(f"数据库错误: {e}")
finally:
conn.close()
# 调用函数添加网址到数据库
def add_website():
# 获取JSON数据
data = request.json
url = data.get('url')
title = data.get('title')
description = data.get('description')
# 当前时间
now = datetime.now()
formatted_date = now.strftime('%Y-%m-%d %H:%M:%S')
# 插入数据到数据库
insert_website('website',0,url, title, description, formatted_date, formatted_date, 0)
return {"message": "添加成功"} # 返回一个可以被 jsonify 序列化的字典
# 添加插件到数据库
def add_plugin():
# 获取JSON数据
data = request.json
plugin_id = data.get('plugin_id')
url = data.get('plug_url')
title = data.get('plug_name')
description = data.get('plug_description')
# 检查URL是否已经存在
existing_urls = DB_select_return('MyFast','URL', url)
if existing_urls:
return {"message": "该插件已添加过"}
# 查找插件信息
print(plugin_id)
DB_JSON_List=query_db('MyPlugins', f'ID={plugin_id}')
DB_JSON = DB_JSON_List[0]
print(DB_JSON)
print(DB_JSON['PlugName'])
# 当前时间
now = datetime.now()
formatted_date = now.strftime('%Y-%m-%d %H:%M:%S')
# 插入数据到数据库
insert_website('plugin', 0, url, DB_JSON['PlugName'], DB_JSON['PlugDes'], formatted_date, formatted_date, 0)
return {"message": "添加成功"} # 返回一个可以被 jsonify 序列化的字典
#检测网址,请求TD
def fetch_title_and_description(url):
try:
response = requests.get(url, timeout=10)
response.raise_for_status() # 检查请求是否成功
# 自动检测字符编码
detected_encoding = chardet.detect(response.content)['encoding']
# 如果检测到编码,则使用该编码解码响应内容
if detected_encoding:
response.encoding = detected_encoding
else:
# 默认编码
response.encoding = 'utf-8'
# 解析HTML内容
soup = BeautifulSoup(response.text, 'html.parser')
# 尝试获取<title>标签内容作为网页标题
title = soup.find('title').string if soup.find('title') else '获取不到标题'
# 尝试获取<meta name="description">标签的内容
description = soup.find('meta', attrs={'name': 'description'})
description = description['content'] if description else '获取不到描述'
return title, description
except Exception as e:
return '链接失败', '链接失败'
# 删除快捷方式
def del_myfast(id):
try:
conn = sqlite3.connect(db_path)
cursor = conn.cursor()
# 执行删除操作
cursor.execute("DELETE FROM MyFast WHERE ID = ?", (id,))
conn.commit()
except sqlite3.Error as e:
print(f"数据库操作错误: {e}")
return False
finally:
conn.close()
return True
# 快捷方式记数
def Utimes_myfast(id):
try:
conn = sqlite3.connect(db_path) # 数据库文件路径
cursor = conn.cursor()
# 更新Utimes字段,使其值加1
update_query = "UPDATE MyFast SET Utimes = Utimes + 1 WHERE ID = ?"
cursor.execute(update_query, (id,))
conn.commit() # 提交事务
conn.close() # 关闭连接
return True
except:
return False
"""<读取快捷方式列表相关函数>"""
# 提取7天天数和日期
def get_days_ago(ub_date_str):
ub_date = datetime.strptime(ub_date_str, "%Y-%m-%d %H:%M:%S")
delta = datetime.now() - ub_date
if delta.days <= 7:
return f"{delta.days} 天前"
else:
return ub_date_str
# 提取快捷方式列表
def fetch_myfast():
# 连接到SQLite数据库
# 数据库文件存放路径
conn = sqlite3.connect(db_path)
cursor = conn.cursor()
# 执行查询
query = "SELECT ID,WType, URL, Title, Des, UbDate, Utimes FROM MyFast ORDER BY UbDate DESC"
cursor.execute(query)
items = cursor.fetchall()
# 关闭数据库连接
conn.close()
# 处理每条记录
formatted_items = []
for item in items:
ID, WType, URL, Title, Des, UbDate, Utimes = item
if WType == "website":
icon_class = "layui-icon-website"
else:
icon_class = "layui-icon-component"
UbDate = get_days_ago(UbDate)
formatted_items.append({
"icon_class": icon_class,
"ID": ID,
"URL": URL,
"Title": Title,
"Des": Des,
"UbDate": UbDate,
"Utimes": Utimes
})
return formatted_items
"""<AI网址大全相关函数>"""
#获取AI网址中的表名
def get_sheet_names():
"""从数据库中读取所有唯一的sheet_name"""
# 连接到SQLite数据库
conn = sqlite3.connect(db_path)
cur = conn.cursor()
# 执行查询操作,选择不重复的sheet_name
cur.execute("SELECT DISTINCT sheet_name FROM AIURL")
# 获取所有结果
sheets = cur.fetchall()
# 关闭游标和连接
cur.close()
conn.close()
# 将结果转换为列表
unique_sheet_names = [sheet[0] for sheet in sheets]
return unique_sheet_names
# 获取所有字段名
def get_table_columns():
"""获取指定表的所有字段名"""
conn = sqlite3.connect(db_path)
cur = conn.cursor()
# 使用PRAGMA table_info()语句获取表的元数据
cur.execute(f"PRAGMA table_info(AIURL)")
# 获取所有字段的信息,每个字段的信息是一个元组,其中第二个元素是字段名
columns_info = cur.fetchall()
# 从每个字段的信息中提取字段名
column_names = [info[1] for info in columns_info if info[1] != 'sheet_name']
cur.close()
conn.close()
return column_names
# 读取表中所有内容
def get_all_records():
"""获取指定表的所有记录"""
# 连接到数据库
conn = sqlite3.connect(db_path)
cur = conn.cursor()
# 构建查询语句,选择所有字段
query = f"SELECT * FROM AIURL"
# 执行查询
cur.execute(query)
# 获取所有记录
records = cur.fetchall()
# 关闭游标和连接
cur.close()
conn.close()
return records
# 临时文件中心表格显示
def file_center_list():
# 连接到SQLite数据库
conn = sqlite3.connect(db_path)
cursor = conn.cursor()
# 查询数据库
cursor.execute("SELECT PlugName, PlugDir, uploadDir FROM MyPlugins")
plugins = cursor.fetchall()
# 存储结果的列表
result = []
# 遍历每个插件
for plug_name, plug_dir, upload_dir in plugins:
if not upload_dir:
print(f"插件 {plug_name} 的 uploadDir 字段为空")
continue
# 分割uploadDir字段得到子目录
directories = upload_dir.split(',')
for dir in directories:
full_path = os.path.join("plugins", plug_dir, dir)
if not os.path.exists(full_path):
print(f"目录 {full_path} 不存在")
continue
# 计算目录下的文件数和总占用空间
file_count = 0
total_size = 0
for root, dirs, files in os.walk(full_path):
file_count += len(files)
total_size += sum(os.path.getsize(os.path.join(root, name)) for name in files)
# 占用空间转为MB
total_size_mb = round(total_size / (1024 * 1024), 2)
# 将结果加入列表
result.append({
"PlugName": plug_name,
"Directory": full_path,
"FileCount": file_count,
"TotalSizeMB": total_size_mb
})
# 关闭数据库连接
conn.close()
# 转化结果为JSON
#fc_json = result
#fc_json = json.dumps(result)
#return fc_json
return {"code": 0, "msg": "", "count": len(result), "data": result}
# 删除给定的目录列表及其所有内容
def delete_directories(directories):
""" 删除给定的目录列表及其所有内容。 """
try:
for directory in directories:
full_path = os.path.join(directory) # 组合成完整路径
shutil.rmtree(full_path) # 删除目录及其所有内容
return {"code": 0, "msg": "删除成功"}
except Exception as e:
return {"code": 500, "msg": str(e)}
"""<一般web端路由交互执行>"""
@web_blueprint.route('/', methods=['POST'])
def handle_web_Execution():
if request.is_json: # 确保请求包含 JSON 数据
data = request.get_json()
action = data.get('action')
url = data.get('url')
id = data.get('id')
print(f'当前于web路由action状态码:{action}')
print(file_center_list())
match action:
case 'fetch_myfast':# 刷新快捷方式
results = fetch_myfast()
return render_template('add_myfast.html', items=results)
case 'add_website':# 添加网址到快捷方式
response_data = add_website()
return jsonify(response_data)
case 'add_plugin':# 添加插件到快捷方式
response_data = add_plugin()
return jsonify(response_data)
case 'conn_website':# 检测网址
title, description = fetch_title_and_description(url)
return jsonify({'title': title, 'description': description})
case 'del_fast':#删除快捷方式
if del_myfast(id):
return jsonify({"message": "删除成功"})
else:
return jsonify({"error": "删除失败"}), 500
case 'utimes':#快捷方式计数
if Utimes_myfast(id):
return jsonify({'message': 'Utimes_myfast success'})
else:
return jsonify({'status': 'error', 'message': 'Missing id'}), 400
case 'file_center_list': # 临时文件中心表格显示
return file_center_list()
case 'del_upload_file': # 临时文件中心表格显示
directories = data.get('directories', [])
result = delete_directories(directories)
return jsonify(result)
case 'AIURL_list':#读取网址大全列表
unique_sheet_names = get_sheet_names()
unique_column_names = get_table_columns()
unique_all_records = get_all_records()
return render_template('AIURL_List.html',
sheet_names=unique_sheet_names,
column_names=unique_column_names,
all_records=unique_all_records)
case _:
return jsonify({"error": "Invalid action or request method"}), 400
else:
return jsonify({'error': 'Invalid Content-Type'}), 400
=======
from flask import Flask, request, jsonify, Blueprint, render_template,Response
import sqlite3,os,json,requests,glob,random,string,re,sys,webbrowser,platform,subprocess,chardet,shutil
from datetime import datetime, timedelta
from bs4 import BeautifulSoup
from Ini_sys import *
from Ini_DB import *
web_blueprint = Blueprint('web', __name__)
"""<快捷方式操作网址相关函数>"""
# 插入数据到数据库的函数
def insert_website(WType, UserID, URL, Title, Des, CreDate, UbDate, Utimes):
try:
conn = sqlite3.connect(db_path)
cursor = conn.cursor()
cursor.execute("INSERT INTO MyFast (WType, UserID, URL, Title, Des, CreDate, UbDate, Utimes) VALUES (?, ?, ?, ?, ?, ?, ?, ?)", (WType, UserID, URL, Title, Des, CreDate, UbDate, Utimes))
conn.commit()
except sqlite3.Error as e:
print(f"数据库错误: {e}")
finally:
conn.close()
# 调用函数添加网址到数据库
def add_website():
# 获取JSON数据
data = request.json
url = data.get('url')
title = data.get('title')
description = data.get('description')
# 当前时间
now = datetime.now()
formatted_date = now.strftime('%Y-%m-%d %H:%M:%S')
# 插入数据到数据库
insert_website('website',0,url, title, description, formatted_date, formatted_date, 0)
return {"message": "添加成功"} # 返回一个可以被 jsonify 序列化的字典
# 添加插件到数据库
def add_plugin():
# 获取JSON数据
data = request.json
plugin_id = data.get('plugin_id')
url = data.get('plug_url')
title = data.get('plug_name')
description = data.get('plug_description')
# 检查URL是否已经存在
existing_urls = DB_select_return('MyFast','URL', url)
if existing_urls:
return {"message": "该插件已添加过"}
# 查找插件信息
print(plugin_id)
DB_JSON_List=query_db('MyPlugins', f'ID={plugin_id}')
DB_JSON = DB_JSON_List[0]
print(DB_JSON)
print(DB_JSON['PlugName'])
# 当前时间
now = datetime.now()
formatted_date = now.strftime('%Y-%m-%d %H:%M:%S')
# 插入数据到数据库
insert_website('plugin', 0, url, DB_JSON['PlugName'], DB_JSON['PlugDes'], formatted_date, formatted_date, 0)
return {"message": "添加成功"} # 返回一个可以被 jsonify 序列化的字典
#检测网址,请求TD
def fetch_title_and_description(url):
try:
response = requests.get(url, timeout=10)
response.raise_for_status() # 检查请求是否成功
# 自动检测字符编码
detected_encoding = chardet.detect(response.content)['encoding']
# 如果检测到编码,则使用该编码解码响应内容
if detected_encoding:
response.encoding = detected_encoding
else:
# 默认编码
response.encoding = 'utf-8'
# 解析HTML内容
soup = BeautifulSoup(response.text, 'html.parser')
# 尝试获取<title>标签内容作为网页标题
title = soup.find('title').string if soup.find('title') else '获取不到标题'
# 尝试获取<meta name="description">标签的内容
description = soup.find('meta', attrs={'name': 'description'})
description = description['content'] if description else '获取不到描述'
return title, description
except Exception as e:
return '链接失败', '链接失败'
# 删除快捷方式
def del_myfast(id):
try:
conn = sqlite3.connect(db_path)
cursor = conn.cursor()
# 执行删除操作
cursor.execute("DELETE FROM MyFast WHERE ID = ?", (id,))
conn.commit()
except sqlite3.Error as e:
print(f"数据库操作错误: {e}")
return False
finally:
conn.close()
return True
# 快捷方式记数
def Utimes_myfast(id):
try:
conn = sqlite3.connect(db_path) # 数据库文件路径
cursor = conn.cursor()
# 更新Utimes字段,使其值加1
update_query = "UPDATE MyFast SET Utimes = Utimes + 1 WHERE ID = ?"
cursor.execute(update_query, (id,))
conn.commit() # 提交事务
conn.close() # 关闭连接
return True
except:
return False
"""<读取快捷方式列表相关函数>"""
# 提取7天天数和日期
def get_days_ago(ub_date_str):
ub_date = datetime.strptime(ub_date_str, "%Y-%m-%d %H:%M:%S")
delta = datetime.now() - ub_date
if delta.days <= 7:
return f"{delta.days} 天前"
else:
return ub_date_str
# 提取快捷方式列表
def fetch_myfast():
# 连接到SQLite数据库
# 数据库文件存放路径
conn = sqlite3.connect(db_path)
cursor = conn.cursor()
# 执行查询
query = "SELECT ID,WType, URL, Title, Des, UbDate, Utimes FROM MyFast ORDER BY UbDate DESC"
cursor.execute(query)
items = cursor.fetchall()
# 关闭数据库连接
conn.close()
# 处理每条记录
formatted_items = []
for item in items:
ID, WType, URL, Title, Des, UbDate, Utimes = item
if WType == "website":
icon_class = "layui-icon-website"
else:
icon_class = "layui-icon-component"
UbDate = get_days_ago(UbDate)
formatted_items.append({
"icon_class": icon_class,
"ID": ID,
"URL": URL,
"Title": Title,
"Des": Des,
"UbDate": UbDate,
"Utimes": Utimes
})
return formatted_items
"""<AI网址大全相关函数>"""
#获取AI网址中的表名
def get_sheet_names():
"""从数据库中读取所有唯一的sheet_name"""
# 连接到SQLite数据库
conn = sqlite3.connect(db_path)
cur = conn.cursor()
# 执行查询操作,选择不重复的sheet_name
cur.execute("SELECT DISTINCT sheet_name FROM AIURL")
# 获取所有结果
sheets = cur.fetchall()
# 关闭游标和连接
cur.close()
conn.close()
# 将结果转换为列表
unique_sheet_names = [sheet[0] for sheet in sheets]
return unique_sheet_names
# 获取所有字段名
def get_table_columns():
"""获取指定表的所有字段名"""
conn = sqlite3.connect(db_path)
cur = conn.cursor()
# 使用PRAGMA table_info()语句获取表的元数据
cur.execute(f"PRAGMA table_info(AIURL)")
# 获取所有字段的信息,每个字段的信息是一个元组,其中第二个元素是字段名
columns_info = cur.fetchall()
# 从每个字段的信息中提取字段名
column_names = [info[1] for info in columns_info if info[1] != 'sheet_name']
cur.close()
conn.close()
return column_names
# 读取表中所有内容
def get_all_records():
"""获取指定表的所有记录"""
# 连接到数据库
conn = sqlite3.connect(db_path)
cur = conn.cursor()
# 构建查询语句,选择所有字段
query = f"SELECT * FROM AIURL"
# 执行查询
cur.execute(query)
# 获取所有记录
records = cur.fetchall()
# 关闭游标和连接
cur.close()
conn.close()
return records
# 临时文件中心表格显示
def file_center_list():
# 连接到SQLite数据库
conn = sqlite3.connect(db_path)
cursor = conn.cursor()
# 查询数据库
cursor.execute("SELECT PlugName, PlugDir, uploadDir FROM MyPlugins")
plugins = cursor.fetchall()
# 存储结果的列表
result = []
# 遍历每个插件
for plug_name, plug_dir, upload_dir in plugins:
if not upload_dir:
print(f"插件 {plug_name} 的 uploadDir 字段为空")
continue
# 分割uploadDir字段得到子目录
directories = upload_dir.split(',')
for dir in directories:
full_path = os.path.join("plugins", plug_dir, dir)
if not os.path.exists(full_path):
print(f"目录 {full_path} 不存在")
continue
# 计算目录下的文件数和总占用空间
file_count = 0
total_size = 0
for root, dirs, files in os.walk(full_path):
file_count += len(files)
total_size += sum(os.path.getsize(os.path.join(root, name)) for name in files)
# 占用空间转为MB
total_size_mb = round(total_size / (1024 * 1024), 2)
# 将结果加入列表
result.append({
"PlugName": plug_name,
"Directory": full_path,
"FileCount": file_count,
"TotalSizeMB": total_size_mb
})
# 关闭数据库连接
conn.close()
# 转化结果为JSON
#fc_json = result
#fc_json = json.dumps(result)
#return fc_json
return {"code": 0, "msg": "", "count": len(result), "data": result}
# 删除给定的目录列表及其所有内容
def delete_directories(directories):
""" 删除给定的目录列表及其所有内容。 """
try:
for directory in directories:
full_path = os.path.join(directory) # 组合成完整路径
shutil.rmtree(full_path) # 删除目录及其所有内容
return {"code": 0, "msg": "删除成功"}
except Exception as e:
return {"code": 500, "msg": str(e)}
"""<一般web端路由交互执行>"""
@web_blueprint.route('/', methods=['POST'])
def handle_web_Execution():
if request.is_json: # 确保请求包含 JSON 数据
data = request.get_json()
action = data.get('action')
url = data.get('url')
id = data.get('id')
print(f'当前于web路由action状态码:{action}')
print(file_center_list())
match action:
case 'fetch_myfast':# 刷新快捷方式
results = fetch_myfast()
return render_template('add_myfast.html', items=results)
case 'add_website':# 添加网址到快捷方式
response_data = add_website()
return jsonify(response_data)
case 'add_plugin':# 添加插件到快捷方式
response_data = add_plugin()
return jsonify(response_data)
case 'conn_website':# 检测网址
title, description = fetch_title_and_description(url)
return jsonify({'title': title, 'description': description})
case 'del_fast':#删除快捷方式
if del_myfast(id):
return jsonify({"message": "删除成功"})
else:
return jsonify({"error": "删除失败"}), 500
case 'utimes':#快捷方式计数
if Utimes_myfast(id):
return jsonify({'message': 'Utimes_myfast success'})
else:
return jsonify({'status': 'error', 'message': 'Missing id'}), 400
case 'file_center_list': # 临时文件中心表格显示
return file_center_list()
case 'del_upload_file': # 临时文件中心表格显示
directories = data.get('directories', [])
result = delete_directories(directories)
return jsonify(result)
case 'AIURL_list':#读取网址大全列表
unique_sheet_names = get_sheet_names()
unique_column_names = get_table_columns()
unique_all_records = get_all_records()
return render_template('AIURL_List.html',
sheet_names=unique_sheet_names,
column_names=unique_column_names,
all_records=unique_all_records)
case _:
return jsonify({"error": "Invalid action or request method"}), 400
else:
return jsonify({'error': 'Invalid Content-Type'}), 400
>>>>>>> 33969d2a895ce8a09fca410185bb3cfa811bfe73
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
Python
1
https://gitee.com/xinyizq/PlugLink.git
[email protected]:xinyizq/PlugLink.git
xinyizq
PlugLink
PlugLink_您的自动化机器人
main

搜索帮助