import os
import wget
from zipfile import ZipFile
import shutil
import time
def UrlPathReadFunction(DownloadsFilePath,DownloadsFileName,DownloadsFileNameSet):
OpenFile = open('Urlpath.txt','r')
ReadFile = OpenFile.readlines()
for i in ReadFile:
i = i.replace('\n','')
UrlPath.append(i)
temp = i.split('/')
temp = temp[-1].replace('\n','')
FileName.append(temp)
OpenFile.close()
FileNameSet = set(FileName)
return FileNameSet,UrlPath
def CheckBetweenDownloadAndUrlPath(FileNameSet,DownloadsFileNameSet):
StopDownloadSet = FileNameSet.difference(DownloadsFileNameSet)
TempDownloadsFileName = []
for i in StopDownloadSet:
TempDownloadsFileName.append(i)
return TempDownloadsFileName
def DownloadFunction(TempFileName,UrlPath):
# 如果同名,就不下載。
for i in range(0,len(TempFileName)):
print("Download "+TempFileName[i]+". Start!")
wget.download(UrlPath[i],str(DownloadsFilePath+TempFileName[i]))
print("Download "+TempFileName[i]+". OK!")
def UnZipFunction(CheckFileName):
for i in CheckFileName:
i = CheckFilePath+i
with ZipFile(i,'r') as zip:
zip.printdir()
zip.extractall('temp')
def MoveFilesToSqlite3():
MoveFiles = []
MoveFilePath = os.getcwd()+'\\temp\\'
MoveFiles = os.listdir(MoveFilePath)
MoveDir = MoveFiles[0]
RenamedFile01 = MoveFiles[1].replace(".def","")
del MoveFiles[0]
for file in MoveFiles:
shutil.move(MoveFilePath+file,MoveFilePath+MoveDir)
os.rename(MoveFilePath+MoveDir,MoveFilePath+RenamedFile01)
# Sqlite3 下載
StopDownloadSet = set()
ReadFile = []
FileName = []
UrlPath = []
DownloadsFileName = []
DownloadsFilePath = os.getcwd()+'\\Downloads\\'
DownloadsFileName = os.listdir(DownloadsFilePath)
DownloadsFileNameSet = set(DownloadsFileName)
# 讀取UrlPath.txt檔案內容,取得下載路徑與檔名
FileNameSet,UrlPath = UrlPathReadFunction(DownloadsFilePath,DownloadsFileName,DownloadsFileNameSet)
# 檢查Downloads資料夾檔案名稱與UrlPath檔案內檔案名稱,是否有同名。如果同名,就不下載。
TempFileName = []
TempFileName = CheckBetweenDownloadAndUrlPath(FileNameSet,DownloadsFileNameSet)
DownloadFunction(TempFileName,UrlPath)
time.sleep(2)
# 檢查Downloads資料夾內的壓縮檔,並解壓縮到temp資料夾
CheckFileName = []
CheckFilePath = os.getcwd()+'\\Downloads\\'
CheckFileName = os.listdir(CheckFilePath)
UnZipFunction(CheckFileName)
# 將temp資料夾內的檔案更名並移動到temp\sqlite3
MoveFilesToSqlite3()