Advertisement
Guest User

Untitled

a guest
Aug 11th, 2018
107
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.94 KB | None | 0 0
  1. # coding: utf-8
  2. """
  3. """
  4. import logging
  5. import argparse
  6. import time
  7. from ftplib import FTP
  8.  
  9. _logger = logging.getLogger(__name__)
  10.  
  11.  
  12. def snap_to_ftp(ftp_client, duration, interval, image_format):
  13.     """Script to save a screenshot on a ftp server"""
  14.     if not ftp_client:  # todo: Improve the check?
  15.         raise ValueError('A valid ftp client must be passed.')
  16.    
  17.     print('Run the script for %4ds seconds with interval %3ds' % (duration, interval))
  18.  
  19.     passed_time = 0
  20.     snapshots = []
  21.  
  22.     while passed_time < duration:
  23.         snapshot = get_snapshot(image_format)
  24.         _logger.debug('Generated the snapshot: %r', snapshot)
  25.         snapshots.append(snapshot)
  26.         try:
  27.             upload_file(ftp_client, snapshot)
  28.         except Exception as e:
  29.             _logger.exception('Failed to upload snapshot:%s', snapshot)
  30.  
  31.         time.sleep(interval)
  32.         passed_time += interval
  33.  
  34. def get_snapshot(image_format):
  35.     """Returns the file object format"""
  36.     # im.show()
  37.     # ImageGrab.grab_to_file(str(datetime.datetime.now()).split('.')[0].replace(' ', '') + '.png')
  38.     file = None
  39.     return file
  40.  
  41. def get_ftp_client(user, password, host):
  42.     """Create, set and return the ftp client"""
  43.     ftp = FTP(host=host, user=user, passwd=password)
  44.     return ftp
  45.  
  46. def upload_file(ftp_client, file_to_upload, prefix='STOR', set_pasv=True):
  47.     """Upload the file using the given ftp_client"""
  48.     ftp_client.set_pasv(set_pasv)
  49.     ftp_file = open(file_to_upload, 'rb')
  50.     ftp_client.storbinary(prefix + file_to_upload.name, ftp_file)
  51.     ftp_file.close()
  52.  
  53.  
  54. if __name__ == '__main__':
  55.     # setup the parser for the arguments of the script
  56.     parser = argparse.ArgumentParser(description='Process the script arguments')
  57.  
  58.     parser.add_argument('--duration', dest='duration', type=int, default=300,
  59.                         help='Duration of the script in seconds. (default=300)')
  60.     parser.add_argument('--interval', dest='interval', type=int, default=60,
  61.                         help='Interval in seconds between every screenshot (default=60)')
  62.     parser.add_argument('--image-format', dest='image_format', type=str, default='png',
  63.                         help='Image format (default:png)')
  64.  
  65.     parser.add_argument('--password', dest='password', type=str, help='FTP password', required=True)
  66.     parser.add_argument('--user', dest='ftp_user', type=str, help='FTP user', required=True)
  67.     parser.add_argument('--host', dest='ftp_host', type=str, help='FTP server host', required=True)
  68.  
  69.     args = parser.parse_args()
  70.  
  71.     duration = args['duration']
  72.     interval = args['interval']
  73.     image_format = args['image_format']
  74.  
  75.     ftp_user = args['ftp_user']
  76.     ftp_password = args['ftp_password']
  77.     ftp_host = args['ftp_host']
  78.  
  79.     ftp_client = get_ftp_client(user=ftp_user, password=ftp_password, host=ftp_host)
  80.  
  81.     snap_to_ftp(ftp_client, duration=duration, interval=interval, image_format=image_format)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement