# coding:utf-8
import sys
from tools_hjh import DBConn, HTTPTools
from tools_hjh import Log
from tools_hjh import ThreadPool
from tools_hjh import Tools
from bs4 import BeautifulSoup
from tools_hjh import ChromePool
import os
import time
import json

date = Tools.locatdate()
log = Log('E:/MyFiles/MyPy/log/woqukan/' + date + '.log')

host = 'https://avbebe.com'

chrome_path = r'D:\MyApps\CentBrowser\App\chrome.exe'
chromedriver_path = r'D:\MyApps\CentBrowser\chromedriver.exe'

try:
    sys_argv_1 = sys.argv[1]
except:
    sys_argv_1 = 'download'
    sys_argv_1 = 'rename'
    sys_argv_1 = 'get'
    sys_argv_1 = 'get'
try:
    sys_argv_2 = sys.argv[2]
except:
    sys_argv_2 = None
try:
    sys_argv_3 = sys.argv[3]
except:
    sys_argv_3 = 2
    

def main(): 
    db = DBConn('sqlite', db='E:/MyFiles/MyPy/data/avbebe_lifan.db')
    createDatabase(db, rebuild=False)
    get_main(db, HTTPTools, '/archives/category/h動畫影片/page/[page_idx]', page_num='')
    # get_main(db, HTTPTools, '/archives/category/3d動畫/page/[page_idx]', page_num='')

    
def createDatabase(db, rebuild=False):
    if rebuild:
        db.run('drop table if exists t_m')

    t_m = '''
        create table if not exists t_m(
            url text,
            name text,
            type text,
            date text
        )
    '''
    db.run(t_m)


def get_main(db, request, firstPageUrl, page_num=''):

    def get_main_one(idx):
        try:
            params = []
            sql = 'insert into t_m select ?,?,?,? where not exists(select 1 from t_m where url = ?)'
            pageurl = host + firstPageUrl.replace('[page_idx]', str(idx))
            page = request.get(pageurl)
            bs = BeautifulSoup(page, features="lxml")
            articles = bs.find_all('article', class_='jeg_post jeg_pl_md_5 format-video')
            for article in articles:
                div_category = article.find('div', class_='jeg_post_category')
                div_content = article.find('div', class_='jeg_postblock_content')
                url = div_content.find('h3', class_='jeg_post_title').find('a')['href'].strip()
                name = div_content.find('h3', class_='jeg_post_title').find('a').text.strip()
                type_ = div_category.find('a').text
                date_ = div_content.find('div', class_='jeg_meta_date').find('a').text
                date_list = date_.split(' ')
                if len(date_list[1]) == 1:
                    dd = '0' + date_list[1]
                else:
                    dd = date_list[1]
                if len(date_list[2]) == 1:
                    mm = '0' + date_list[2]
                else:
                    mm = date_list[2]
                yyyy = date_list[4]
                date_ = yyyy + mm + dd
                params.append((url, name, type_, date_, url))
            num = db.run(sql, params)
            print(pageurl, num, len(params))
        except Exception as _:
            print(idx, _)
    
    tp = ThreadPool(16)
    
    if page_num is None or len(page_num) == 0 or page_num == '':
        pageurl = host + firstPageUrl.replace('[page_idx]', '1')
        page = request.get(pageurl)
        bs = BeautifulSoup(page, features="lxml")
        href = bs.find('a', class_='last')
        page_num = int(href.text)
    for idx in range(1, int(page_num) + 1):
        tp.run(get_main_one, (idx,))
    tp.wait()


if __name__ == '__main__':
    main()
    
