def main()

in scripts/uploadImages.py [0:0]


def main():
    args = sys.argv[1:]
    print('########### Lookout For Vision - Serverless App - Image Uploader ############\n')



    try:
        DIRECTORY = args[0]
        CAMERA_ID = args[1]
        ASSEMBLY_LINE_ID = args[2]
        API_ENDPOINT = args[3]
        AUTH_TOKEN = args[4]
        TIME_BETWEEN_REQUESTS = int(args[5])
        IMAGE_EXTENSION_TYPE = '.jpeg'
        
        print('Image Source Directory: ' + DIRECTORY)
        print('Camera: ' + CAMERA_ID)
        print('AssemblyLine: ' + ASSEMBLY_LINE_ID)
        print('API Endpoint: ' + API_ENDPOINT)
        print('Time Delay: ' + args[5]  + 'seconds')
        print('\nStarting with image upload process....')
        
        file_count_success = 0
        file_count_failure = 0
        
        for file in os.scandir(DIRECTORY):
            if file.is_file():
                file_path = file.path
                if (file_path.endswith(IMAGE_EXTENSION_TYPE)):

                    print('\nUploading image file: ' + file_path)
                    IMAGE_ID = file.name
                    headers = {}
                    headers['authorizationToken'] = AUTH_TOKEN
                    params = {}
                    params['cameraid'] = CAMERA_ID
                    params['assemblylineid'] = ASSEMBLY_LINE_ID
                    params['imageid'] = IMAGE_ID
                    
                    response = requests.request('GET', API_ENDPOINT, headers=headers, params=params)
                    response_json = response.json()

                    print('Response from API Gateway when fetching signed URL: ' + str(response.status_code))

                    if response.status_code == 200:
                        print('Signed URL received - proceeding with image upload...')
                        upload_url = response_json['uploadURL']

                        with open(file_path, 'rb') as file_to_upload:
                            file_content = file_to_upload.read()
                            upload_response = requests.request('PUT', upload_url, data=file_content, headers={'Content-Type': 'image/jpeg'})

                            print('Image upload response: ' +
                                str(upload_response.status_code))
                            if upload_response.status_code == 200:
                                print('Image uploaded successfully...')
                                file_count_success+=1
                            else:
                                print('Image upload failed...')
                                file_count_failure+=1
                    else:
                        print('\nUpload failed - error encountered - ' + response.text)

                    time.sleep(TIME_BETWEEN_REQUESTS)
        print('\nUpload process completed')
        print(str(file_count_success) + ' files uploaded successfully')
        print(str(file_count_failure)  + ' files could not be uploaded')
        
    except Exception as e:
        print(e)
        sys.exit(2)