Skip to content

Commit

Permalink
Update error info for mysql
Browse files Browse the repository at this point in the history
  • Loading branch information
NaiboWang committed Jul 20, 2023
1 parent f16722f commit f8c575f
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 29 deletions.
14 changes: 7 additions & 7 deletions ElectronJS/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@ function travel(dir,callback){
}
function compare(p){ //这是比较函数
return function(m,n){
var a = m[p];
var b = n[p];
let a = m[p];
let b = n[p];
return b - a; //降序
}
}
Expand Down Expand Up @@ -217,9 +217,9 @@ exports.start = function(port = 8074) {
res.write(JSON.stringify(output));
res.end();
} else if (pathName == "/queryTask") {
var params = url.parse(req.url, true).query;
let params = url.parse(req.url, true).query;
try {
var tid = parseInt(params.id);
let tid = parseInt(params.id);
const data = fs.readFileSync(path.join(getDir(), `tasks/${tid}.json`), 'utf8');
// parse JSON string to JSON object
res.write(data);
Expand All @@ -229,9 +229,9 @@ exports.start = function(port = 8074) {
res.end();
}
} else if (pathName == "/queryExecutionInstance") {
var params = url.parse(req.url, true).query;
let params = url.parse(req.url, true).query;
try {
var tid = parseInt(params.id);
let tid = parseInt(params.id);
const data = fs.readFileSync(path.join(getDir(), `execution_instances/${tid}.json`), 'utf8');
// parse JSON string to JSON object
res.write(data);
Expand All @@ -244,7 +244,7 @@ exports.start = function(port = 8074) {
res.write("Hello World!", 'utf8');
res.end();
} else if(pathName == "/deleteTask"){
var params = url.parse(req.url, true).query;
let params = url.parse(req.url, true).query;
try {
let tid = parseInt(params.id);
let data = fs.readFileSync(path.join(getDir(), `tasks/${tid}.json`), 'utf8');
Expand Down
62 changes: 40 additions & 22 deletions ExecuteStage/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,15 @@
import pymysql
from lxml import etree


def is_valid_url(url):
try:
result = urlparse(url)
return all([result.scheme, result.netloc])
except ValueError:
return False


def lowercase_tags_in_xpath(xpath):
return re.sub(r"([A-Z]+)(?=[\[\]//]|$)", lambda x: x.group(0).lower(), xpath)

Expand All @@ -30,10 +32,10 @@ def on_press_creator(press_time, event):
def on_press(key):
try:
if key.char == 'p':
if press_time["is_pressed"] == False: # 没按下p键时,记录按下p键的时间
if press_time["is_pressed"] == False: # 没按下p键时,记录按下p键的时间
press_time["duration"] = time.time()
press_time["is_pressed"] = True
else: # 按下p键时,判断按下p键的时间是否超过2.5秒
else: # 按下p键时,判断按下p键的时间是否超过2.5秒
duration = time.time() - press_time["duration"]
if duration > 2:
if event._flag == False:
Expand All @@ -53,6 +55,7 @@ def on_press(key):
pass
return on_press


def on_release_creator(event, press_time):
def on_release(key):
try:
Expand Down Expand Up @@ -139,9 +142,12 @@ def get_output_code(output):
return code

# 判断字段是否为空


def isnotnull(s):
return len(s) != 0


def new_line(outputParameters, maxViewLength, record):
line = []
i = 0
Expand All @@ -153,6 +159,7 @@ def new_line(outputParameters, maxViewLength, record):
print("")
return line


def write_to_csv(file_name, data, record):
with open(file_name, 'a', encoding='utf-8-sig', newline="") as f:
f_csv = csv.writer(f)
Expand All @@ -164,6 +171,7 @@ def write_to_csv(file_name, data, record):
f_csv.writerow(to_write)
f.close()


def replace_field_values(orginal_text, outputParameters):
pattern = r'Field\["([^"]+)"\]'
try:
Expand All @@ -173,6 +181,7 @@ def replace_field_values(orginal_text, outputParameters):
replaced_text = orginal_text
return replaced_text


def write_to_excel(file_name, data, types, record):
first = False
if os.path.exists(file_name):
Expand All @@ -186,7 +195,7 @@ def write_to_excel(file_name, data, types, record):
first = True
# 追加数据到工作表
for line in data:
if not first: # 如果不是第一行,需要转换数据类型
if not first: # 如果不是第一行,需要转换数据类型
for i in range(len(line)):
if types[i] == "int" or types[i] == "bigInt":
try:
Expand All @@ -209,9 +218,6 @@ def write_to_excel(file_name, data, types, record):
wb.save(file_name)





class Time:
def __init__(self, type1=""):
self.t = int(round(time.time() * 1000))
Expand All @@ -229,7 +235,8 @@ def __init__(self, config_file="mysql_config.json"):
if sys.platform == "darwin":
if config_file.find("./") >= 0:
config_file = config_file.replace("./", "")
config_file = os.path.expanduser("~/Library/Application Support/EasySpider/" + config_file)
config_file = os.path.expanduser(
"~/Library/Application Support/EasySpider/" + config_file)
print("MySQL config file path: ", config_file)
with open(config_file, 'r') as f:
config = json.load(f)
Expand All @@ -239,18 +246,20 @@ def __init__(self, config_file="mysql_config.json"):
passwd = config["password"]
db = config["database"]
except Exception as e:
print("读取配置文件失败,请检查配置文件:"+config_file+"是否存在。")
print("Failed to read configuration file, please check if the configuration file: "+config_file+" exists.")
print("读取配置文件失败,请检查配置文件:"+config_file+"是否存在,或配置信息是否有误。")
print("Failed to read configuration file, please check if the configuration file: " +
config_file+" exists, or if the configuration information is incorrect.")
print(e)
try:
self.conn = pymysql.connect(
host=host, port=port, user=user, passwd=passwd, db=db)
host=host, port=port, user=user, passwd=passwd, db=db)
print("成功连接到数据库。")
print("Successfully connected to the database.")
except:
print("连接数据库失败,请检查配置文件是否正确。")
print("Failed to connect to the database, please check if the configuration file is correct.")

print(
"Failed to connect to the database, please check if the configuration file is correct.")

def create_table(self, table_name, parameters):
self.table_name = table_name
self.field_sql = "("
Expand All @@ -259,7 +268,8 @@ def create_table(self, table_name, parameters):
cursor.execute("SHOW TABLES LIKE '%s'" % table_name)
result = cursor.fetchone()

sql = "CREATE TABLE " + table_name + " (_id INT AUTO_INCREMENT PRIMARY KEY, "
sql = "CREATE TABLE " + table_name + \
" (_id INT AUTO_INCREMENT PRIMARY KEY, "
for item in parameters:
if item["recordASField"]:
name = item['name']
Expand Down Expand Up @@ -315,25 +325,32 @@ def write_to_mysql(self, OUTPUT, record, types):
line[i] = 0.0
elif types[i] == "datetime":
try:
line[i] = datetime.datetime.strptime(line[i], '%Y-%m-%d %H:%M:%S')
line[i] = datetime.datetime.strptime(
line[i], '%Y-%m-%d %H:%M:%S')
except:
line[i] = datetime.datetime.strptime("1970-01-01 00:00:00", '%Y-%m-%d %H:%M:%S')
line[i] = datetime.datetime.strptime(
"1970-01-01 00:00:00", '%Y-%m-%d %H:%M:%S')
elif types[i] == "date":
try:
line[i] = datetime.datetime.strptime(line[i], '%Y-%m-%d')
line[i] = datetime.datetime.strptime(
line[i], '%Y-%m-%d')
except:
line[i] = datetime.datetime.strptime("1970-01-01", '%Y-%m-%d')
line[i] = datetime.datetime.strptime(
"1970-01-01", '%Y-%m-%d')
elif types[i] == "time":
try:
line[i] = datetime.datetime.strptime(line[i], '%H:%M:%S')
line[i] = datetime.datetime.strptime(
line[i], '%H:%M:%S')
except:
line[i] = datetime.datetime.strptime("00:00:00", '%H:%M:%S')
line[i] = datetime.datetime.strptime(
"00:00:00", '%H:%M:%S')
to_write = []
for i in range(len(line)):
if record[i]:
to_write.append(line[i])
# 构造插入数据的 SQL 语句
sql = f"INSERT INTO "+ self.table_name +" "+self.field_sql+" VALUES ("
sql = f"INSERT INTO " + self.table_name + \
" "+self.field_sql+" VALUES ("
for item in to_write:
sql += "%s, "
# 移除最后的逗号并添加闭合的括号
Expand All @@ -347,14 +364,15 @@ def write_to_mysql(self, OUTPUT, record, types):
print("插入数据库错误,请查看以上的错误提示,然后检查数据的类型是否正确,是否文本过长(超过一万的文本类型要设置为大文本)。")
print("Inserting database error, please check the above error, and then check whether the data type is correct, whether the text is too long (text type over 10,000 should be set to large text).")
print("重新执行任务时,请删除数据库中的数据表" + self.table_name + ",然后再次运行程序。")
print("When re-executing the task, please delete the data table " + self.table_name + " in the database, and then run the program again.")
print("When re-executing the task, please delete the data table " +
self.table_name + " in the database, and then run the program again.")

# 提交到数据库执行
self.conn.commit()

# 关闭游标和连接
cursor.close()

def close(self):
self.conn.close()
print("成功关闭数据库。")
Expand Down

0 comments on commit f8c575f

Please sign in to comment.