from curl_cffi import requests
import json


class dow_shutterstock_img():
    def __init__(self):
        self.Cookie = 'n_v=35c0dc14612; locale=zh; NEXT_LOCALE=zh; stck_anonymous_id=472cdbf8-94ab-4400-bc2e-1282144cd384; sstk_anonymous_id=472cdbf8-94ab-4400-bc2e-1282144cd384; ssnext=true; hl=zh; sstk_session_start=2024-07-25T01%3A18%3A40.151Z; stck_session_id=0448368f-52a7-4e28-a347-ab0f05ac3aaa; sstk_session_id=0448368f-52a7-4e28-a347-ab0f05ac3aaa; visit_id=81138438424; visitor_id=73374677017; htjs_anonymous_id=472cdbf8-94ab-4400-bc2e-1282144cd384; OptanonCachedGroups=,C0001,C0003,C0002,C0005,C0004,C0007,; gtm_monit_roll=35; _gcl_au=1.1.2007568107.1721870326; _ga=GA1.1.167863445.1721870327; __rtbh.lid=%7B%22eventType%22%3A%22lid%22%2C%22id%22%3A%225zW82mi1vrTw1mhmBI1x%22%7D; FPID=FPID2.2.jbbGJsFa%2F4QZTUcoZS3oBakRd4Dy31w%2BcrLWua3ga58%3D.1721870327; FPLC=rZnqDf%2Bn%2BS52gDBS2ipJg%2BAWydfKAh%2B9delXhynY3fwgGwb1bpQqEorUA7kHpdBKH5bONHU5lKe2ZUWSO2vga3C5IRiisVuQEKJf2G7LQX5PDSEpmwe3aduB%2FPp4tA%3D%3D; __ssid=9124e091e2c0f8a255d533447d3edf8; slireg=https://scout.us1.salesloft.com; sliguid=3b796208-9a66-40cc-91c6-9144ef0f91d0; slirequested=true; _4c_=%7B%22_4c_mc_%22%3A%22af3661e3-cfe6-459a-bc7a-efefde831641%22%7D; extole_access_token=CQ6KFSOGH1TV2OF5Q09VSV4DNO; did=50c367d8-bf47-462d-b543-ff94bda0d3f7; accts_customer=daiting55; accts_customer_sso1=331232635-undefined; next.sid=s%3A4Hs9k_fNrFzMq_FEPYNsShMtQWMbkjMT.EMdAfR%2F4H6hJNs662VC98Nn69ekJZV9yYwam00S5frk; Authorization=1%2FeyJjbGllbnRfaWQiOiJhMGI3Ni1hN2Y1ZS1mZWRlYy1iYmViZS1mYTY1Yi04NTcxOSIsInJlYWxtIjoiY3VzdG9tZXIiLCJzY29wZSI6InVzZXIudmlldyB1c2VyLmVtYWlsIHVzZXIuYWRkcmVzcyB1c2VyLmVkaXQgb3JnYW5pemF0aW9uLnZpZXcgb3JnYW5pemF0aW9uLmFkZHJlc3MgY29sbGVjdGlvbnMudmlldyBjb2xsZWN0aW9ucy5lZGl0IGxpY2Vuc2VzLnZpZXcgbGljZW5zZXMuY3JlYXRlIG1lZGlhLnVwbG9hZCBtZWRpYS5zdWJtaXQgbWVkaWEuZWRpdCBwdXJjaGFzZXMudmlldyBwdXJjaGFzZXMuY3JlYXRlIiwidXR2IjoiOGJSVSIsImVtYWlsX3ZlcmlmaWVkIjp0cnVlLCJ1c2VybmFtZSI6ImRhaXRpbmc1NSIsInVzZXJfaWQiOjMzMTIzMjYzNSwib3JnYW5pemF0aW9uX2lkIjpudWxsLCJwYXJlbnRfb3JnYW5pemF0aW9uX2lkcyI6W10sImN1c3RvbWVyX2lkIjozMzEyMzI2MzUsImV4cCI6MTcyMTg3NDYxM30.GTyqg0nXBm4LFrxY9OsIOQnVKAAi-XjO4XzTP8f3hGTI-IYqI6csxzbCzrwjhUpVaYfjqBe2xRn6f35NRzoZDg; htjs_user_id=331232635; __rtbh.uid=%7B%22eventType%22%3A%22uid%22%2C%22id%22%3A%22472cdbf8-94ab-4400-bc2e-1282144cd384%22%7D; search-component-cookie=pagination; downlink=mid; OptanonConsent=isGpcEnabled=0&datestamp=Thu+Jul+25+2024+09%3A44%3A43+GMT%2B0800+(%E4%B8%AD%E5%9B%BD%E6%A0%87%E5%87%86%E6%97%B6%E9%97%B4)&version=202403.2.0&browserGpcFlag=0&isIABGlobal=false&hosts=&consentId=f9ee0de2-d442-4486-a0b8-fc7418338d88&interactionCount=1&isAnonUser=1&landingPath=NotLandingPage&groups=C0001%3A1%2CC0003%3A1%2CC0002%3A1%2CC0005%3A1%2CC0004%3A1%2CC0007%3A1&AwaitingReconsent=false; _ga_5JRYE4Y8J9=GS1.1.1721870327.1.1.1721871885.60.0.0; _ga_SSGTMSSTK=GS1.1.1721870327.1.1.1721871885.0.0.593396081; _uetsid=d96bba304a2311ef91b8b97ed1dee9b6|1atmgbl|2|fnr|0|1667; _uetvid=d96bbea04a2311ef898be93aba9723f6|tngh3d|1721871886172|7|1|bat.bing.com/p/insights/c/l; datadome=7tnXqyo18T3wrY9BkHxkVF_5Bk39NamIoV9SzIO~iHwDJU2fnbgpuH65b2FEeMW~kI3is7vvxi~zSIG_fIL~9JG_d9Hge9StDOHiRJtzre~8TZpTB0j7tMUg~ZM2c7LH',

    def get_imgid_data_index(self):
        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.7',
            'Accept-Encoding': 'gzip, deflate, br, zstd',
            'Accept-Language': 'zh-CN,zh;q=0.9',
            'Cache-Control': 'no-cache',
            'Cookie': self.Cookie,
            'Pragma': 'no-cache',
            'Sec-Ch-Ua': '"Not_A Brand";v="8", "Chromium";v="120", "Google Chrome";v="120"',
            'Sec-Ch-Ua-Mobile': '?0',
            'Sec-Ch-Ua-Platform': '"Windows"',
            'Sec-Fetch-Dest': 'document',
            'Sec-Fetch-Mode': 'navigate',
            'Sec-Fetch-Site': 'none',
            'Sec-Fetch-User': '?1',
            'Upgrade-Insecure-Requests': '1',
            'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36'

        }
        url = 'https://www.shutterstock.com/api/s/dam/holdings/search?include=media-item%2Cmedia-item.track-assets&sort=-licensedAt&useMms=True&channel=shutterstock&page[size]=50&page[number]=1&filter[assetStatus]=comped%2Clicensed&language=zh'

        resp = requests.get(url, headers=headers)
        # resp_dict = resp.json()
        # totalPages = resp_dict['meta']['pagination']['totalPages']  # 总页数
        # totalRecords = resp_dict['meta']['pagination']['totalRecords']  # 总图片数量
        # size = resp_dict['meta']['pagination']['size']  # 一页返回的图片数量
        print(resp.text)
        # get_imgid_data(int(totalPages))

    def get_imgid_data(self, totalPages):
        for i in range(2, totalPages + 1):
            next_page_url = f'https://www.shutterstock.com/api/s/dam/holdings/search?include=media-item%2Cmedia-item.track-assets&sort=-licensedAt&useMms=True&channel=shutterstock&page[size]=50&page[number]={i}&filter[assetStatus]=comped%2Clicensed&language=zh'
            print(next_page_url)

    def dow_image_eps(self):
        headers = {
            "Host": "www.shutterstock.com",
            'Cookie': self.Cookie,
            "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/126.0.0.0 Safari/537.36",
            "Content-Type": "application/json",
            "Accept": "application/json",
            "Origin": "https://www.shutterstock.com",
            "Referer": "https://www.shutterstock.com/zh/catalog/licenses",
            "Accept-Language": "zh-CN,zh;q=0.9",

        }

        url = "https://www.shutterstock.com/napi/licensees/current/redownload"
        params = {
            "v": "35c0dc14612"
        }
        data = {"required_cookies": "", "content": [
            {"content_id": content_id, "content_type": "photo", "content_size": "vector", "content_format": "eps",
             "license_name": "standard", "show_modal": True}]}
        data = json.dumps(data, separators=(',', ':'))
        response = requests.post(url, headers=headers, params=params, data=data, verify=False)
        print(response.json())
        meta_items = response.json()
        downloadUrl = meta_items['meta']['licensedContent'][0]['downloadUrl']
        resp = requests.get(downloadUrl)
        with open(rf'C:\Users\ASUS\Downloads\{content_id}.eps', 'wb')as f:
            f.write(resp.content)

        # 返回下载链接
        # {"meta":{"licensedContent":[{"licenseName":"standard","contentFormat":"eps","contentId":"2254280157","contentSize":"vector","contentType":"photo","downloadUrl":"https://download.shutterstock.com/gatekeeper/W3siZCI6ICJzaHV0dGVyc3RvY2stbWVkaWEiLCAiayI6ICJwaG90by8yMjU0MjgwMTU3L3ZlY3Rvci5lcHMiLCAiZGMiOiAiaWRsXzEyMyIsICJlIjogMTcyMjM1NDcwMywgIm0iOiAxfSwgImQzTzh6QlFzNDNiaHlEcmFRcUNJek5lRUNjRSJd/shutterstock_2254280157.eps","isRedownload":true,"contentLicenseId":"e1f8df6ea1080f070e19283dcb6f1cc97b","downloadId":"deprecated"}]}}

    def select_totalRecords(self):
        """
        查询 上一次下载总数。当前总数减去上一次图片总数 除 50 得到需要下载的总页数。
        """


if __name__ == '__main__':
    # get_imgid_data(302)
    dow_shutterstock_img().get_imgid_data_index()
