
import json
import re
from datetime import datetime
import os
os.environ['NO_PROXY'] = 'stackoverflow.com'
import logging
logging.captureWarnings(True)
from all_connect import ConnectSpider
Con = ConnectSpider()
import os
os.environ['NO_PROXY'] = 'stackoverflow.com'
import random
import requests
import time
from to_upload_id import aa_496775268


class Stock_Detail():
    def __init__(self):
        self.headers = {
            'accept': 'application/json',
            'accept-language': 'zh-CN,zh;q=0.9',
            # 'cookie': 'stck_anonymous_id=7c4b5046-d8eb-4ee5-8bac-893a170fa766; sstk_anonymous_id=7c4b5046-d8eb-4ee5-8bac-893a170fa766; _gcl_au=1.1.874517867.1723276566; __ssid=c064478ee3926370e6737c3582c60d0; _ga=GA1.1.675721154.1723276566; did=02a1c297-357e-400b-87f5-41457a394697; accts_customer=903859535; accts_customer_sso1=331378495-undefined; next.sid=s%3Agy3BgikznI1m6vIui-5gGcFYcjWzDFIK.OXZW%2FtHJ8HVG%2Fx9eXhfAilGM0zicWdtakg1aacGy8OM; _4c_=%7B%22_4c_mc_%22%3A%222a68f713-f2b5-49b7-8e45-07549f8a66d8%22%7D; FPID=FPID2.2.eJMnQEWeDZwuxnkWmESNukKGwRAIKHd9t3eewKvzeQQ%3D.1723276566; visitor_id=73458769441; htjs_user_id=331378495; htjs_anonymous_id=7c4b5046-d8eb-4ee5-8bac-893a170fa766; __rtbh.uid=%7B%22eventType%22%3A%22uid%22%2C%22id%22%3A%227c4b5046-d8eb-4ee5-8bac-893a170fa766%22%7D; slireg=https://scout.us1.salesloft.com; extole_access_token=BV1L1U32MH7O4IOEUHOH6F88AJ; sliguid=2259d959-8b66-4544-8a26-ee6f9e0d872c; slirequested=true; n_v=2d4faa42e64; locale=zh; NEXT_LOCALE=zh; hl=zh; sstk_session_start=2024-08-12T00%3A50%3A01.185Z; stck_session_id=eb218863-1d7b-453e-b68f-826b54a4f567; sstk_session_id=eb218863-1d7b-453e-b68f-826b54a4f567; downlink=slow; visit_id=81252382944; gtm_monit_roll=99; OptanonConsent=consentId=5cff2293-6adc-4e35-960b-a301a65fc215&datestamp=Mon+Aug+12+2024+08%3A50%3A04+GMT%2B0800+(%E4%B8%AD%E5%9B%BD%E6%A0%87%E5%87%86%E6%97%B6%E9%97%B4)&version=202403.2.0&interactionCount=0&isAnonUser=1&isGpcEnabled=0&browserGpcFlag=0&isIABGlobal=false&hosts=&landingPath=NotLandingPage&groups=C0001%3A1%2CC0003%3A1%2CC0002%3A1%2CC0005%3A1%2CC0004%3A1%2CC0007%3A1&AwaitingReconsent=false; OptanonCachedGroups=,C0001,C0003,C0002,C0005,C0004,C0007,; FPLC=xYngCOYQalQCTnXJSrL5MkPtTggGURkuZLMfzokC8ITFBHfwhBl4NrpZuNrp44Zw2eHfL22tL9Q1ejmZot6AEMzltj2Hvtapbeu8iPj20h1y4yHKQZLIWATPUg4j1A%3D%3D; datadome=1Q5Il~6ANKiyum9UFnRoJyruo7C1U0TXOsxV_tOU4rYdEk5a6dO3mjUEFLs5FgsAbSj2xGvoSUp3jYNLxrjs~XHUOwCU9L~8nimhKtYEznVW_AeNZ23e2zxJLf2GHikU; Authorization=1%2FeyJjbGllbnRfaWQiOiJhMGI3Ni1hN2Y1ZS1mZWRlYy1iYmViZS1mYTY1Yi04NTcxOSIsInJlYWxtIjoiY3VzdG9tZXIiLCJzY29wZSI6InVzZXIudmlldyB1c2VyLmVtYWlsIHVzZXIuYWRkcmVzcyB1c2VyLmVkaXQgb3JnYW5pemF0aW9uLnZpZXcgb3JnYW5pemF0aW9uLmFkZHJlc3MgY29sbGVjdGlvbnMudmlldyBjb2xsZWN0aW9ucy5lZGl0IGxpY2Vuc2VzLnZpZXcgbGljZW5zZXMuY3JlYXRlIG1lZGlhLnVwbG9hZCBtZWRpYS5zdWJtaXQgbWVkaWEuZWRpdCBwdXJjaGFzZXMudmlldyBwdXJjaGFzZXMuY3JlYXRlIiwidXR2IjoicUhpNCIsImVtYWlsX3ZlcmlmaWVkIjp0cnVlLCJ1c2VybmFtZSI6IjkwMzg1OTUzNSIsInVzZXJfaWQiOjMzMTM3ODQ5NSwib3JnYW5pemF0aW9uX2lkIjpudWxsLCJwYXJlbnRfb3JnYW5pemF0aW9uX2lkcyI6W10sImN1c3RvbWVyX2lkIjozMzEzNzg0OTUsImV4cCI6MTcyMzQyNzMyMX0.QcAGSAHi_1fLXJQReHBAtupw2qzn6DXiX1nsLVOoxg6OEe277kv3zt85EsJHen0_DmoHdMS35s4Fh418lXWR-w; _uetsid=d11e0420584411efb988e71c7217a51d|17zp06m|2|fo9|0|1685; _uetvid=58dc29704a6511ef8f5e5754465f397d|1dj9d84|1723425007889|3|1|bat.bing.com/p/insights/c/i; _ga_H22ZZQTXLV=GS1.1.1723423806.2.1.1723425008.0.0.0; _ga_SSGTMSSTK=GS1.1.1723423806.2.1.1723425008.0.0.1890544395; _ga_5JRYE4Y8J9=GS1.1.1723423806.2.1.1723425008.59.0.0',
            'if-modified-since': 'Sat, 10 Aug 2024 08:09:24 GMT',
            'if-none-match': 'W/"17t8cdyfo1s1un"',
            'newrelic': 'eyJ2IjpbMCwxXSwiZCI6eyJ0eSI6IkJyb3dzZXIiLCJhYyI6Ijk2NzIzMiIsImFwIjoiMTU4ODYzMjc5MiIsImlkIjoiMTU5ZDAyYjQ0YTdjN2VlZiIsInRyIjoiNTEzYjdiODMwNGFkODE4N2Y5YzMwMzg0OTc4YWYwOWUiLCJ0aSI6MTcyMzQyNTAxNzYyOH19',
            'priority': 'u=1, i',
            'referer': 'https://www.shutterstock.com/zh/catalog/licenses',
            'sec-ch-ua': '"Not)A;Brand";v="99", "Google Chrome";v="127", "Chromium";v="127"',
            'sec-ch-ua-mobile': '?0',
            'sec-ch-ua-platform': '"Windows"',
            'sec-fetch-dest': 'empty',
            'sec-fetch-mode': 'cors',
            'sec-fetch-site': 'same-origin',
            'traceparent': '00-513b7b8304ad8187f9c30384978af09e-159d02b44a7c7eef-01',
            'tracestate': '967232@nr=0-1-967232-1588632792-159d02b44a7c7eef----1723425017628',
            'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/127.0.0.0 Safari/537.36',
            'x-end-app-name': 'next-web',
            'x-end-app-version': '2d4faa42e64',
            'x-newrelic-id': 'XQAAU1VRGwIEVVhaBgYGUlI=',
            'x-request-id': 'db700cde-0361-46f8-8ff3-d7234616ab34',
        }

    def random_ua(self):
        first_num = random.randint(55, 62)
        third_num = random.randint(0, 3200)
        fourth_num = random.randint(0, 140)
        os_type = [
            '(Windows NT 6.1; WOW64)',
            '(Windows NT 10.0; WOW64)',
            '(X11; Linux x86_64)',
            '(Macintosh; Intel Mac OS X 10_12_6)'
        ]
        chrome_version = 'Chrome/{}.0.{}.{}'.format(first_num, third_num, fourth_num)
        ua = ' '.join(['Mozilla/5.0', random.choice(os_type), 'AppleWebKit/537.36',
                       '(KHTML, like Gecko)', chrome_version, 'Safari/537.36']
                      )
        self.headers['user-agent'] = ua

    def transmission_api(self, data):
        # url = 'http://192.168.2.97:6661/microservice-visual/visual/fileSystem/saveImageDetail?token=dacce869-0471-4ec7-ac50-3b3b1ec22c87'
        url = 'http://wx.yswg.com.cn:8000/microservice-visual/visual/fileSystem/saveImageDetail?token=dacce869-0471-4ec7-ac50-3b3b1ec22c87'
        data_json = json.dumps(data)
        max_retries = 3
        retries = 0
        while retries <= max_retries:
            try:
                response = requests.post(url, data=data_json)
                if response.status_code == 200:
                    return response.json()
                else:
                    print(f'请求失败，状态码: {response.status_code}，重试 ({retries}/{max_retries})')
                    retries += 1
            except requests.exceptions.RequestException as e:
                print(f'请求异常: {e}，重试 ({retries}/{max_retries})')
                retries += 1
        raise Exception(f'请求失败，已达到最大重试次数：{max_retries} 次')


    def get_pic(self,account_id, image_id, item_id, cookie_list,retry=0):
        while retry <3:
            try:
                start_time = datetime.now().strftime("%m-%d %H:%M:%S")
                start_times = datetime.now()
                cookie = random.choice(cookie_list)

                self.random_ua()
                item = {}
                transmission_data = {}
                # 这里有两种不同的请求 需要判断
                headers = {
                    'accept': 'application/json',
                    'accept-language': 'zh-CN,zh;q=0.9',
                    'if-modified-since': 'Sat, 10 Aug 2024 08:09:24 GMT',
                    'if-none-match': 'W/"17t8cdyfo1s1un"',
                    'newrelic': 'eyJ2IjpbMCwxXSwiZCI6eyJ0eSI6IkJyb3dzZXIiLCJhYyI6Ijk2NzIzMiIsImFwIjoiMTU4ODYzMjc5MiIsImlkIjoiMTU5ZDAyYjQ0YTdjN2VlZiIsInRyIjoiNTEzYjdiODMwNGFkODE4N2Y5YzMwMzg0OTc4YWYwOWUiLCJ0aSI6MTcyMzQyNTAxNzYyOH19',
                    'priority': 'u=1, i',
                    'referer': 'https://www.shutterstock.com/zh/catalog/licenses',
                    'sec-ch-ua': '"Not)A;Brand";v="99", "Google Chrome";v="127", "Chromium";v="127"',
                    'sec-ch-ua-mobile': '?0',
                    'sec-ch-ua-platform': '"Windows"',
                    'sec-fetch-dest': 'empty',
                    'sec-fetch-mode': 'cors',
                    'sec-fetch-site': 'same-origin',
                    'traceparent': '00-513b7b8304ad8187f9c30384978af09e-159d02b44a7c7eef-01',
                    'tracestate': '967232@nr=0-1-967232-1588632792-159d02b44a7c7eef----1723425017628',
                    'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/127.0.0.0 Safari/537.36',
                    'x-end-app-name': 'next-web',
                    'x-end-app-version': '2d4faa42e64',
                    'x-newrelic-id': 'XQAAU1VRGwIEVVhaBgYGUlI=',
                    'x-request-id': 'db700cde-0361-46f8-8ff3-d7234616ab34',
                }
                params = {
                    'fields[images]': 'alt,aspect,channels,content_tier,contributor_id,description,displays,height,image_type,is_editorial,creativeInsights,link,model_release_info,sizes,src,title,width,has_model_release,has_property_release,status',
                    'fields[licenses]': 'content_id,content_size,content_type,global_accounts_user_id,insert_time,license_name,media-item,metadata',
                    'include': 'media-item.categories',
                    'filter[media_type]': 'photo',
                    'filter[licensee_type]': 'all',
                    'page[number]': '1',
                    'page[size]': '50',
                    'filter[media_id]': f'{image_id}',
                    'sort': 'newest',
                    'v': '2d4faa42e64',
                }
                response = requests.get('https://www.shutterstock.com/napi/user/licenses', params=params, cookies=cookie,headers=headers, timeout=600)
                if response.status_code == 200:
                    # 获取title size
                    try:
                        data = json.loads(response.text)
                        image_title = data['included'][0]['attributes']['alt']
                        image_size_info = {}
                        try:
                            hugeJpg = data['included'][0]['attributes']['sizes']['hugeJpg']
                            image_size_info['hugeJpg'] = hugeJpg
                        except:
                            hugeJpg = None

                        try:
                            mediumJpg = data['included'][0]['attributes']['sizes']['mediumJpg']
                            image_size_info['mediumJpg'] = mediumJpg
                        except:
                            mediumJpg = None

                        try:
                            smallJpg = data['included'][0]['attributes']['sizes']['smallJpg']
                            image_size_info['smallJpg'] = smallJpg
                        except:
                            smallJpg = None
                        image_size_info = json.dumps(image_size_info)
                    except:
                        # 第一个请求获取title失败后尝试第二个
                        params = {
                            'recordActivity': 'false',
                            'fields[images]': 'description,keywords,sizes',
                            'include': 'contributor',
                            'v': '76c84077d03',
                        }
                        response = requests.get(f'https://www.shutterstock.com/napi/images/{image_id}', params=params, cookies=cookie,headers=headers)
                        if response.status_code == 200:
                        # 获取title size
                            try:
                                data = json.loads(response.text)
                                image_title = data['data']['attributes']['description']
                                image_size_info = {}
                                try:
                                    largeJpg = data['data']['attributes']['sizes']['largePng']
                                    image_size_info['largeJpg'] = largeJpg
                                except:
                                    largeJpg = None
                                try:
                                    largePsd = data['data']['attributes']['sizes']['largePsd']
                                    image_size_info['largePsd'] = largePsd
                                except:
                                    largePsd = None
                                image_size_info = json.dumps(image_size_info)
                            except:
                                image_size_info = None
                                image_title = None
                    # 获取图片url
                    if image_title:
                        try:
                            if '@' in account_id:
                                file_name = account_id.split('@')[0]
                            else:
                                file_name = account_id
                            save_folder = f'/home/wangjing/picture_material/stock_summery/all_pic/{file_name}'
                            # save_folder= f'D:\公司备用文件\\all_images\\{file_name}'
                            pic_name = f'{str(image_id)}+{image_title}'.replace(':', '_').replace('\t', '').replace('\r', '').replace('|','').replace( '/', '').replace('"', "'").replace(' ', '_').replace(',', '_').replace('.', '_').replace('\n', '_')
                            pic_name = re.sub(r'[\\/*?:"<>|]', '_', pic_name)[:160] + '.jpg'
                            json_data = {
                                'required_cookies': '',
                                'content': [
                                    {
                                        'content_id': f'{image_id}',
                                        'content_type': 'photo',
                                        'content_size': 'huge',
                                        'content_format': 'jpg',
                                        'license_name': 'standard',
                                        'show_modal': True,
                                    },
                                ],
                            }
                            response = requests.post('https://www.shutterstock.com/napi/licensees/current/redownload',cookies=cookie, headers=self.headers,json=json_data,timeout =600)
                            image_url = json.loads(response.text)['meta']['licensedContent'][0]['downloadUrl']
                            image_type = 'jpg'
                            res = requests.get(image_url,timeout=600)
                            # 构建完整的文件路径
                            file_path = os.path.join(save_folder, pic_name)
                            # with open(file_path, "wb") as f:
                            #     f.write(res.content)
                            item['account_id'] = account_id
                            item['image_id'] = image_id
                            item['image_size_info'] = image_size_info
                            item['image_title'] = image_title
                            item['image_type'] = image_type
                            item['image_url'] = image_url
                            item['created_time'] = datetime.now().strftime('%Y-%m-%d %H:%M:%S')

                            transmission_data['accountId'] = account_id
                            transmission_data['imageId'] = image_id
                            transmission_data['imageSizeInfo'] = image_size_info
                            transmission_data['imageTitle'] = image_title
                            transmission_data['imageType'] = image_type
                            transmission_data['imageUrl'] = image_url

                            # Con.save_stock_detail(item)
                            # Con.update_image_id_to_3(item_id)
                            self.transmission_api(transmission_data)

                            now_time = datetime.now().strftime("%m-%d %H:%M:%S")
                            now_times = datetime.now()
                            print(f'pic_name:{pic_name[:38]},time:{start_time}——{now_time}下载成功')
                            time_difference = (now_times - start_times).total_seconds()
                            # 检查时间差是否小于5秒
                            if time_difference < 4.8:
                                # 随机等待2到5秒
                                wait_time = random.uniform(2, 5)
                                time.sleep(wait_time)
                            break

                        except Exception as e:
                            print('不是jpg', e)
                            if 'meta' in str(e):
                                try:
                                    # 如果 jpg 格式失败，则尝试 png 格式
                                    pic_name = f'{str(image_id)}+{image_title}'.replace(':', '_').replace('\t', '').replace('\r', '').replace('|', '').replace('/', '').replace('"', "'").replace(' ', '_').replace(',', '_').replace('.', '_').replace('\n', '_')
                                    pic_name = re.sub(r'[\\/*?:"<>|]', '_', pic_name)[:160] + '.png'
                                    json_data = {
                                        'required_cookies': '',
                                        'content': [
                                            {
                                                'content_id': f'{image_id}',
                                                'content_type': 'photo',
                                                'content_size': 'large',
                                                'content_format': 'png',
                                                'include_shadows': True,
                                                'angle': 'G03',
                                                'license_name': 'standard',
                                                'show_modal': True,
                                            },
                                        ],
                                    }
                                    response = requests.post('https://www.shutterstock.com/napi/licensees/current/redownload',cookies=cookie,headers=self.headers,json=json_data,timeout=600)
                                    image_url = json.loads(response.text)['meta']['licensedContent'][0]['downloadUrl']
                                    image_type = 'png'
                                    res = requests.get(image_url,timeout=600)

                                    # 构建完整的文件路径
                                    file_path = os.path.join(save_folder, pic_name)
                                    # with open(file_path, "wb") as f:
                                    #     f.write(res.content)
                                    item['account_id'] = account_id
                                    item['image_id'] = image_id
                                    item['image_size_info'] = image_size_info
                                    item['image_title'] = image_title
                                    item['image_type'] = image_type
                                    item['image_url'] = image_url
                                    item['created_time'] = datetime.now().strftime('%Y-%m-%d %H:%M:%S')

                                    transmission_data['accountId'] = account_id
                                    transmission_data['imageId'] = image_id
                                    transmission_data['imageSizeInfo'] = image_size_info
                                    transmission_data['imageTitle'] = image_title
                                    transmission_data['imageType'] = image_type
                                    transmission_data['imageUrl'] = image_url

                                    # Con.save_stock_detail(item)
                                    # Con.update_image_id_to_3(item_id)
                                    self.transmission_api(transmission_data)

                                    now_time = datetime.now().strftime("%m-%d %H:%M:%S")
                                    now_times = datetime.now()
                                    print(f'pic_name:{pic_name[:38]},time:{start_time}——{now_time}下载成功')
                                    time_difference = (now_times - start_times).total_seconds()
                                    # 检查时间差是否小于5秒
                                    if time_difference < 4.8:
                                        # 随机等待2到5秒
                                        wait_time = random.uniform(2, 5)
                                        time.sleep(wait_time)
                                    break
                                except Exception as e:
                                    print('不是png', e)
                                    raise
                            else:
                                raise
                    else:
                        Con.update_image_id_to_4(item_id)

                elif response.status_code == 403 and retry < 3:
                    print(f'重试第{retry + 1}次')
                    # 根据重试次数设置不同的休眠时间
                    if retry == 0:
                        sleep_time = random.randint(3, 6)
                    elif retry == 1:
                        sleep_time = random.randint(63, 66)
                    elif retry == 2:
                        sleep_time = random.randint(360, 366)
                    time.sleep(sleep_time)
                    del cookie
                    print(f'cookie还剩{len(cookie_list)}个')
                    cookie = random.choice(cookie_list)
                    return self.get_pic(account_id, image_id, item_id, cookie, retry + 1)
                else:
                    raise
                    print(image_id, '状态码=', response.status_code)

            except Exception as e:
                if 'image_title' in str(e):
                    Con.update_image_id_to_4(item_id)
                    print(f'{image_id}过期 修改为4')
                    break
                else:
                    print(f"网络错误 {e}, 尝试重新连接第{retry}次...")
                    # 设置不同的休眠时间
                    sleep_time = [random.randint(3, 6), random.randint(63, 66), random.randint(360, 366)][retry]
                    time.sleep(sleep_time)
                    return self.get_pic(account_id, image_id, item_id, cookie_list, retry + 1)
        if retry >= 3:
            print('重试次数用完')
            raise


    def run(self):
        for item_id in range(1, 2):
            account_id = Con.get_account_id(item_id)
            # 获取图片id
            image_id_id_pairs = Con.get_stock_images_id(account_id)
            counts_start = 0
            counts_last = len(image_id_id_pairs)
            cookie_list = Con.get_stock_cookie(account_id)

            for count in range(counts_start, counts_last):
                image_id = image_id_id_pairs[count].split('||-||')[0]
                item_id = image_id_id_pairs[count].split('||-||')[1]

                print(f'执行{account_id}', image_id, item_id, count)
                self.get_pic(account_id, image_id, item_id, cookie_list)
                count += 1
                # break
                if count == len(image_id_id_pairs):
                    print(f'{account_id}全部下载完成')
                    break

if __name__ == '__main__':
    stock_detail = Stock_Detail()
    stock_detail.run()


