import requests
import gevent
from gevent import monkey
import re
from pypinyin import lazy_pinyin
import time
import random
import csv

monkey.patch_all()


def get_service_info(spu_id, url, ids_bug):
    time.sleep(random.uniform(2, 3))
    url = url + "?tab=0&rj=0"

    headers = {
        "Cookie": "smidV2=202108111914527c82de7c31a1ebbb6f65a12dfd5021de00aefc1da947e1af0; _ga=GA1.2.1705589232.1628680492; _gid=GA1.2.1191277434.1628680492; PHPSESSID=e81ba73301b430097db1c9599826880a; __usersign__=1628680488549938593",
        "User-Agent": "Mozilla/5.0 (iPhone; CPU iPhone OS 13_6_1 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Mobile/15E148 AliApp(DingTalk/6.0.23) com.laiwang.DingTalk/15108471 Channel/201200 language/zh-Hans-CN UT4Aplus/0.0.6 WK",
        "Accept-Language": "zh-cn",
        "Accept-Encoding": "gzip, deflate, br",
        "Connection": "keep-alive",
        "DingTalk-Flag": "1",
        "Accept": "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8",
        "Host": "m.soyoung.com"
    }
    response_res = requests.get(url, headers=headers, verify=False, timeout=30)
    print("url:%s,status:%s" % (url, response_res.status_code))
    sub_title_dict = {}
    if response_res.status_code == 200:
        if spu_id in ids_bug:
            ids_bug.remove(spu_id)
        sub_title = re.compile('<span class="sub-title">(.*?)</span><span class=".*?">(.*?)</span>').findall(
            response_res.text)
        for item in sub_title:
            times = item[0].strip()
            price = item[1].strip().split("<")[0]
            sub_title_dict[times] = price

        x_p_o = re.compile('<span class="x-p-o">(.*?)</span>').findall(response_res.text)
        if x_p_o:
            sub_title_dict['门市价'] = x_p_o
    else:
        ids_bug.append(spu_id)
        time.sleep(600)
    return sub_title_dict


def gevent_test():
    tasks = []
    for city_name in ['合肥市', '芜湖市', '蚌埠市', '淮南市']:
        for word in ['瘦脸针', '双眼皮', '光子嫩肤', '吸脂', '水光针', '玻尿酸', '热玛吉']:
            tasks.append(
                gevent.spawn(get_service_info, query=word, city_name="".join(lazy_pinyin(city_name)))
            )
    print(tasks)
    gevent.joinall(tasks)


def get_cika_info_to_csv(get_data=False):
    read_file = open("soyoung_service.csv", "r", encoding="utf-8")
    write_file = open("soyoung_service_cika.csv", "a+", encoding="utf-8")
    all_cika_title = list()
    ids_bug = []
    have_read_service = []
    for item in read_file.readlines():
        try:
            cika_price_dict = dict()
            print("index:", item.strip().split(",")[0])
            if int(item.strip().split(",")[0]) > 64526:
                service_id = item.strip().split(",")[2]
                url = item.strip().split(",")[-1]
                if service_id in have_read_service:
                    pass
                else:
                    have_read_service.append(service_id)
                    sub_title_dict = get_service_info(str(service_id), url, ids_bug)
                    if get_data == False:
                        if sub_title_dict:
                            all_cika_title.extend(sub_title_dict.keys())
                            cika_price_dict[service_id] = sub_title_dict
                            print(cika_price_dict)
                            write_file.write(str(cika_price_dict))
                            write_file.write("\n")
                    else:
                        return sub_title_dict

        except Exception as e:
            print(e)
            pass

    print(all_cika_title)
    read_file.close()
    write_file.close()


def read_cika_info():
    all_cika_title = {}
    cika_file = open("soyoung_service_cika.csv", "r", encoding="utf-8")
    for item in cika_file.readlines():
        cika = eval(item)
        for key, values in cika.items():
            all_cika_title[key] = values

    print(all_cika_title)
    cika_file.close()
    return all_cika_title


def np_write_csv_data():
    all_cika_title = read_cika_info()
    write_file = open("soyoung_service_write_cika.csv", "a+", encoding="utf-8")
    # /Users/edz/Desktop/xinyang.csv
    with open("soyoung_service.csv", encoding='utf-8') as f:
        reader = csv.reader(f)
        header_row = next(reader)
        print(header_row)
        for row in reader:
            service_id = row[2]
            cika_info = all_cika_title.get(service_id)
            if cika_info:
                print('cika_info:', cika_info)
                write_file.write(str(row.append(cika_info)))
                write_file.write("\n")
            else:
                sub_title_dict = get_cika_info_to_csv(get_data=True)
                print('sub_title_dict:', sub_title_dict)
                write_file.write(str(row.append(sub_title_dict)))
                write_file.write("\n")

    write_file.close()


if __name__ == '__main__':
    get_cika_info_to_csv()
