import scrapy
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from pathlib import Path
import os, time, selenium, json,  MySQLdb
from scrapy.loader import ItemLoader
from webdriver_manager.chrome import ChromeDriverManager
from drspyscraper.items import DrspyscraperItem
from scrapy.linkextractors import LinkExtractor
from drspyscraper.pipelines import DatabasescraperPipeline

class drSpider(scrapy.Spider):
    """docstring for spider."""
    name = "drSpider"
    allowed_domains = ['google.com']
    start_urls = ['https://www.google.com']
    lookup_until_date = None
    __allowed = ("lookup_until_date")
    host = 'localhost'
    user = 'phpmyadmin'
    password = '123456'
    db = 'drspy'

    def __init__(self, *args, **kwargs):
        Base_path = os.getcwd()
        self.driver = webdriver.Chrome(ChromeDriverManager().install())
        self.connection = MySQLdb.connect(self.host, self.user, self.password, self.db,use_unicode=True, charset="utf8")
        self.cursor = self.connection.cursor()
        super(drSpider, self).__init__(*args, **kwargs)
        self.myargument = kwargs.get('myargument', '')

    def parse(self, response):
        search_value = self.myargument
        self.driver.get(response.url)
        """Search for all key strings"""
        keys = ['amazon', 'alibaba', 'ebay', 'shopify', 'aliexpress']
        elm = []
        try:
            if search_value is not None:
                l = ItemLoader(item=DrspyscraperItem(), response=response)
                for k in keys:
                    next = self.driver.find_element_by_name("q")
                    next.send_keys(k + '"title="'+search_value+'')
                    next.submit()
                    print('Page loading time')
                    time.sleep(2.5)
                    print('Page loading complete')
                    """Item loader"""
                    divs = self.driver.find_elements_by_class_name('srg')
                    div = self.driver.find_elements_by_class_name('g')
                    i = 0
                    while i < len(div):
                        tag_name = div[i].find_element_by_tag_name('a')
                        res = tag_name.get_attribute('href')
                        print('INNER RESULT', res)
                        l.add_value('name', res)
                        elm.append(res)
                        i += 1
                    self.driver.back()
                self.driver.close()
                return l.load_item()
            else:
                self.driver.close()
                return False
        except Exception as e:
            self.driver.close()
            return False

        def insert_data(self, data):
            try:
                self.cursor.execute(query,params)
                self.connection.commit()
            except Exception as ex:
                self.connection.rollback()


        def __del__(self):
            self.connection.close()
