!44 修复#星铁公告/资讯/活动报错puppeteer.browserInit is not a function,新增登录验证码自动获取ticket,更新依赖
Merge pull request !44 from touchscale/master
This commit is contained in:
		
						commit
						5abba8b80e
					
				|  | @ -73,13 +73,18 @@ export default class loginEvent extends EventListener { | ||||||
|         type: 'list', |         type: 'list', | ||||||
|         name: 'type', |         name: 'type', | ||||||
|         message: '触发滑动验证,需要获取ticket通过验证,请选择获取方式:', |         message: '触发滑动验证,需要获取ticket通过验证,请选择获取方式:', | ||||||
|         choices: ['1.手动获取ticket', '2.滑动验证app请求码获取'] |         choices: ['0.自动获取ticket', '1.手动获取ticket', '2.滑动验证app请求码获取'] | ||||||
|       } |       } | ||||||
|     ]) |     ]) | ||||||
| 
 | 
 | ||||||
|     await common.sleep(200) |     await common.sleep(200) | ||||||
|     let ticket |     let ticket | ||||||
| 
 | 
 | ||||||
|  |     if (ret.type == '0.自动获取ticket') { | ||||||
|  |       ticket = await this.getTicket(event.url) | ||||||
|  |       if (!ticket) console.log('\n请求错误,返回手动获取ticket方式\n') | ||||||
|  |     } | ||||||
|  | 
 | ||||||
|     if (ret.type == '2.滑动验证app请求码获取') { |     if (ret.type == '2.滑动验证app请求码获取') { | ||||||
|       ticket = await this.requestCode(event.url) |       ticket = await this.requestCode(event.url) | ||||||
|       if (!ticket) console.log('\n请求错误,返回手动获取ticket方式\n') |       if (!ticket) console.log('\n请求错误,返回手动获取ticket方式\n') | ||||||
|  | @ -103,6 +108,27 @@ export default class loginEvent extends EventListener { | ||||||
|     this.client.submitSlider(ticket.trim()) |     this.client.submitSlider(ticket.trim()) | ||||||
|   } |   } | ||||||
| 
 | 
 | ||||||
|  |    async getTicket (url) { | ||||||
|  |     let req = `https://hlhs-nb.cn/captcha/slider?key=${Bot.uin}` | ||||||
|  |     await fetch(req, { | ||||||
|  |       method: 'POST', | ||||||
|  |       body: JSON.stringify({ url }) | ||||||
|  |     }) | ||||||
|  | 
 | ||||||
|  |     console.log('\n----请打开下方链接并在2分钟内进行验证----') | ||||||
|  |     console.log(`${logger.green(req)}\n----完成后将自动进行登录----`) | ||||||
|  | 
 | ||||||
|  |     for (let i = 0; i < 40; i++) { | ||||||
|  |       let res = await fetch(req, { | ||||||
|  |         method: 'POST', | ||||||
|  |         body: JSON.stringify({ submit: Bot.uin }) | ||||||
|  |       }) | ||||||
|  |       res = await res.json() | ||||||
|  |       if (res.data?.ticket) return res.data.ticket | ||||||
|  |       await common.sleep(3000) | ||||||
|  |     } | ||||||
|  |   } | ||||||
|  | 
 | ||||||
|   async requestCode (url) { |   async requestCode (url) { | ||||||
|     let txhelper = { |     let txhelper = { | ||||||
|       url: url.replace('ssl.captcha.qq.com', 'txhelper.glitch.me') |       url: url.replace('ssl.captcha.qq.com', 'txhelper.glitch.me') | ||||||
|  |  | ||||||
|  | @ -33,7 +33,7 @@ | ||||||
|     "node-xlsx": "^0.21.2", |     "node-xlsx": "^0.21.2", | ||||||
|     "oicq": "^2.3.1", |     "oicq": "^2.3.1", | ||||||
|     "pm2": "^5.3.0", |     "pm2": "^5.3.0", | ||||||
|     "puppeteer": "^20.1.2", |     "puppeteer": "^20.2.1", | ||||||
|     "redis": "^4.6.6", |     "redis": "^4.6.6", | ||||||
|     "sequelize": "^6.31.1", |     "sequelize": "^6.31.1", | ||||||
|     "sqlite3": "^5.1.6", |     "sqlite3": "^5.1.6", | ||||||
|  |  | ||||||
|  | @ -44,86 +44,13 @@ export default class MysNews extends base { | ||||||
| 
 | 
 | ||||||
|     const param = await this.newsDetail(postId) |     const param = await this.newsDetail(postId) | ||||||
| 
 | 
 | ||||||
|     const img = await this.rander(param) |     const img = await this.render(param) | ||||||
| 
 | 
 | ||||||
|     return await this.replyMsg(img, `崩坏星穹铁道${typeName}:${param.data.post.subject}`) |     return await this.replyMsg(img, `崩坏星穹铁道${typeName}:${param.data.post.subject}`) | ||||||
|   } |   } | ||||||
| 
 | 
 | ||||||
|   async rander (param) { |   async render (param) { | ||||||
|     const pageHeight = 7000 |     return await puppeteer.screenshots(this.model, param) | ||||||
| 
 |  | ||||||
|     await puppeteer.browserInit() |  | ||||||
| 
 |  | ||||||
|     if (!puppeteer.browser) return false |  | ||||||
| 
 |  | ||||||
|     const savePath = puppeteer.dealTpl('mysNews', param) |  | ||||||
|     if (!savePath) return false |  | ||||||
| 
 |  | ||||||
|     const page = await puppeteer.browser.newPage() |  | ||||||
|     try { |  | ||||||
|       await page.goto(`file://${_path}${lodash.trim(savePath, '.')}`, { timeout: 120000 }) |  | ||||||
|       const body = await page.$('#container') || await page.$('body') |  | ||||||
|       const boundingBox = await body.boundingBox() |  | ||||||
| 
 |  | ||||||
|       const num = Math.round(boundingBox.height / pageHeight) || 1 |  | ||||||
| 
 |  | ||||||
|       if (num > 1) { |  | ||||||
|         await page.setViewport({ |  | ||||||
|           width: boundingBox.width, |  | ||||||
|           height: pageHeight + 100 |  | ||||||
|         }) |  | ||||||
|       } |  | ||||||
| 
 |  | ||||||
|       const img = [] |  | ||||||
|       for (let i = 1; i <= num; i++) { |  | ||||||
|         const randData = { |  | ||||||
|           type: 'jpeg', |  | ||||||
|           quality: 90 |  | ||||||
|         } |  | ||||||
| 
 |  | ||||||
|         if (i != 1 && i == num) { |  | ||||||
|           await page.setViewport({ |  | ||||||
|             width: boundingBox.width, |  | ||||||
|             height: parseInt(boundingBox.height) - pageHeight * (num - 1) |  | ||||||
|           }) |  | ||||||
|         } |  | ||||||
| 
 |  | ||||||
|         if (i != 1 && i <= num) { |  | ||||||
|           await page.evaluate(() => window.scrollBy(0, 7000)) |  | ||||||
|         } |  | ||||||
| 
 |  | ||||||
|         let buff |  | ||||||
|         if (num == 1) { |  | ||||||
|           buff = await body.screenshot(randData) |  | ||||||
|         } else { |  | ||||||
|           buff = await page.screenshot(randData) |  | ||||||
|         } |  | ||||||
| 
 |  | ||||||
|         if (num > 2) await common.sleep(200) |  | ||||||
| 
 |  | ||||||
|         puppeteer.renderNum++ |  | ||||||
|         /** 计算图片大小 */ |  | ||||||
|         const kb = (buff.length / 1024).toFixed(2) + 'kb' |  | ||||||
| 
 |  | ||||||
|         logger.mark(`[图片生成][${this.model}][${puppeteer.renderNum}次] ${kb}`) |  | ||||||
| 
 |  | ||||||
|         img.push(segment.image(buff)) |  | ||||||
|       } |  | ||||||
| 
 |  | ||||||
|       await page.close().catch((err) => logger.error(err)) |  | ||||||
| 
 |  | ||||||
|       if (num > 1) { |  | ||||||
|         logger.mark(`[图片生成][${this.model}] 处理完成`) |  | ||||||
|       } |  | ||||||
|       return img |  | ||||||
|     } catch (error) { |  | ||||||
|       logger.error(`图片生成失败:${this.model}:${error}`) |  | ||||||
|       /** 关闭浏览器 */ |  | ||||||
|       if (puppeteer.browser) { |  | ||||||
|         await puppeteer.browser.close().catch((err) => logger.error(err)) |  | ||||||
|       } |  | ||||||
|       puppeteer.browser = false |  | ||||||
|     } |  | ||||||
|   } |   } | ||||||
| 
 | 
 | ||||||
|   async newsDetail (postId) { |   async newsDetail (postId) { | ||||||
|  | @ -324,7 +251,7 @@ export default class MysNews extends base { | ||||||
|       logger.mark(`[崩坏星穹铁道${typeName}推送] ${param.data.post.subject}`) |       logger.mark(`[崩坏星穹铁道${typeName}推送] ${param.data.post.subject}`) | ||||||
| 
 | 
 | ||||||
|       this[postId] = { |       this[postId] = { | ||||||
|         img: await this.rander(param), |         img: await this.render(param), | ||||||
|         title: param.data.post.subject |         title: param.data.post.subject | ||||||
|       } |       } | ||||||
|     } |     } | ||||||
|  |  | ||||||
		Loading…
	
		Reference in New Issue