SHARE
TWEET

Untitled

a guest Mar 19th, 2019 105 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import paramiko as pmko
  2. import os
  3. from PIL import Image
  4. import re
  5.  
  6.  
  7. def get_date_taken(path):
  8.     """
  9.     This will be implemented later and is currently
  10.     just to test how to get EXIF data from photos.
  11.     """
  12.     return Image.open(path)._getexif()[36867]
  13.  
  14.  
  15. def list_files(directory, filetype, ssh):
  16.     """
  17.     This will scan a directory for the filetype,
  18.     which is passed as `.jpg`, or `.mp3`, etc. and return
  19.     a list of those files.
  20.     """
  21.     print("Collecting filenames of all photos in", directory)
  22.     distantFiles = list()
  23.     filePath = '/storage/emulated/0/' + directory
  24.     filePattern = '"*' + filetype + '"'
  25.     rawcommand = 'find {path} -name {pattern}'
  26.     command = rawcommand.format(path=filePath, pattern=filePattern)
  27.     stdin, stdout, stderr = ssh.exec_command(command)
  28.     filelist = stdout.read().splitlines()
  29.     for afile in filelist:
  30.         (head, filename) = os.path.split(afile)
  31.         distantFiles.append(filename)
  32.     return distantFiles
  33.  
  34.  
  35. def connect_to_ssh_server(host_ip, port, username, password):
  36.     """
  37.     This will connect to an SSH Server and return the sftp and
  38.     ssh objects
  39.     """
  40.     print("Starting connection to", host_ip)
  41.     ssh = pmko.SSHClient()
  42.     ssh.set_missing_host_key_policy(pmko.AutoAddPolicy())
  43.     try:
  44.         ssh.connect(host_ip, port=port, username=username,
  45.                     password=password)
  46.         sftp = ssh.open_sftp()
  47.         print("Connected!")
  48.         return sftp, ssh
  49.     except pmko.ssh_exception.NoValidConnectionsError:
  50.         print("No valid connections for", host_ip)
  51.         ssh.close()
  52.         quit()
  53.     except TimeoutError:
  54.         print("Connection attempt timed out when trying to connect to",
  55.               host_ip)
  56.         ssh.close()
  57.         quit()
  58.  
  59.  
  60. def fix_filenames(files, directory, rgx, replacement, sftp):
  61.     for file in files:
  62.         if type(file) != str:
  63.             file = file.decode('utf-8')
  64.         if file.endswith(".jpg_c"):
  65.             print("Fixing", file)
  66.             new_end = re.sub(rgx, replacement, file)
  67.             print(directory + file, " will be in ", directory + new_end)
  68.             sftp.rename(directory + file, directory + new_end)
  69.  
  70.  
  71. def download_file(sftp, filename, origin, destination):
  72.     sftp.get(origin + filename, destination + filename, callback=None)
  73.  
  74.  
  75. def rename_file(sftp, filename, directory, suffix):
  76.     """
  77.     This will rename a file in a directory with the passed in suffix.
  78.     """
  79.     extention = filename.rsplit(".")[1]
  80.     new_name = re.sub(r"..*", suffix + extention, filename)
  81.     print(filename, "will become", new_name)
  82.     sftp.rename(directory + filename, os.path.join(directory, new_name))
  83.  
  84.  
  85. def copy_all_files(lst, origin_directory, destination, ssh, sftp):
  86.     """
  87.     This copies files from a list, from an origin directory
  88.     to the destination.
  89.     """
  90.     for _file in lst:
  91.         if type(_file) != str:
  92.             _file = _file.decode('utf-8')
  93.         if not bool(re.search(r'_c.', _file)):
  94.             try:
  95.                 download_file(sftp, _file, origin_directory, destination)
  96.                 rename_file(sftp, _file, origin_directory, "_c.")
  97.             except FileNotFoundError:
  98.                 print("Could not find", origin_directory + _file)
  99.                 continue
  100.             except OSError:
  101.                 print("OSError on", str(os.path.join(
  102.                     origin_directory, _file)), "--------------<<")
  103.                 continue
  104.         else:
  105.             print(_file, "already copied")
  106.  
  107.  
  108. def main():
  109.     sftp, ssh = connect_to_ssh_server(
  110.         "192.168.0.100", 2222, "testUsr", "supersecretpassword")
  111.     android_path = "DCIM/camera/"
  112.     # use `.*` to copy all filetypes
  113.     files = list_files(android_path, ".*", ssh)
  114.     origin_directory = '/storage/emulated/0/' + android_path
  115.     copy_all_files(files, origin_directory, '/media/pi/My Passport/Galaxy S8/',
  116.                    ssh, sftp)
  117.     ssh.close()
  118.  
  119.  
  120. if __name__ == "__main__":
  121.     main()
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top