bing.js 906 B

1234567891011121314151617181920212223242526272829303132333435363738
  1. const https = require('https')
  2. const fs = require('fs')
  3. const options = {
  4. hostname: 'www.bing.com',
  5. port: 443,
  6. path: '/HPImageArchive.aspx?format=js&idx=0&n=8',
  7. method: 'GET'
  8. }
  9. const req = https.request(options, bing_res => {
  10. let bing_body = [], bing_data = {};
  11. bing_res.on('data', (chunk) => {
  12. bing_body.push(chunk);
  13. });
  14. bing_res.on('end', () => {
  15. bing_body = Buffer.concat(bing_body);
  16. bing_data = JSON.parse(bing_body.toString());
  17. let img_array = bing_data.images;
  18. let img_url = [];
  19. img_array.forEach(img => {
  20. img_url.push(img.url);
  21. });
  22. var jsonpStr = "getBingImages(" + JSON.stringify(img_url) + ")";
  23. fs.writeFile('./assets/json/images.json', jsonpStr, (err) => {
  24. if (err) {
  25. throw err;
  26. }
  27. console.log("JSON data is saved: " + jsonpStr);
  28. });
  29. });
  30. })
  31. req.on('error', error => {
  32. console.error(error)
  33. })
  34. req.end()