#!/usr/bin/env python
# coding: utf-8

# In[6]:


from selenium import webdriver
from selenium.webdriver.common.keys import Keys
import time
import urllib.request
import os
import pandas as pd
from urllib.parse import quote_plus          
from bs4 import BeautifulSoup as bs 
from selenium import webdriver
import time
from urllib.request import (urlopen, urlparse, urlunparse, urlretrieve)
from selenium.common.exceptions import NoSuchElementException
from selenium.webdriver import ActionChains
import re
import os 


# In[7]:


def url_filter(index, url,conditons, ID_list):
    if(index ==0):
        if(conditons[0] in url):  #상품 이미지만 추출
            if('preview' in url) :  #프리뷰 있으면 사이즈 키워서 리턴
                url = re.sub('100/output=preview','',url) + '600'
                return url
            else: return url #없으면 그냥 리턴
        else: return None  #유효하지 않은 링크
    else:
        if(ID_list[index-1] in url): return None  #이미 중복된 상품 이미지
        else:
            if('preview' in url) :                         #URL 필터 후 추출
                url = re.sub('100/output=preview','',url) + '600' 
                return url
            else: return url
            
    


# In[8]:


def selenium_scroll_option():
    SCROLL_PAUSE_SEC = 2
    last_height = driver.execute_script("return document.body.scrollHeight")
    while True:
        driver.execute_script("window.scrollTo(0, document.body.scrollHeight);")
        time.sleep(SCROLL_PAUSE_SEC)
        new_height = driver.execute_script("return document.body.scrollHeight")
  
        if new_height == last_height:
            break
        last_height = new_height


# In[13]:


def Saint_Laurent(category, style, driver, chrome_options):
    
    base_url = 'https://www.ysl.com/en-kr/shop-men/ready-to-wear/by-category'


    driver.set_window_size(1000, 1200)
    driver.get(base_url+'/'+ category)
    action = ActionChains(driver)
    button = driver.find_element_by_xpath("//*[@id=\"product-search-results\"]/div/div/button")

    last_height = driver.execute_script("return document.body.scrollHeight")
    new_height = 0


    while (last_height != new_height):
        try:
            last_height = driver.execute_script("return document.body.scrollHeight")
            button = driver.find_element_by_xpath("//*[@id=\"product-search-results\"]/div/div/button")
            selenium_scroll_option()
            button.click()
            selenium_scroll_option()
            new_height = driver.execute_script("return document.body.scrollHeight")
        except: NoSuchElementException

    products = driver.find_elements_by_tag_name('img') # 이미지 더보기 클릭
    item_name = []
    ID_list = []
    img_url =[]
    for i,product in enumerate(products): 
        if(product.get_attribute("class") == 'c-product__image lazyloaded'):
            url= product.get_attribute('src')
            img_url.append(url)
            ID = re.match(r"^.*\-(.*)\_.*$",url).group(1)
            ID_list.append(ID)
            item = driver.find_element_by_xpath("//*[@id="+"\""+str(ID)+"\""+"]/div[2]/div[1]/h2").text
            item_name.append(item)
        

    style = 'RockChic'
    img_folder = './img'

    data_df = pd.DataFrame({'Item': item_name,
                       'url': img_url,
                        'Category':category,
                       'Style':'Rockchic'}, index = ID_list)
    return data_df


# In[95]:


def Amiri(category, style, driver, chrome_options):
    
    base_url = 'https://amiri.com/collections/mens'

    driver.set_window_size(1000, 1200)
    driver.get(base_url+'-'+ category+'-all')
    action = ActionChains(driver)

    selenium_scroll_option()
    item_name = []
    ID_list = []
    img_url =[]
    product_class_name = 'pgi'
    products = driver.find_elements_by_class_name(product_class_name)
    products_num = len(products)
    
    img_xpath_f = '//*[@id="PageContainer"]/main/div/div/div[2]/div['
    img_xpath_b = ']/a/div[1]/img'
    
    name_xpath_f = '//*[@id="PageContainer"]/main/div/div/div[2]/div['
    name_xpath_b = ']/p/a'
    action = ActionChains(driver)
    
    for i in range(1,products_num+1): 
        product = driver.find_element_by_xpath(img_xpath_f+str(i)+img_xpath_b)
        action.move_to_element(product).perform()
        url = product.get_attribute('src')
        item = driver.find_element_by_xpath(name_xpath_f+str(i)+name_xpath_b).text
        ID = re.findall('v=(.*\d)',url)[0]
        
        img_url.append(url)
        item_name.append(item)
        ID_list.append(ID)
                
    img_folder = './img'
    data_df = pd.DataFrame({'Item': item_name,
                       'url': img_url,
                        'Category':category,
                       'Style':style}, index = ID_list)
    return data_df


# In[46]:


def Farfetch_crawler(Brand,category, style, driver, chrome_options):
    
    base_url = 'https://www.farfetch.com/uk/shopping/men/'+brand+'/items.aspx?view=90&q=lemaire&category='
    category_dic = {'knitwear' : '136396|136399',
                  'sweatshirts-and-t-shirts':'136398|136397|136441|136333|136332',
                   'coats':'136336',
                   'denim':'136337',
                   'jackets-and-trousers':'136439|136402|136440|136442|136437|136436',
                   'outerwear':'136468|136407|136404|136405|136401|136408|136411|136409',
                   }
    category_url = category_dic.get(category)
    
    driver.set_window_size(1000, 1200)
    driver.get(base_url+'-'+ category_url)
    
    action = ActionChains(driver)
    selenium_scroll_option()
    item_name = []
    ID_list = []
    img_url =[]

    products = driver.find_elements_by_tag_name('img')
    products_num = 0
    for product in products:
        if(product.get_attribute('data-component')=='ProductCardImagePrimary'): products_num+=1
    
    
    img_xpath_f = '//*[@id="slice-container"]/div[3]/div/div[2]/div/div[1]/ul/li['
    img_xpath_b = ']/a/div[1]/div/img'
    
    name_xpath_f = '//*[@id="slice-container"]/div[3]/div/div[2]/div/div[1]/ul/li['
    name_xpath_b = ']/a/div[2]/p[2]'
    
    action = ActionChains(driver)
    
    for i in range(1,products_num+1): 
        product = driver.find_element_by_xpath(img_xpath_f+str(i)+img_xpath_b)
       
        url = product.get_attribute('src')
        item = driver.find_element_by_xpath(name_xpath_f+str(i)+name_xpath_b).text
        ID = re.findall('_(\d.*?)_',url)[0]
        ID_list.append(ID)
        img_url.append(url)
        item_name.append(item)
                  
    img_folder = './img'
    data_df = pd.DataFrame({'Item': item_name,
                       'url': img_url,
                        'Category':category,
                       'Style':style}, index = ID_list)
    return data_df


# In[45]:


chrome_options = webdriver.ChromeOptions()
chrome_path = 'C:\\Users\\dhkim\\IMG_Classifier\\chromedriver.exe'
driver = webdriver.Chrome(chrome_path,chrome_options=chrome_options)

Lemaire('sweatshirts-and-t-shirts','Minimal',driver,chrome_options)


# In[ ]:


def Celine(category, style, driver, chrome_options):
    
    base_url = 'https://www.celine.com/en-int/celine-men/ready-to-wear'

    driver.set_window_size(1000, 1200)
    driver.get(base_url+'/'+ category)
    action = ActionChains(driver)

    selenium_scroll_option()
    item_name = []
    ID_list = []
    img_url =[]

    path_f = '//*[@id=\"'
    path_b = '\"]/a/div/div[2]/div'

    url_conditions = ['product-prd']
    id = driver.find_elements_by_class_name('m-product-listing')
    products = driver.find_elements_by_tag_name('img')
    
    index = 0
    for product in products: 
        if(index == len(id)): 
            break
        
        if(product.get_attribute('src') != None):
            url = product.get_attribute('src')
            url = url_filter(index, url, url_conditions, ID_list)
            if(url != None):
                img_url.append(url)   
                ID = id[index].get_attribute('data-id')           #상품 id추출
                ID_list.append(ID)
            
                key = id[index].get_attribute('id')                 #상품 이름 추출하기 위한 path용 id 추출
                item = driver.find_element_by_xpath(path_f + str(key) + path_b).get_attribute('textContent')
                item_name.append(item)
                index+=1
                
    data_df = pd.DataFrame({'Item': item_name,
                       'url': img_url,
                        'Category':category,
                       'Style':style}, index = ID_list)
    return data_df


# In[1]:


def img_saver(img_url, category):
    img_folder = './img/'+category 
    
    if not os.path.isdir(img_folder) : # 없으면 새로 생성하는 조건문 
        os.mkdir(img_folder)
    
    for index, link in enumerate(img_url) :
        urlretrieve(link, f'./img/RockChic/{ID_list[index]}.jpg')


# In[83]:



Celine_category = ['jackets-and-trousers','shirts','knitwear','denim','t-shirts-and-sweatshirts','coats-and-blousons','leather-and-fur']
Saint_Laurent_category = ['shirts','denim','t-shirts-and-sweatshirts','knitwear','jackets-and-pants','leather-and-fur','outerwear']
Amiri_category =['knits','blazers','outerwear','shirting','sweatshirts','tees','denim','pants','shorts','sweatpants']


# In[12]:


Saint_Laurent('shirts','RockChic', driver, chrome_options)


# In[21]:


def Dataframe_generator(function, categories, style, driver, chrome_options):
    
    for i,category in enumerate(categories):
        if(i==0): 
            df = function(category, style, driver, chrome_options)
        else: 
            temp = function(category, style, driver, chrome_options)
            df = pd.concat([df, temp])
    return df
        
    


# In[39]:


a = 'https://cdn-images.farfetch-contents.com/17/06/29/27/17062927_34272853_480.jpg'
b = re.findall('_(\d.*?)_',a)
b


# In[ ]:




