import re
import os
import requests
import time
from webdav3.client import Client
from webdav3.exceptions import LocalResourceNotFound, RemoteResourceNotFound

requests.packages.urllib3.disable_warnings()

def pixiv_handler(entries):
    # 整理为JSON数组
    pixiv_list = []
    for entry in entries:
        links = []
        for i in entry['content']:
            pattern = re.compile(
                r'http[s]?://(?:[a-zA-Z]|[0-9]|[$-_@.&+]|[!*\(\),]|(?:%[0-9a-fA-F][0-9a-fA-F]))+')
            links = (re.findall(pattern, i['value']))

        tag_pattern = re.compile(r'tags/(.*)/')
        item = {'id': entry['id'], 'title': entry['title'], 'link': links, 'author': entry['author'], 'tag': re.findall(tag_pattern, entry['source']['id'])}

        pixiv_list.append(item)
    return pixiv_list

def pixiv_result_handler(entries):
    list = pixiv_handler(entries)
    
    success_entries = []
    failed_entries = []
    
    need_sync_files = []
    for item in list:
        code = item['id']
        tag = ','.join(item['tag'])
        title = item['title']
        author = item['author']
        prefix_name = f'#{tag}# @{author}@ {title}'
        
        result_flag = True
        
        for url in item['link']:
            file_name_pattern = re.compile(r'\/(\w*\.(?:jpg|png))$')
            file_name = ','.join(re.findall(file_name_pattern, url))
            
            if file_name:
                full_name = f'{prefix_name} {file_name}'
                (status, data) = downloadPic(url)
                if status:
                    saveFile(full_name, data)
                    need_sync_files.append({ 'id': item['id'], 'file_name': full_name })
                else:
                    result_flag = False
                time.sleep(10)
        
        if not(result_flag):
            for entry in entries:
                if entry['id'] == item['id']:
                    failed_entries.append(entry)
    (success_ids, failed_ids) = sync(need_sync_files)
    
    for entry in entries:
        if entry['id'] in success_ids:
            success_entries.append(entry)
        elif entry['id'] in failed_ids:
            failed_entries.append(entry)
    
    return (success_entries, failed_entries)
    
def sync(file_list):
    success_ids = []
    failed_ids = []
    
    options = {
        'webdav_hostname': 'https://pan.liliyamol.cn:8081/webdav/',
        'webdav_login': 'mol',
        'webdav_password': 'YvG4SkF82qd7ks',
        'disable_check': True,
    }

    client = Client(options)
    temp_path = os.path.join(os.path.dirname(__file__), 'temp')

    for file_item in file_list:
        path = os.path.join(temp_path, file_item['file_name'])
        try:
            client.upload('Pictures/ACGN/Pixiv/' + file_item['file_name'], path)
            print('Info: ' + file_item['file_name'] + ' upload success!!')
        except LocalResourceNotFound as exception:
            print('Error: ' + file_item['file_name'] + ' upload failed!!')
            if not(file_item['id'] in failed_ids):
                failed_ids.append(file_item['id'])
        removeFile(path)
    
    for file_item in file_list:
        if not(file_item['id'] in failed_ids):
            success_ids.append(file_item['id'])
    
    return (success_ids, failed_ids)
      
def saveFile(name, data):
    temp_path = os.path.join(os.path.dirname(__file__), 'temp')
    with open(os.path.join(temp_path, name), 'wb') as code:
        code.write(data)

def removeFile(path):
    if len(path) > 0:
        try:
            os.remove(path)
        except:
            pass
        
def downloadPic(url):
    headers = {
        "User-Agent": "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/63.0.3239.132 Safari/537.36 QIHU 360SE",
    }
    data = None
    status = False
    try:
        proxies={'https': 'http://192.168.124.12:7890','http': 'http://192.168.124.12:7890'}
        res = requests.get(url, headers = headers, verify=False, timeout=(5,5), proxies=proxies)
        data = res.content
        status = True
        res.close()
        print(f'Info: download success {url}')
    except:
        print(f'Error: download failed {url}')
    return (status, data)