#!/usr/bin/env python
# -*- coding: utf-8 -*-
#
from google_images_download import google_images_download   #importing the library
from googletrans import Translator
from array import *
import numpy as np
import os
from stem.util import system
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
import socket
import stem.process
import urllib

from stem.util import term



#tor_process = subprocess.Popen(runtime_args, stdout = subprocess.PIPE, stdin = subprocess.PIPE, stderr = subprocess.PIPE)
SOCKS_PORT = 9050

# Set socks proxy and wrap the urllib module

#socks.setdefaultproxy(socks.PROXY_TYPE_SOCKS5, '127.0.0.1', SOCKS_PORT)
#socket.socket = socks.socksocket

# Perform DNS resolution through the socket

def getaddrinfo(*args):
  return [(socket.AF_INET, socket.SOCK_STREAM, 6, '', (args[0], args[1]))]

socket.getaddrinfo = getaddrinfo

def query(url):
    return urllib.request.urlopen(url).read()

def print_bootstrap_lines(line):
  if "Bootstrapped " in line:
    print(term.format(line, term.Color.BLUE))

def stop_process_on_name():
  process =  system.pid_by_name('tor')
  if  process is not None:
    os.kill(process, 2) 
# check language short name https://www.science.co.il/language/Codes.php
# check tor short name of country  https://b3rn3d.herokuapp.com/blog/2014/03/05/tor-country-codes
#
countrynamesDirectory = np.array([ "Germany", "South_Africa","Brazil","USA", "Mexico", "Hong_Kong", "Taiwan",  "Norway", "Vietnam","Thailand","Turkey", "Russia"])    
#https://sccmrookie.blogspot.com/2016/03/tor-country-codes-list.html
countries = np.array(["de","za", "br","us", "mx","hk", "tw", "no","vn","th","tr" , "ru"])
#https://ctrlq.org/code/19899-google-translate-languages
#https://www.vengaglobal.com/blog/simplified-traditional-chinese-mandarin-cantonese/
languages = np.array([ "de","af", "pt","en" ,"es",  "zh-TW", "zh-TW", "no","vi", "th","tr", "ru"])


translator = Translator()#
specific_site = ""
searchterms = np.array(["climate change", "global warming", "energy transformation", "climate change adaptation", "climate change impacts", "climate change disaster", "climate change risk", "sustainability","climate change mitigation"])
#searchterms = np.array(["climate change mitigation"])
for searchtermIndex in range(0, searchterms.shape[0]):
    path = "downloads/" + searchterms[searchtermIndex]
    
    try:  
        os.mkdir(path)
    except OSError:  
        print ("Creation of the directory %s failed" % path)
    else:  
        print ("Successfully created the directory %s " % path)




    for index in range(0, countries.shape[0]):
        country = "{"+countries[index]+"}"
        print(country)
        print(term.format("Starting Tor:\n", term.Attr.BOLD))
        tor_process = stem.process.launch_tor_with_config(
         timeout = 500, 
         config = {
#            'SocksPort': str(SOCKS_PORT),
#            'DataDirectory': '~/.tor/custom',
            'ExitNodes': country,
          },
          init_msg_handler = print_bootstrap_lines,
        )
    
        try:
            searchterm=searchterms[searchtermIndex]
            print(searchterm)
            translateSearchterm = translator.translate(searchterm, dest=languages[index]).text
            imageDirectory=searchterm+"/"+countrynamesDirectory[index]+"_"+languages[index]
            arguments = {"keywords":translateSearchterm,"limit":250,"country":countrynamesDirectory[index], "language_english": languages[index],"searchterm": searchterm,"prefix":"img_","print_urls":True,"chromedriver": "/usr/bin/chromedriver",  "extract_metadata": True, "image_directory":imageDirectory }
            if(len(specific_site)>1):
                arguments.append({"specific_site":specific_site})
            paths = response.download(arguments)   #passing the arguments to the function"type":"line-drawing"
#            print(paths) 
            
            os.rename('logs/'+translateSearchterm+'.json', 'logs/'+searchterm+"_"+countrynamesDirectory[index]+"_"+languages[index]+'.json')
            tor_process.kill() 
            
            
        except:
            print("fails")

            if tor_process:
                tor_process.kill()  # don't leave a lingering process
                tor_process.wait()

            exit -1



