Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/env python
- from ftplib import FTP
- import os.path
- humaxip = '192.168.XX.YY'
- humaxuser = 'humaxftp'
- humaxpass = '0000'
- humaxpath = '/media/drive1/Video'
- localpath = '/home/bkram/Videos'
- deleteremote = True
- listing = []
- filelist = []
- ftp = FTP(humaxip)
- def downloaddir(dir):
- downloads = []
- ftp.cwd(dir)
- ftp.retrlines('LIST', downloads.append)
- for item in downloads:
- filedetails = item.split()
- filename = " ".join(filedetails[8::])
- if item.split('.')[-1] == 'ts':
- filelist.append((humaxpath, dir, filename))
- ftp.cwd(humaxpath)
- def download(file):
- localfile = file[2]
- localdir = file[1]
- if localdir:
- print "/".join((localpath, localdir, localfile))
- dlpath = "/".join((localpath, localdir))
- if not os.path.isdir(dlpath):
- print "Creating %s" % dlpath
- os.mkdir(dlpath)
- downloadto = "/".join((localpath, localdir, localfile)).replace('//', '/')
- downloadfrom = "/".join((humaxpath, localdir,
- localfile)).replace('//', '/')
- command = "RETR " + downloadfrom
- if os.path.isfile(downloadto):
- print "Skipping %s exists locally" % (downloadfrom)
- else:
- print "Getting %s" % (downloadfrom)
- ftp.retrbinary(command, open(downloadto, 'wb').write)
- if deleteremote:
- print " Deleting remote file %s" % (downloadfrom)
- ftp.delete(downloadfrom)
- def main():
- ftp.set_pasv(False)
- ftp.login(humaxuser, humaxpass)
- ftp.cwd(humaxpath)
- ftp.retrlines('LIST', listing.append)
- for item in listing:
- filedetails = item.split()
- filename = " ".join(filedetails[8::])
- filerights = filedetails[0]
- if filerights == "drwxr-xr-x":
- if not filename == '.' and not filename == '..':
- downloaddir(filename)
- else:
- if item.split('.')[-1] == 'ts':
- filelist.append((humaxpath, '', filename))
- for filedl in filelist:
- download(filedl)
- ftp.quit()
- if __name__ == "__main__":
- main()
Add Comment
Please, Sign In to add comment