const fetch = require('node-fetch'); // Node.js 18以下版本需要安装 node-fetch
const tools = require('../../../tools');
const config  = require('../../../etc/config.json');
const redis_help = require('../../use_redis');
const HttpClient = require('../../HttpClient');
const helper = require('../../helper');
const CMD = {}

CMD.get_book_tg_chapter_id = async function(book_id) {
    try{
        let OPENSESSID = await redis_help.getKeyValue("OPENSESSID")
        // let client = tools.getOneNewClinet()
        // let postData = {
        //     cmd:"chapterSpread",
        //     cbid:book_id,
        //     OPENSESSID:OPENSESSID
            
        // }
        // let response = await client.post(config.python_config.host, postData)
        // console.log("response.data.received_data:",response.data.received_data)
        // response = response.data.received_data
     
        await require('./getActiveInfo').getActiveInfo()
        let timestamp = helper.getCurrentUnixTimestamp()
        let url = `https://open.yuewen.com/api/wechatspread/chapterSpread?cbid=${book_id}&page=1&version=2&order_field=allwords&order_type=0&content_type=0&category2=-1&isfinish=-1`
        const response =   await fetch(url, {
            "headers": {
              "accept": "application/json, text/plain, */*",
              "accept-language": "zh-CN,zh;q=0.9,en;q=0.8,en-GB;q=0.7,en-US;q=0.6",
              "priority": "u=1, i",
              "sec-ch-ua": "\"Microsoft Edge\";v=\"131\", \"Chromium\";v=\"131\", \"Not_A Brand\";v=\"24\"",
              "sec-ch-ua-mobile": "?0",
              "sec-ch-ua-platform": "\"Windows\"",
              "sec-fetch-dest": "empty",
              "sec-fetch-mode": "cors",
              "sec-fetch-site": "same-origin",
              "Content-Type" : "application/json; charset=utf-8",
              "cookie": `Hm_lvt_990f9ab9737a266517417cc2949bb3f4=${timestamp}; csrfToken=ffZRRnqo6A4ngGbvAVP_zK5_; OPENSESSID=${OPENSESSID}; yw_open_token=67db7bcd069db; is_read_notice=67db7bcd069db; sidebarStatus=0`,
              "Referer": "https://open.yuewen.com/new/library",
              "Referrer-Policy": "strict-origin-when-cross-origin"
            },
            "body": null,
            "method": "GET"
          });
    
          const data =  await response.json();
          if(data.code!=0){
              throw data
          }
          console.log('get_book_tg_chapter_id::响应状态:', response.status);
          console.log('get_book_tg_chapter_id::响应数据:', data);
          return data.data.chapterList[0];
    }catch(error){
        console.error('get_book_tg_chapter_id::请求错误:', error);
        return null;
    }

}

module.exports = CMD;