Guest User

Untitled

a guest
Oct 31st, 2018
121
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.06 KB | None | 0 0
  1. #!/usr/bin/env python
  2. from ftplib import FTP
  3. import os.path
  4.  
  5. humaxip = '192.168.XX.YY'
  6. humaxuser = 'humaxftp'
  7. humaxpass = '0000'
  8. humaxpath = '/media/drive1/Video'
  9. localpath = '/home/bkram/Videos'
  10.  
  11. deleteremote = True
  12.  
  13. listing = []
  14. filelist = []
  15.  
  16. ftp = FTP(humaxip)
  17.  
  18.  
  19. def downloaddir(dir):
  20. downloads = []
  21. ftp.cwd(dir)
  22. ftp.retrlines('LIST', downloads.append)
  23. for item in downloads:
  24. filedetails = item.split()
  25. filename = " ".join(filedetails[8::])
  26. if item.split('.')[-1] == 'ts':
  27. filelist.append((humaxpath, dir, filename))
  28. ftp.cwd(humaxpath)
  29.  
  30.  
  31. def download(file):
  32. localfile = file[2]
  33. localdir = file[1]
  34. if localdir:
  35. print "/".join((localpath, localdir, localfile))
  36. dlpath = "/".join((localpath, localdir))
  37. if not os.path.isdir(dlpath):
  38. print "Creating %s" % dlpath
  39. os.mkdir(dlpath)
  40.  
  41. downloadto = "/".join((localpath, localdir, localfile)).replace('//', '/')
  42. downloadfrom = "/".join((humaxpath, localdir,
  43. localfile)).replace('//', '/')
  44. command = "RETR " + downloadfrom
  45.  
  46. if os.path.isfile(downloadto):
  47. print "Skipping %s exists locally" % (downloadfrom)
  48. else:
  49. print "Getting %s" % (downloadfrom)
  50. ftp.retrbinary(command, open(downloadto, 'wb').write)
  51. if deleteremote:
  52. print " Deleting remote file %s" % (downloadfrom)
  53. ftp.delete(downloadfrom)
  54.  
  55.  
  56. def main():
  57. ftp.set_pasv(False)
  58. ftp.login(humaxuser, humaxpass)
  59. ftp.cwd(humaxpath)
  60.  
  61. ftp.retrlines('LIST', listing.append)
  62.  
  63. for item in listing:
  64. filedetails = item.split()
  65. filename = " ".join(filedetails[8::])
  66. filerights = filedetails[0]
  67. if filerights == "drwxr-xr-x":
  68. if not filename == '.' and not filename == '..':
  69. downloaddir(filename)
  70.  
  71. else:
  72.  
  73. if item.split('.')[-1] == 'ts':
  74. filelist.append((humaxpath, '', filename))
  75.  
  76. for filedl in filelist:
  77. download(filedl)
  78.  
  79. ftp.quit()
  80.  
  81. if __name__ == "__main__":
  82. main()
Add Comment
Please, Sign In to add comment