# 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 ChromePool
import json
from tools_hjh import HTTPTools
from tools_hjh.Tools import echo, rm, cat
import ast

Tools.unzip_chromedriver()

Tools.mkdir('log')
Tools.mkdir('tmp')
Tools.mkdir('data/download')

date = Tools.locatdate()
log = Log('log/' + date + '.log')

host = 'https://avbebe.com/'

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',
    'dnt': '1',
    'pragma': 'no-cache',
    'sec-ch-ua': '".Not/A)Brand";v="99", "Google Chrome";v="103", "Chromium";v="103"',
    '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/103.0.0.0 Safari/537.36',
    'Referer': 'https://avbebe.com/'
}

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
    
# SELECT 'm_pages.append(('''||url||''', '''||name||'【'||date||'】.mp4'||'''))' FROM T_M t where t.date like '2023%' order by date;'


def main(): 
    m_pages = []
    m_pages.append(('https://avbebe.com/archives/137431', 'けものっ娘通信〜The Animation〜 猫娘ニア[中文字幕]【20230102】.mp4'))
    
    params = get_url(m_pages, sys_argv_3)
    echo(str(params), 'tmp/avbebe_lifan.par')

    download('tmp/avbebe_lifan.par', r'E:\avbebe')
    # rm('tmp/avbebe_lifan.par')

    
def get_url(m_pages, sys_argv_3):
    params = []
    chrome = ChromePool(sys_argv_3, chrome_path, chromedriver_path, is_display_picture=False, is_hidden=False)

    def get_one(m_page, name, chrome):
        page = chrome.get(m_page)
        bs = BeautifulSoup(page, features="lxml")
        
        # type
        try:
            as_ = bs.find('div', class_='jeg_meta_category').find_all('tr')
            type_ = ''
            for a in as_:
                typ_ = a.text
                type_ = type_ + typ_ + '、'
            type_ = type_[0:-1]
        except Exception as _:
            log.error('getE', m_page, 'tag not find')
            type_ = None
        
        # tags
        try:
            tags = ''
            as_ = bs.find('div', class_='jeg_post_tags').find_all('a', rel='tag')
            for a in as_:
                tag = a.text
                tags = tags + tag + '、'
            tags = tags[0:-1]
        except:
            tags = None
        
        # name_ video_url picture_url subtitles_url
        if '.mp4' not in page.lower() and '.m3u8' not in page.lower():
            Tools.echo(m_page, 'no_video.txt', 'a')
            log.error('getE', m_page, '不存在链接')
        else: 
            try:  # 一类json
                content = bs.find('div', attrs={"id":True, "data-item":True})['data-item']
                obj = json.loads(content)
                try:
                    if name is None:
                        name_ = obj['fv_title']
                    else:
                        name_ = name
                except:
                    name_ = ''
                    
                try:
                    video_url = ''
                    sources = obj['sources']
                    for source in sources:
                        video_url = video_url + source['src'] + '、'
                    video_url = video_url[0:-1]
                except:
                    video_url = None
                    
                try:
                    picture_url = obj['splash']
                except:
                    picture_url = None
                    
                try:
                    subtitles_url = ''
                    subtitles = obj['subtitles']
                    for subtitle in subtitles:
                        subtitles_url = subtitles_url + subtitle['src'] + '、'
                    subtitles_url = subtitles_url[0:-1]
                except:
                    subtitles_url = None
                    
                if video_url is not None:
                    param = (m_page, name_, picture_url, video_url, subtitles_url, tags, type_)
                    params.append(param)
            except:
                content = None
            
            if content is None:  # 二类json
                try:
                    as_ = bs.find_all('a', attrs={"data-fancybox":True, "itemprop":True, "data-item":True, "itemscope":True, "itemtype":True, "href":True})
                    for a in as_:
                        content = a['data-item']
                        obj = json.loads(content)
                        try:
                            if name is None:
                                name_ = obj['fv_title']
                            else:
                                name_ = name
                        except:
                            name_ = ''
                        try:
                            video_url = ''
                            sources = obj['sources']
                            for source in sources:
                                video_url = video_url + source['src'] + '、'
                            video_url = video_url[0:-1]
                        except:
                            video_url = None
                        try:
                            picture_url = obj['splash']
                        except:
                            picture_url = None
                        try:
                            subtitles_url = ''
                            subtitles = obj['subtitles']
                            for subtitle in subtitles:
                                subtitles_url = subtitles_url + subtitle['src'] + '、'
                            subtitles_url = subtitles_url[0:-1]
                        except:
                            subtitles_url = None
                            
                        if video_url is not None:
                            param = (m_page, name_, picture_url, video_url, subtitles_url, tags, type_)
                            params.append(param)
                except:
                    content = None
                    
    if sys_argv_3 < 4:
        sys_argv_3 = 4
    tp = ThreadPool(sys_argv_3)
    for m_page, name in m_pages:
        tp.run(get_one, (m_page, name, chrome))
    tp.wait()
                
    chrome.close()
    return params


def download(params, filepath_=r'E:/avbebe_lifan'):
    headers_ = 'referer:https://avbebe.com/'
    exe_path = 'E:/MyApps/N_m3u8DL-CLI/N_m3u8DL-CLI_v3.0.2.exe'

    def d_one(param):
        try:
            video_url = param[3]
            subtitles_url = param[4]
            name = param[1].replace('/', ' ').replace('\\', ' ').replace(':', '：').replace('*', '·').replace('?', '？').replace('"', '\'').replace('<', '[').replace('>', ']').replace('|', ' ')
            name = Tools.merge_spaces(name)
            name = name.replace('＃', '#')
            name = name.replace('] ', ']').replace(' [', '[')
            name = name.replace('～ ', '～').replace(' ～', '～')
            name = name.replace('# ', '#').replace(' #', '#')
            name = name.replace('。 ', '。').replace(' 。', '。')
            name = name.replace('( ', '(').replace(' (', '(')
            name = name.replace('！ ', '！').replace(' ！', '！')
            name = name.replace('OVA ', 'OVA').replace(' OVA', 'OVA')
            if subtitles_url != 'None' and subtitles_url is not None:
                subtitles_flags = False
                if '、' in subtitles_url:
                    for s_url in subtitles_url.split('、'):
                        if '簡' in s_url or '简' in s_url:
                            HTTPTools.download(s_url, filepath=filepath_ + '/' + name + '.vtt', headers=headers)
                            subtitles_flags = True
                    if not subtitles_flags:
                        HTTPTools.download(subtitles_url[0], filepath=filepath_ + '/' + name + '.vtt', headers=headers)
                else:
                    HTTPTools.download(subtitles_url, filepath=filepath_ + '/' + name + '.vtt', headers=headers)                    
            if video_url.endswith('.m3u8'):
                HTTPTools.N_m3u8DL_CLI(url=video_url, filepath=filepath_, filename=name, theadnum=32, exe_path=exe_path, headers=headers_)
            elif video_url.endswith('.mp4'):
                HTTPTools.download(video_url, filepath=filepath_ + '/' + name + '.mp4', headers=headers)
            echo(str(param), 'tmp/downloaded.par')
        except Exception as _:
            echo(str(param), 'tmp/exception.par')
        
    params = cat(params)
    params = ast.literal_eval(params)
    tp = ThreadPool(4)
    for param in params:
        if str(param) not in cat('tmp/downloaded.par') and str(param) not in cat('tmp/exception.par'):
            tp.run(d_one, (param,))
    tp.wait()


if __name__ == '__main__':
    main()
    
