Trying to make a Twitterbot that uses lots of text as source material


#1

I’m working with the basic twitter bot template, but I feel totally out of my depth here.

Here’s the basic idea of what I’m trying to do: I have a text file that contains a lot of snippets of text (no, A LOT. Like, it’s a 6MB plaintext file, and it has about 50,000 unique snippets inside).

I turned the text file into a JSON file, and tried to make each snippet into a JSON object with an index property (integer) and a content property (the content as a string).

I want a Twitter bot that tweets each snippet in sequence.

I uploaded my JSON file to the assets folder, but now I don’t know how to get the text out of there. The JSON file looks like this:

data = '[
{"content":"foo","id":1}, 
{"content":"bar","id":2}, 
…
{"content":"baz","id":49999}, 
]';

What do I do next? I tried the “Ask for help” feature but it disappeared after a few seconds.


#2

There will be an issue with your JSON, the single quote before and after the opening and closing [] should be removed. You may also want to remove the data = as well. Then in your main file you can load your JSON using something like

const data = require('./assests/data.json');

Then you can access your data like

console.log(data[0].content) // will log "foo"

#3

Thanks! However I am getting an error in “module.js” – do you know what this means? It says "Cannot find module ‘./assets/myfile.json’.


#4

The path ./assests/data.json is likely incorrect, I just used that as an example, which would only work if you had the file in one of the directories You actually wouldn’t be able to require from the assets URL, sorry about that. I recommend going into the console and using wget to download the file to the local file system. The command would be something like

wget https://cdn.hyperdev.com/drag-in-files.json?1477153069954

You would likely need to rename the file it will be saved with the ?1477153… stuff after it.


#5

The npm package remote-json might help in this case, rather then downloading the file itself.

https://www.npmjs.com/package/remote-json