import scrapy
import json


class YutianTopSpider(scrapy.Spider):
    name = 'yutian_top_resume'
    allowed_domains = ['yutian.top']
    start_urls = ['https://www.yutian.top/job/company/v1/resume/page']

    def start_requests(self):
        headers = {
            'accept': 'application/json, text/plain, */*',
            'accept-language': 'zh-CN,zh;q=0.9',
            'cache-control': 'no-cache',
            'content-type': 'application/json;charset=UTF-8',
            'origin': 'https://www.yutian.top',
            'pragma': 'no-cache',
            'priority': 'u=1, i',
            'referer': 'https://www.yutian.top/enterprise/resume_store/list',
            'sec-ch-ua': '"Google Chrome";v="135", "Not-A.Brand";v="8", "Chromium";v="135"',
            'sec-ch-ua-mobile': '?0',
            'sec-ch-ua-platform': '"Windows"',
            'sec-fetch-dest': 'empty',
            'sec-fetch-mode': 'cors',
            'sec-fetch-site': 'same-origin',
            'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/135.0.0.0 Safari/537.36',
        }

        cookies = {
            'company_sign': '',
            'company_nonce': '',
            'cuid': '',
            'PHPSESSID': '210b19c9d51dbf8eec8e8ffb0540ad33',
            'auth-token': 'eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJleHAiOjE3NDY4MTIxNTksImp0aSI6IjgwZGVjMzY4LWUwODktNGYxYS1hNWJjLWExNDMzMDYzMjdmYiIsIm5hbWUiOiIxODYxNzg3MjE4NSIsInVzZXJfaWQiOiIwM2M2MmI5ODM4Yjk3Y2UzYmQxZTQwNDllZGVlNmI0OCIsInRlbmFudF90b2tlbiI6IjY1OTAxM2RlNjAxZmJmNjg1MzZmYTU0OTc4ODVkMTA2In0.0rXFe1iQClJ33rgXnTjhmye3zqVEZkJQvHGGET9dsz0',
        }

        for i in range(1,6):

            payload = {
                'step': 1000,
                'page': i,
                'education_level': [],
                'arrival_time': [],
                'work_time': [],
                'area_id': [],
                'keywords': '',
                'work_status': '',
                'work_status_show': '求职状态',
                'category_id': '',
                'work_type': '',
                'work_type_show': '是否兼职',
                'sex': '',
                'sex_show': '性别',
                'is_head': '',
                'is_head_show': '有无照片',
                'job_id': '',
                'age': [],
                'age_show': '年龄',
                'refresh_time': 0,
                'site_id': '',
                'site_id2': '',
                'province': '',
                'city': '',
                'county': '',
                'provinceArr': [],
                'cityArr': [],
                'countyArr': [],
                'only_job_category': 0,
            }

            yield scrapy.Request(
                url=self.start_urls[0],
                method='POST',
                headers=headers,
                cookies=cookies,
                body=json.dumps(payload),
                callback=self.parse,
            )

    def parse(self, response):
        status_code = response.status
        print(status_code)
        data = json.loads(response.text)
        for item in data.get('data', []):
            yield item