import os
#import threading
import time
import requests
from xml.dom.minidom import parse
import xml.dom.minidom
import json
from urllib3 import encode_multipart_formdata
import shutil
path = "D:\Database\homework/"
class HOMEWORK:
files = []
file = []
def __init__(self,path):
# model = models.HomeWork.objects
self.path = path
self.headers = {"User-Agent":"Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/53.0.2785.101 Safari/537.36"}
def readfile(self):
Files = os.listdir(self.path)
xmls = []
for i in Files:
f = i.split(".")[-1]
if f == "xml" and i not in HOMEWORK.files:
HOMEWORK.files.append(i)
HOMEWORK.file = i
break
else:
HOMEWORK.file = ""
print('检索到未入库xml文件:%s\n当前准备入库:%s' % (HOMEWORK.files, HOMEWORK.file))
def analysis_file(self):
try:
if HOMEWORK.file=="":
print("无有效xml可分析")
return
print("Analysing %s" % HOMEWORK.file)
DOMTree = xml.dom.minidom.parse(self.path +HOMEWORK.file)
collection = DOMTree.documentElement
#print(HOMEWORK.file,collection)
standl = collection.getAttribute("stand")
homeworks = collection.getElementsByTagName("dbo.Homework")
Hplname = []
Huser = []
Participantunitid = []
Submittime = []
Hfile = []
Hid = []
for h in homeworks:
hid = h.getElementsByTagName("HID")[0].childNodes[0].data
Hid.append(hid)
filename = h.getElementsByTagName("Hname")[0].childNodes[0].data
#print(filename,len(homeworks))
Hfile.append(filename)
plname = h.getElementsByTagName("Hplaname")[0].childNodes[0].data
Hplname.append(plname)
unit = h.getElementsByTagName("Htusername")[0].childNodes[0].data
Huser.append(unit)
role = h.getElementsByTagName("Htuserole")[0].childNodes[0].data
Participantunitid.append(role)
tim = h.getElementsByTagName("Htime")[0].childNodes[0].data
Submittime.append(tim)
shutil.move("D:\\Database\\homework\\"+HOMEWORK.file,"D:\\Database\\homeworked\\"+HOMEWORK.file)
except Exception as e:
print(e)
#print(Hplname, Huser, Participantunitid, Submittime, Hfile)
return Hid,Hplname, Huser, Participantunitid, Submittime, Hfile
def savefile(self,Hid,Hplname, Huser, Participantunitid, Submittime, Hfile):
dicts = []
files ={}
for i in range(0,len(Hplname)):
try:
dict ={}
dict['H_ID'] = Hid[i]
dict["H_User"] = Huser[i]
dict['H_File'] = Hfile[i]
dict['ParticipantUnit_ID'] = Participantunitid[i]
dict['Submit_Time'] = Submittime[i]
dict['Plan_ID'] = Hplname[i]
#dict['filecontent'] = open(self.path + Hfile[i],'rb')
#dict['filetype'] = Hfile[i].split('.')[-1]
dicts.append(dict)
time.sleep(0.5)
except Exception as e:
print(e)
try:
if len(dicts) >0:
url = "http://192.168.36.173:8000/savehomework"
for f_info in dicts:
#print("D:\\Database\\homework\\"+f_info['H_ID']+'_'+f_info['H_File'])
f_body = {
"file": open("D:\\Database\\homework\\"+f_info['H_ID']+'_'+f_info['H_File'],'rb'),
}
print("+++++++++++++++++++++++++++++++++++++++\n上传文件%s" % f_info)
r = requests.post(url,data = f_info, files=f_body)
print(r.text)
if r.text == "上传成功":
shutil.move("D:\\Database\\homework\\"+f_info['H_ID']+'_'+f_info['H_File'],"D:\\Database\\homeworked\\"+f_info['H_ID']+'_'+f_info['H_File'])
#break
except Exception as e:
print(e)
def gethomework(path):
f = HOMEWORK(path=path)
while True:
try:
f.readfile()
Hid,Hplname, Huser, Participantunitid, Submittime, Hfile = f.analysis_file()
print(Hplname)
f.savefile(Hid,Hplname, Huser, Participantunitid, Submittime, Hfile)
except:
pass
time.sleep(3)
gethomework(path)
作业导入
最新推荐文章于 2024-06-21 19:20:27 发布