download-images.mjs 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. // #!/usr/bin/env node
  2. // import { readFileSync, createWriteStream, existsSync, writeFileSync } from 'node:fs'
  3. // import { join, dirname } from 'node:path'
  4. // import request, { head } from 'request'
  5. // import { fileURLToPath } from 'node:url';
  6. // const __dirname = dirname(fileURLToPath(import.meta.url))
  7. // const filePath = join(__dirname, '..', 'preview', 'pages', '_data', 'photos.json')
  8. // const photos = JSON.parse(readFileSync(filePath, 'utf8'))
  9. // const urlTitle = (str) => {
  10. // str = str
  11. // .toLowerCase()
  12. // .replaceAll('&', 'and')
  13. // .replace(/[^[a-z0-9-]/g, '-')
  14. // .replace(/-+/g, '-')
  15. // return str
  16. // }
  17. // const download = function (uri, filename, callback, error) {
  18. // head(uri, function (err, res, body) {
  19. // request(uri).pipe(createWriteStream(filename))
  20. // .on('close', callback)
  21. // .on('error', error)
  22. // })
  23. // }
  24. // async function downloadPhotos() {
  25. // for (const key in photos) {
  26. // const photo = photos[key]
  27. // let filename, i = 1;
  28. // do {
  29. // filename = `${urlTitle(photo['title'])}${i > 1 ? `-${i}` : ''}.jpg`
  30. // i++
  31. // } while (existsSync(join(__dirname, `../preview/static/photos/${filename}`)))
  32. // await new Promise((resolve, reject) => {
  33. // download(photo['path'], join(__dirname, `../preview/static/photos/${filename}`), function () {
  34. // resolve()
  35. // }, function () {
  36. // reject()
  37. // });
  38. // })
  39. // photos[key]['file'] = filename
  40. // photos[key]['horizontal'] = photo['width'] > photo['height']
  41. // }
  42. // writeFileSync(filePath, JSON.stringify(photos))
  43. // }
  44. // downloadPhotos();