#!/usr/bin/env python
# -*- coding: utf-8 -*-
#
from google_images_download import google_images_download   #importing the library
from array import *
import numpy as np
import os

response = google_images_download.googleimagesdownload()   #class instantiation

# find possible exit nodes:
#https://metrics.torproject.org/rs.html#search/country:uk%20flag:exit
#country:uk flag:exit

#systemctl stop tor@default.service
#systemctl stop tor.service

#USAGE: ./scraper.py --cromedriver '/usr/bin/'


#import socks


#searchterms = np.array([])

searchterms = np.array(["temperature graph", "temperature curve", "climate change graph", "global warming graph", "hockey stick" ])
websites = np.array(["ipcc.ch"
                     ])

dirs = np.array(["ipcc" ])






#searchterms = np.array(["climate change mitigation"])
for searchtermIndex in range(0, searchterms.shape[0]):
    for websiteIndex in range(0, dirs.shape[0]):
        searchterm=searchterms[searchtermIndex]
        path = "downloads/" + searchterm+"/"
        pathsubfolder = "downloads/" + searchterm+"/"+dirs[websiteIndex] +"/"
        
        try:  
            os.mkdir(pathsubfolder)
        except OSError:  
            print ("Creation of the directory %s failed" % pathsubfolder)
        else:  
            print ("Successfully created the directory %s " % pathsubfolder)

        
        try:

            print(searchterm)
            imageDirectory=searchterm+"/"+dirs[websiteIndex] +"/"
            arguments = {"keywords":searchterm,"limit":350,"specific_site":websites[websiteIndex],"language_english": "de","country":dirs[websiteIndex], "searchterm": searchterm,"prefix":"img_","print_urls":True,"chromedriver": "/usr/bin/chromedriver",  "extract_metadata": True, "image_directory":imageDirectory } 
            paths = response.download(arguments)   #passing the arguments to the function"type":"line-drawing"
            os.rename('logs/'+searchterm+'.json', 'logs/'+searchterm+"_"+dirs[websiteIndex]+'.json')

                
        except:
            print("fails 2")


