Update sync.py
This commit is contained in:
parent
efcb99b310
commit
b88d2ea7bf
75
sync.py
75
sync.py
|
@ -31,67 +31,75 @@ def CheckPDF(lst):
|
||||||
for pr in lst:
|
for pr in lst:
|
||||||
m=GetMD5(pr[1])
|
m=GetMD5(pr[1])
|
||||||
if(m in clst):
|
if(m in clst):
|
||||||
print('md5 same: ' + pr[0])
|
print('md5 same: ' + pr[0] + '. Skipped.')
|
||||||
else:
|
else:
|
||||||
clst.append(m)
|
clst.append(m)
|
||||||
flst.append([pr[0],pr[1],m])
|
flst.append([pr[0],pr[1],m])
|
||||||
return flst
|
return flst
|
||||||
|
|
||||||
def SyncPDF(uinfo,lst):
|
def SyncPDF(uinfo,lst):
|
||||||
|
mbcalc=lambda x:round(x/1024/1024,2)
|
||||||
|
|
||||||
ftp=FTP(uinfo[0])
|
ftp=FTP(uinfo[0])
|
||||||
ftp.encoding='UTF-8'
|
ftp.encoding='UTF-8'
|
||||||
ftp.login(uinfo[1],uinfo[2])
|
ftp.login(uinfo[1],uinfo[2])
|
||||||
|
|
||||||
ftp.cwd('/md5')
|
ftp.cwd('/md5')
|
||||||
mlst=ftp.nlst()
|
remote_track_list=ftp.nlst()
|
||||||
tlst=[]
|
|
||||||
upsz=0
|
to_upload_list=[]
|
||||||
|
to_upload_byte=0
|
||||||
tmpid=0
|
tmpid=0
|
||||||
for name,addr,check in lst:
|
for name,addr,check in lst:
|
||||||
tmpid=tmpid+1
|
tmpid=tmpid+1
|
||||||
if(check not in mlst):
|
if(check not in remote_track_list):
|
||||||
print('[' + str(tmpid) + '][Untracked] ' + name)
|
print('[' + str(tmpid) + '][Untracked] ' + name)
|
||||||
tlst.append([name,addr,check])
|
to_upload_list.append([name,addr,check])
|
||||||
upsz+=os.path.getsize(addr)
|
to_upload_byte+=os.path.getsize(addr)
|
||||||
else:
|
else:
|
||||||
print('[' + str(tmpid) + '][Synced] ' + name)
|
print('[' + str(tmpid) + '][Synced] ' + name)
|
||||||
|
|
||||||
lsz=len(tlst)
|
to_upload_cnt=len(to_upload_list)
|
||||||
if(lsz<1):
|
if(to_upload_cnt<1):
|
||||||
print('Nothing to upload.')
|
print('Nothing to upload.')
|
||||||
return
|
return
|
||||||
|
|
||||||
print("Totoally " + str(lsz) + " files need to upload. Need to upload: " + str(round(upsz/1024/1024,2)) + 'MB')
|
print('Totoally ' + str(to_upload_cnt) + ' files need to upload. '
|
||||||
|
+ 'Need to upload: ' + str(mbcalc(to_upload_byte)) + 'MB')
|
||||||
choice=input('Are you sure to upload? (Y/N): ')
|
choice=input('Are you sure to upload? (Y/N): ')
|
||||||
if(choice!='Y'):
|
if(choice!='Y'):
|
||||||
print('Aborted.')
|
print('Aborted.')
|
||||||
return
|
return
|
||||||
|
|
||||||
ftp.cwd('/')
|
ftp.cwd('/')
|
||||||
fnlst=ftp.nlst()
|
remote_filename_list=ftp.nlst()
|
||||||
|
|
||||||
donesz=0
|
uploaded_byte=0
|
||||||
for i in range(lsz):
|
for i in range(to_upload_cnt):
|
||||||
print('[' + str(i+1) + '/' + str(lsz) + '][Uploading] ' + tlst[i][0])
|
print('[' + str(i+1) + '/' + str(to_upload_cnt) + '][Uploading] '
|
||||||
remote_filename=tlst[i][0]
|
+ to_upload_list[i][0] + '...')
|
||||||
while(remote_filename in fnlst):
|
|
||||||
|
# Adjust filename
|
||||||
|
remote_filename=to_upload_list[i][0]
|
||||||
|
while(remote_filename in remote_filename_list):
|
||||||
remote_filename=remote_filename.replace(".pdf","_.pdf",1)
|
remote_filename=remote_filename.replace(".pdf","_.pdf",1)
|
||||||
with open(tlst[i][1],'rb') as fp:
|
|
||||||
file_size=round(os.path.getsize(tlst[i][1])/1024/1024,2)
|
# Upload
|
||||||
donesz+=os.path.getsize(tlst[i][1])
|
with open(to_upload_list[i][1],'rb') as fp:
|
||||||
print('file size: ' + str(file_size) + 'MB')
|
file_byte=os.path.getsize(to_upload_list[i][1])
|
||||||
|
print('file size: ' + str(mbcalc(file_byte)) + 'MB')
|
||||||
ftp.storbinary('STOR '+remote_filename,fp)
|
ftp.storbinary('STOR '+remote_filename,fp)
|
||||||
print(str(round(donesz/1024/1024,2))
|
uploaded_byte+=file_byte
|
||||||
+ "MB uploaded. ("
|
print(str(mbcalc(uploaded_byte)) + 'MB uploaded. ('
|
||||||
+ str(round(donesz/upsz*100,2))
|
+ str(round(uploaded_byte/to_upload_byte*100,2)) + '%)')
|
||||||
+ '%)' )
|
|
||||||
|
|
||||||
# MD5 file content must be utf-8
|
# MD5 file content must be utf-8
|
||||||
with open(tlst[i][2],'w',encoding='utf-8') as cf:
|
with open(to_upload_list[i][2],'w',encoding='utf-8') as cf:
|
||||||
cf.write(remote_filename)
|
cf.write(remote_filename)
|
||||||
with open(tlst[i][2],'rb') as cf:
|
with open(to_upload_list[i][2],'rb') as cf:
|
||||||
ftp.storbinary('STOR /md5/'+tlst[i][2],cf)
|
ftp.storbinary('STOR /md5/'+to_upload_list[i][2],cf)
|
||||||
print('Check file ' + tlst[i][2] + ' updated.')
|
print('Check file ' + to_upload_list[i][2] + ' updated.')
|
||||||
os.remove(tlst[i][2])
|
os.remove(to_upload_list[i][2])
|
||||||
|
|
||||||
def FetchInfo():
|
def FetchInfo():
|
||||||
svaddr=input('Server Addr:')
|
svaddr=input('Server Addr:')
|
||||||
|
@ -102,10 +110,11 @@ def FetchInfo():
|
||||||
# Main Program
|
# Main Program
|
||||||
print("""PDF Sync Manager
|
print("""PDF Sync Manager
|
||||||
Author: Kiritow""")
|
Author: Kiritow""")
|
||||||
search_dir=input('Where should we start? :')
|
search_dir=input('Enter root directory to search :')
|
||||||
print ('Scanning...')
|
print ('Scanning...')
|
||||||
cl=CheckPDF(ScanPDF(search_dir))
|
tlst=ScanPDF(search_dir)
|
||||||
|
clst=CheckPDF(tlst)
|
||||||
|
print ('[Scan Result] ' + str(len(tlst)) + ' PDF found. ' + str(len(clst)) + ' unique PDF.')
|
||||||
print ('Syncing...')
|
print ('Syncing...')
|
||||||
uinfo=FetchInfo()
|
uinfo=FetchInfo()
|
||||||
SyncPDF(uinfo,cl)
|
SyncPDF(uinfo,clst)
|
||||||
|
|
||||||
|
|
Reference in New Issue
Block a user