# -*- coding: utf-8 -*-
import os
import time
import sys
import lineNotify as line
import json
from datetime import datetime as dt
import chromedriver_binary
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
from bs4 import BeautifulSoup
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.action_chains import ActionChains
from selenium.webdriver.support.ui import Select
import kindle_sqlite
import urllib.request
from datetime import datetime as dt
from pprint import pprint
from webdriver_manager.chrome import ChromeDriverManager

keys_json = open('config.json', 'r')
keys = json.load(keys_json)


#任意に変更してください
MAIL_ADDRESS = keys["mail_address"]
PASSWORD = keys["password"]
# DRIVER_PATH = keys["driver_path"]
BASE_URL = 'https://affiliate.amazon.co.jp/home/'

TARGET_UPDATE_URL = "https://comicy.jp/items/update_items"
# TARGET_UPDATE_URL = "http://192.168.10.20/items/update_items"

associate_tags = ['koooo10000-22']
# associate_tags = ['allstars-22']


#キャッシュディレクトリの作成
path = os.getcwd()
tmp_dir_name = 'tmp_data'
os.makedirs(tmp_dir_name, exist_ok=True)






if __name__ == '__main__':
    try:

        print("start")

        #テーブルの作成
        kindle_sqlite.createTableKindleItems()






        list = []
        options = Options()
        options.add_argument('--user-data-dir='+ path + '\\' + tmp_dir_name)
        #https://www.cloudgate.jp/ua.php
        #ここにUAを書きます。
        #--user-agent= ***
        if('user_agent' in keys == False):
            options.add_argument('--user-agent=' + keys["user_agent"])
        else:
            options.add_argument('--user-agent=' + 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/87.0.4280.88 Safari/537.36')


        # driver = webdriver.Chrome(chrome_options=options)
        driver = webdriver.Chrome(ChromeDriverManager().install(),options=options)




        #ActionChainsのインスタンスを作成   
        driver_action = ActionChains(driver)

        today_str = dt.now().strftime('%Y-%m-%d')
        print(today_str)
        # sys.exit()
        for associate_tag in associate_tags:
            print(today_str + " - Amazonアクセス：" + associate_tag)

            # https://affiliate.amazon.co.jp/home/store?ref_url=%2Fhome%2Freports&idbox_store_id=koooo10000-22
            
            url = "https://affiliate.amazon.co.jp/home/reports/table.json?"
            url += "query%5Btype%5D=realtime&"
            url += "query%5Bstart_date%5D="+today_str+"&"
            url += "query%5Bend_date%5D="+today_str+"&"
            url += "query%5Border%5D=desc&"
            url += "query%5Btag_id%5D=all&"
            url += "query%5Bcolumns%5D=product_title%2Casin%2Cproduct_category%2Cmerchant_name%2Cordered_items%2Ctracking_id%2Cprice&"
            url += "query%5Bsort%5D=day&"
            url += "query%5Blimit%5D=3000&"
            url += "store_id=" + associate_tag + "&"

            driver.get(url)
            time.sleep(3)

            if len(driver.find_elements_by_id("ap_email")) > 0:
                driver.find_element_by_id("ap_email").send_keys(MAIL_ADDRESS)
            if len(driver.find_elements_by_id("ap_password")) > 0:
                driver.find_element_by_id("ap_password").send_keys(PASSWORD)
                
            if len(driver.find_elements_by_id("signInSubmit")) > 0:
                driver.find_element_by_id("signInSubmit").click()
                time.sleep(3)
                print("login ok")




            html = driver.page_source.encode('utf-8')
            soup = BeautifulSoup(html, 'lxml')

            info = soup.find("pre").text
            json_data = json.loads(info)

            # json = json.loads(info)
            for row in json_data['records']:
                # asin = row['asin']
                item = kindle_sqlite.checkItem(row['asin'])

                if item != None:
                    print(row['asin'] + " - " + row['product_title'] + " - テーブルにすでに登録されています。")
                    through_flg = 1
                else:
                    # browser.get(link)
                    # print(link)
                    # time.sleep(random.choice(l))

                    #データの追加
                    data = {
                        "asin":row['asin'],
                        "title":row['product_title'],
                        "tracking_id":row['tracking_id'],
                        "product_category":row['product_category'],
                        "merchant_name":row['merchant_name'],
                        "price": row['price'],
                        "point": -1,
                    }
                    kindle_sqlite.insertData(data)


            # month_total_buy_price = 0
            # month_total_buy_count = 0
            # cursor = kindle_sqlite.getDayItems()
            # res = month_total_buy_items.fetchall()
            # print(cursor.fetchall())
            # for row in cursor:
            #     # print(row['price'])
            #     month_total_buy_price += row['price']
            #     month_total_buy_count += 1

            # print(month_total_buy_price)
            # print(month_total_buy_count)
            # driver.quit()


            # httpリクエストを準備してPOST
            # url = "https://buypy.jp/items/update_items"
            url = TARGET_UPDATE_URL
            method = "POST"
            obj = {
                "items" : json_data['records'],
            }

            json_data = json.dumps(obj).encode("utf-8")
            headers = {"Content-Type" : "application/json"}

            request = urllib.request.Request(url, data=json_data, headers=headers, method=method)
            with urllib.request.urlopen(request) as response:
                response_body = response.read().decode("utf-8")
                print(response_body)






            #集計
            # url = "https://affiliate.amazon.co.jp/home/reports/table.json?"
            # url += "query%5Btype%5D=earnings&"
            
            # url += "query%5B" + "start_date%5D="+today_str+"&"
            # url += "query%5B" + "end_date%5D="+today_str+"&"
            # url += "query%5B" + "order%5D=desc&"
            # url += "query%5B" + "tag_id%5D=all&"
            # url += "query%5B" + "last_accessed_row_index%5D=0&"
            # url += "query%5B" + "group_by%5D=tag_id&"
            # url += "query%5B" + "columns%5D=tag_value%2Cclicks%2Ctag_id%2Cshipped_items%2Crevenue%2Chva_total_earnings%2Ccommission_earnings%2Creturned_items%2Creturned_revenue%2Creturned_earnings&"
            # url += "query%5B" + "skip%5D=0&"
            # url += "query%5B" + "sort%5D=shipped_items&"    
            # url += "query%5B" + "limit%5D=3000&"
            # url += "store_id=" + associate_tag + "&"

            # # https://affiliate.amazon.co.jp/home/reports/table.json?query%5Btype%5D=earnings&query%5Bstart_date%5D=2021-01-01&query%5Bend_date%5D=2021-01-13&query%5Btag_id%5D=all&query%5Border%5D=desc&query%5Bdevice_type%5D=all&query%5Blast_accessed_row_index%5D=0&query%5Bgroup_by%5D=tag_id&query%5Bcolumns%5D=tag_value%2Cclicks%2Ctag_id%2Cshipped_items%2Crevenue%2Chva_total_earnings%2Ccommission_earnings%2Creturned_items%2Creturned_revenue%2Creturned_earnings&query%5Bskip%5D=0&query%5Bsort%5D=shipped_items&query%5Blimit%5D=25&store_id=koooo10000-22

            # driver.get(url)
            # time.sleep(3)

            # html = driver.page_source.encode('utf-8')
            # soup = BeautifulSoup(html, 'lxml')

            # info = soup.find("pre").text
            # json_data = json.loads(info)

            # url = TARGET_UPDATE_TAG_URL
            # method = "POST"
            # obj = {
            #     "json_data" : json_data,
            # }

            # json_data = json.dumps(obj).encode("utf-8")
            # headers = {"Content-Type" : "application/json"}

            # request = urllib.request.Request(url, data=json_data, headers=headers, method=method)
            # with urllib.request.urlopen(request) as response:
            #     pprint(response)
            #     response_body = response.read().decode("utf-8")
            #     print(response_body)



            print("処理終了")



    except Exception as e:
        exc_type, exc_obj, exc_tb = sys.exc_info()
        fname = os.path.split(exc_tb.tb_frame.f_code.co_filename)[1]
        print(exc_type, fname, exc_tb.tb_lineno)

    finally:
        driver.quit()
        print("end")

