import json
import scrapy
import time
import urllib
from datetime import datetime
from scrapy.http import Response

class QJ050ComSpider(scrapy.Spider):
    name = 'qj050_com'
    allowed_domains = ['qj050.com']
    start_urls = ['https://www.qj050.com/api/v1/resumes']

    headers = {
        'accept': 'application/json, text/plain, */*',
        'accept-language': 'zh-CN,zh;q=0.9,en;q=0.8',
        'authorization': 'Bearer 你的token',
        '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',
        'x-platform': '1',
        'x-site-id': 'undefined',
    }
    cookies = {
        'x-trace-id': '7d60110f6a7a4df595db14e54ee772dd',
        'has_login_log': 'yes',
        'HMACCOUNT': '52014CC932A93E9B',
        'token': 'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpZCI6NDgxNTMsInVzZXJuYW1lIjoi55yf6LSkODg4OCIsInB3ZCI6IjFiYmJjNzc5OGRkMTFiNTI2YWQ4ZTVmYTYyNWY5MjVkIiwiaWF0IjoxNzQ1NzU3MjM4LCJleHAiOjE3NzcyOTMyMzh9.uU9G81yizRRUYCyJymit4n9vuysXCT-2V9PLdmdohgA',
        'token.sig': 'Zta83bKMN9mPlsm9ZVnv7PaA7MwJZrLYHYrQK4Ft1rY',
        'logged': '1',
    }

    def start_requests(self):
        for page in range(1, 5):
            params = {
                '_': str(int(time.time() * 1000)),
                'tab': 'resume',
                'pageSize': '1000',
                'pageIndex': str(page),
                'showStatus': 'true',
            }
            query_string = urllib.parse.urlencode(params)
            url = f"{self.start_urls[0]}?{query_string}"
            yield scrapy.Request(
                url=url,
                method='GET',
                headers=self.headers,
                cookies=self.cookies,
                callback=self.parse
            )

    def parse(self, response: Response, **kwargs):
        data = json.loads(response.text)
        for item in data['data']['items']:
            resume_id = item['id']
            detail_url = f"https://www.qj050.com/api/v1/resume/{resume_id}?_={int(time.time() * 1000)}&view_type=resumeLibrary&privacy_description=1"
            yield scrapy.Request(
                url=detail_url,
                method='GET',
                headers=self.headers,
                cookies=self.cookies,
                callback=self.parse_detail
            )

    def parse_detail(self, response):
        info = json.loads(response.text).get('data', {})

        data = {}

        # 常规字段
        data['resume_id'] = info.get('id')
        data['name'] = info.get('name') or None
        data['age'] = int(info.get('age')) if info.get('age') else None
        data['birthday'] = info.get('birthday') or None
        data['work_years'] = info.get('work_exp_value') or None
        data['highest_education'] = info.get('edu_value') or None
        data['marital_status'] = info.get('marriage_value') or None
        data['phone'] = info.get('phone') or None
        data['intended_position'] = ','.join(
            [item.get('name') for item in info.get('infoCateforyArrObj', [])]) if info.get(
            'infoCateforyArrObj') else None
        data['expected_salary'] = info.get('salaryDesc') or None
        data['job_property'] = info.get('work_type_value') or None
        data['job_status'] = info.get('job_instant_value') or None
        data['job_location'] = info.get('job_region_value') or None

        # 更新时间 (如果有 last_edit_time，要转 datetime)
        if 'last_edit_time' in info and info.get('last_edit_time'):
            try:
                data['update_time'] = datetime.strptime(info['last_edit_time'], "%Y-%m-%d %H:%M:%S")
            except Exception:
                data['update_time'] = None
        else:
            data['update_time'] = None

        # 工作经历处理
        works = info.get('works', [])
        for i in range(4):
            if i < len(works):
                company = works[i].get('company', '')
                content = works[i].get('content', '')
                combined = f"{company}:{content}" if company or content else ''
                data[f'work_{i + 1}_experience'] = combined or None
            else:
                data[f'work_{i + 1}_experience'] = None

            data[f'work_{i + 1}_time'] = None
            data[f'work_{i + 1}_description'] = None
        data['source_id'] = 4
        data['crawl_keywords'] = ''
        yield data
