作业导入

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)
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值