Last active
October 19, 2021 17:30
-
-
Save Christopher-Hayes/1f24385709c257efca3353a4b79cd09e to your computer and use it in GitHub Desktop.
All Copilot - Download images from a list of <a> links
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Parse <a> tag into the link and the text | |
function parseLink(str) { | |
var m = str.match(/<a\s+href="([^"]+)"[^>]*>([^<]+)<\/a>/i); | |
if (m) { | |
return { | |
href: m[1], | |
text: m[2] | |
}; | |
} | |
return null; | |
} | |
// Node.js read a text file one line at a time | |
// and pass the line to the callback function | |
function readFile(file, callback) { | |
var lineReader = require('readline').createInterface({ | |
input: require('fs').createReadStream(file) | |
}); | |
lineReader.on('line', function (line) { | |
callback(line); | |
}); | |
} | |
// Node.js download an image from the web from the url and set the filename to the passed in title | |
function downloadImage(url, title, callback) { | |
var request = require('request'); | |
var fs = require('fs'); | |
// Remove URL params from URL | |
url = url.split('?')[0] | |
// Get the file extensions from the URL | |
var ext = url.substring(url.lastIndexOf('.') + 1); | |
var filename = `${title}.${ext}`; | |
// Remove characters from filename that are not allowed on Mac OS X | |
filename = filename.replace(/[^a-zA-Z0-9_\-\.]/g, '_'); | |
request(url).pipe(fs.createWriteStream(`output/${filename}`)).on('close', function () { | |
callback(); | |
}); | |
} | |
readFile(process.argv[2], function (line) { | |
var link = parseLink(line); | |
if (link) { | |
downloadImage(link.href, link.text, () => { | |
console.log(link.text); | |
}) | |
} | |
}) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
GitHub Copilot is amazing. This code is almost entirely written by CoPilot. I pretty much only wrote the comments. All code pulled in from CoPilot worked without bugs.
Example usage:
node.js downloadLinks.js ./input.txt
input.txt