(Kind of) Simple Imageboard API Grabber

(Kind of) Simple Imageboard API Grabber

Intro

Hey everyone! New Bitburner player here, decided to (objectively mangle) some code out of

Sysroot's Cat Viewer Program! which I highly suggest you check out here.

Code

First off you're gonna want to make a quick .js file to store the code & run it in.

nano name.js

Next, paste in the code below, then add the link to the API of your choice. Remember to READ THE COMMENTS!

// BoredHouse's ImageBoard API Grabber //Original Cat Viewer by Sysroot // Whatever you set foor the limit on line 7, -1 that number and // replace the 10 on line 9, with that -1 specifically the //"(Math.random() * 10)" part. (replace the 10) (You might still get errors) export async function main(ns) { await ns.wget("!!! API LINK !!!", "json.txt"); var content = await ns.read("json.txt"); let x = Math.floor(Math.random() * 10) + 1; var imageJSON = JSON.parse(content)[x]; var aspectRatio = imageJSON.width / imageJSON.height; if (imageJSON.height > 570) { imageJSON.height = 570; imageJSON.width = imageJSON.height * aspectRatio; } else if (imageJSON.width > 1024) { imageJSON.width = 1024; imageJSON.height = imageJSON.width / aspectRatio; } // Some sites may have file_url below as something else, replace it if need be with that variable. var imageHTML = `<img src=\"${imageJSON.file_url}\" width=${imageJSON.width} height=${imageJSON.height}></img>`; ns.alert(imageHTML); };

Once you're finished setting up and doing some configurations the code, go back to the Terminal (Remember to SAVE!) and type:

run name.js

While there is some general help there, if you don't understand or have issues there's a help section right below here, where I actually answer the questions and don't mangle the writing.

Help!

I'm getting an error like this!

SYNTAX ERROR name.js@home (PID - 3) Unexpected token '<', "<?xml vers"... is not valid JSON (sorry we can't be more helpful)

ANSWER: Read the documentation for the API, you likely have to specify that you're using json in the API request link.

I'm getting an error like this!

RUNTIME ERROR name.js@home (PID - 4) Cannot read properties of undefined (reading 'width') stack: TypeError: Cannot read properties of undefined (reading 'width') at Module.main (home/name.js:10:30) at async L (file:///SteamLibrary/steamapps/common/Bitburner/resources/app/dist/main.bundle.js:2:1045379)

ANSWER: The request you put into the random number generator is likely larger than the maximum images the API loaded or was able to find. Either check again or the request you put in doesn't have enough images to match the maximum you set. I recommend requesting one more image if possible than the maximum number you put into the random generator.

If you have any other problems, put them in the comments. I'll try to respond to them ASAP.

The End


(Kind of) Simple Imageboard API Grabber image 20

Thank you for listening and potentially giving feedback, this is the first guide and the first real thing I've ever (semi) made relating to code, so let me know how I can improve.

If sysroot wants this gone or something because I used his code I'll gladly take it down if he dms me or asks in the comments.

Thanks for reading, have a great day.

Source: https://steamcommunity.com/sharedfiles/filedetails/?id=3016801382					

More Bitburner guilds