import os, time, argparse
from tqdm import tqdm
from multiprocessing import Process

from google_screenshot import GoogleCrawler
from google_screenshot import GoogleScreenCrawler
from yahoo_screenshot import YahooCrawler
from bing_screenshot import BingCrawler
from instagram_screenshot import InstagramCrawler
# from mtcnn_face_cropper import mtcnnCropper
'''
## USAGE
python3 face_photo_crawler.py -o <path-to folders of ids>
## EXAMPLE
python3 face_photo_crawler.py -o '/mnt/e/RKB-Dataset/Filipina2'
'''

ap = argparse.ArgumentParser()
ap.add_argument("-o", "--output", required=True,
	help="path to output directory of downloads")
ap.add_argument("--google",
    help="use if you want to google crawl", action='store_true')
ap.add_argument("--bing",
    help="use if you want to bing crawl", action='store_true')
args = vars(ap.parse_args())

out_path = args['output']
# out_path = '/media/sf_E/RKB-Dataset/Filipina2/'

daftar_nama = [
    "burqa",
    "niqab",
    "cadar",
    "surgical mask"
]

def files(path):
    for file in os.listdir(path):
        if os.path.isfile(os.path.join(path, file)):
            yield file

def cropper():
	print("cropping for", nama)
	nama_path = os.path.join(out_path,nama)
	# filenames = files(nama_path)
	filenames_jpg = glob.glob(nama_path + '/**/*.jpg', recursive=True)
	filenames = glob.glob(nama_path + '/**/*.png', recursive=True)
	filenames.extend(filenames_jpg)
	for filename in filenames:
		mtcnnCropper(nama_path, filename)

for index, nama in enumerate(daftar_nama):
    
    # GoogleCrawling = Process(target=GoogleCrawler, args=(nama, out_path))
    # GoogleCrawling.start()
    # BingCrawling = Process(target=BingCrawler, args=(nama, out_path))
    # BingCrawling.start()
    if args['google'] == True:
        GoogleCrawler(nama, out_path)
    if args['bing'] == True:
        BingCrawler(nama, out_path)

    # GoogleCrawler(nama, out_path)
    # GoogleScreenCrawler(nama, out_path)
    # YahooCrawler(nama, out_path)
    # BingCrawler(nama, out_path)
    # InstagramCrawler(nama, out_path)

    # GoogleCrawling.join()
    # BingCrawling.join()

    try:    
        # cropper()

        if index < (len(daftar_nama) - 1):
            for i in tqdm(range(50)):
                time.sleep(1)
    except:
        print("failed or incomplete operation on", nama)