Advertisement
asteroidsteam

SSTV Auto Cam

Apr 14th, 2022
911
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.20 KB | None | 0 0
  1. # pip3 install requests boto3 watchdog
  2.  
  3. import time
  4. import logging
  5. import mimetypes
  6. from watchdog.observers import Observer
  7. from watchdog.events import LoggingEventHandler, FileCreatedEvent
  8. import boto3
  9. import os
  10. from botocore.exceptions import NoCredentialsError
  11.  
  12. AWS_ACCESS_KEY_ID = ''
  13. AWS_SECRET_ACCESS_KEY = ''
  14. S3_BUCKET_NAME = 'sstv'
  15. SSTV_FOLDER = '/mmsstv/images/'
  16.  
  17.  
  18. def upload_to_aws(local_file, bucket, s3_file):
  19.     s3 = boto3.client('s3', aws_access_key_id=AWS_ACCESS_KEY_ID,
  20.                       aws_secret_access_key=AWS_SECRET_ACCESS_KEY)
  21.  
  22.     try:
  23.         mime = mimetypes.guess_type(local_file)[0] or 'binary/octet-stream'
  24.         s3.upload_file(local_file, bucket, s3_file, ExtraArgs={
  25.             "ContentType": mime
  26.         })
  27.         logging.info(f"File Upload Successful ({s3_file})")
  28.         return True
  29.     except FileNotFoundError:
  30.         logging.warning("File Upload Failed: File not found")
  31.         return False
  32.     except NoCredentialsError:
  33.         logging.warning("File Upload Failed: No credentials")
  34.         return False
  35.  
  36.  
  37. def upload_to_website(awsname):
  38.     # Handle uploading file to your website
  39.     pass
  40.  
  41.  
  42. class SSTVEvent(LoggingEventHandler):
  43.     def dispatch(self, event):
  44.         if isinstance(event, FileCreatedEvent):
  45.             if not event.is_directory:
  46.                 logging.info(f"New SSTV image detected: {event.src_path}")
  47.                 awsname = f"sstv/{time.time()}.jpg"
  48.                 if upload_to_aws(event.src_path, S3_BUCKET_NAME, awsname):
  49.                     if upload_to_website(awsname):
  50.                         logging.info("Removing SSTV file from local disk")
  51.                         os.remove(event.src_path)
  52.                         logging.info("Done!")
  53.  
  54.  
  55. if __name__ == "__main__":
  56.     logging.basicConfig(level=logging.INFO,
  57.                         format='%(asctime)s - %(message)s',
  58.                         datefmt='%Y-%m-%d %H:%M:%S')
  59.     event_handler = SSTVEvent()
  60.     observer = Observer()
  61.     observer.schedule(event_handler, SSTV_FOLDER, recursive=False)
  62.     observer.start()
  63.     try:
  64.         while True:
  65.             time.sleep(1)
  66.     except KeyboardInterrupt:
  67.         observer.stop()
  68.     observer.join()
  69.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement