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

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

host = 'https://sissy.game'

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

headers = {
    'accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9',
    'accept-language': 'zh-CN,zh;q=0.9',
    'cache-control': 'no-cache',
    'cookie':'66b513be114e386c60be7555c767e261latest_time_id=3794; theme_dark=0',
    'dnt': '1',
    'pragma': 'no-cache',
    'sec-ch-ua': '"Google Chrome";v="107", "Chromium";v="107", "Not=A?Brand";v="24"',
    'sec-ch-ua-mobile': '?0',
    'sec-ch-ua-platform': '"Windows"',
    'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/107.0.0.0 Safari/537.36'
}

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

def main():
    req = ChromePool(1, chrome_path, chromedriver_path, is_display_picture=False, is_hidden=False)
    download('/page/PAGEIDX/', req)

    
def download(first_page_url, req):
    tp = ThreadPool(sys_argv_3)

    def download_one_page(idx, req):
        pageurl = host + first_page_url.replace('PAGEIDX', str(idx))
        page = req.get(pageurl)
        bs = BeautifulSoup(page, features="lxml")
        divs = bs.find_all('div', class_='single-post panel box-shadow-wrap-normal')
        for div in divs:
            name_ = div.find('h2', class_='m-t-none text-ellipsis index-post-title text-title').find('a').text
            date_ = div.find('div', class_='text-muted post-item-foot-icon text-ellipsis list-inline').find_all('li')[1].text
            date_ = date_.replace(' 年 ', '').replace(' 月 ', '').replace(' 日', '')
            try:
                i = 0
                e_url = div.find('div', class_='index-post-img').find('a')['href']
                e_page = req.get(e_url)
                bs = BeautifulSoup(e_page, features="lxml")
                for img in bs.find_all('img', class_='block'):
                    i = i + 1
                    pic_url = img['src']
                    if i > 1:
                        pic_name = date_ + '_' + name_ + '_' + str(i) + '.jpg'
                    else:
                        pic_name = date_ + '_' + name_ + '.jpg'
                    pic_name = Tools.merge_spaces(pic_name)
                    pic_name = pic_name.replace('/', ' ').replace('\\', ' ').replace(':', '：').replace('*', '·').replace('?', '？').replace('"', '\'').replace('<', '[').replace('>', ']').replace('|', ' ') 
                    pic_name = pic_name.replace('＃', '#')
                    pic_name = pic_name.replace('] ', ']').replace(' [', '[')
                    pic_name = pic_name.replace('～ ', '～').replace(' ～', '～')
                    pic_name = pic_name.replace('# ', '#').replace(' #', '#')
                    pic_name = pic_name.replace('。 ', '。').replace(' 。', '。')
                    pic_name = pic_name.replace('( ', '(').replace(' (', '(')
                    pic_name = pic_name.replace('！ ', '！').replace(' ！', '！')
                    pic_name = 'E:/MyFiles/MyPy/download/sissy_game/' + pic_name
                    if 'uploads' in pic_url:
                        tp.run(download_, (pic_url, pic_name, headers))
            except:
                log.error('download', idx, str(_))
    
    page = get_host(req, host)
    bs = BeautifulSoup(page, features="lxml")
    max_page = bs.find('ol', class_='page-navigator').find_all('li')[-2].text
    for idx in range(1, int(max_page) + 1):
        download_one_page(idx, req)
    req.close()
    tp.wait()

    
def download_(pic_url, pic_name, headers):
    try:
        if not os.path.exists(pic_name):
            HTTPTools.download(pic_url, pic_name, headers)
            log.info('download', 'no exists', pic_name, pic_url)
        else:
            net_size = HTTPTools.get_size(pic_url, headers)
            file_size = os.path.getsize(pic_name)
            if net_size == file_size:
                log.info('download', 'exists', pic_name, pic_url)
            else:
                rm(pic_name)
                HTTPTools.download(pic_url, pic_name, headers)
                log.info('download', 'no same size', pic_name, pic_url)
    except Exception as _:
        time.sleep(1)
        log.warning('download', 'retry', pic_name, pic_url)
        download_(pic_url, pic_name, headers)

        
def get_host(req, host):
    try:
        page = req.get(host)
    except:
        get_host(req, host)
    return page

    
if __name__ == '__main__':
    main()

