process-post-img.js 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. import fs from 'fs'
  2. import request from 'request'
  3. import { nanoid } from 'nanoid'
  4. let fileName = './data/user-SUNMENG333.json'
  5. let saveFileStr = fs.readFileSync(fileName, 'utf8')
  6. let inputData = JSON.parse(saveFileStr)
  7. let saveFilePath = './imgs/'
  8. const downloadImage = async (src, dest) => {
  9. console.log('下载:', src)
  10. return new Promise((resolve) => {
  11. request.head(src, (err) => {
  12. if (err) {
  13. console.log(err)
  14. return
  15. }
  16. src &&
  17. request(src)
  18. .pipe(fs.createWriteStream(dest))
  19. .on('close', () => {
  20. setTimeout(() => resolve(true), 1500)
  21. })
  22. })
  23. })
  24. }
  25. async function test3(list) {
  26. let url = list.pop()
  27. if (url.includes('http')) {
  28. let name = nanoid() + '.png'
  29. await downloadImage(url, saveFilePath + name)
  30. return [name]
  31. }
  32. return [url]
  33. // fs.writeFileSync(fileName, JSON.stringify(inputData, null, 2));
  34. }
  35. inputData = inputData.slice(0, 70)
  36. console.log(inputData.length)
  37. for (let i = 0; i < inputData.length; i++) {
  38. let v = inputData[i]
  39. console.log(`处理:第${i}个视频:`, v.desc)
  40. if (v.video?.play_addr?.url_list) {
  41. v.video.play_addr.url_list = [v.video.play_addr.url_list.pop()]
  42. }
  43. try {
  44. if (v.video?.cover?.url_list) {
  45. v.video.cover.url_list = await test3(v.video?.cover?.url_list)
  46. }
  47. if (v.video?.animated_cover) {
  48. delete v.video.animated_cover
  49. }
  50. fs.writeFileSync(fileName, JSON.stringify(inputData, null, 2))
  51. } catch (e) {
  52. console.log('出错了', e)
  53. }
  54. }