My twitterbot sometimes gets error "media type unrecognized" during upload

(i’ve had to modify several links because I’m only able to post 2 of them due to glitch rules)

I’m trying to build a twitter bot that:

  • downloads an image from an API,
  • downloads some info (via json) from a separate API
  • saves image locally and then upload it to twitter
  • inserts alt_text for image;
  • posts the tweet.

My code is at:
https://spiral-tennis.glitch.me

And occasionally, it works as intended!
(see the results at twitter user: publiccleveland)

How it’s run:

  1. save (which generates the random number used later on)(I haven’t figured out yet how to generate a random number only when the endpoint is reached; that seems to involve more advanced javascript concepts that I don’t fully understand yet).
  2. hit the secret endpoint. (server.js:166:0) e.g. spiral-tennis.glitch.me/mysecretendpoint which
    (the downloads of the image, adds metadata, posts the tweet)

I’ve observed that, if I do not visit my bot’s endpoint for at least 4-5 hours and then visit it,
what happens is:
The b64content of my image is logged in my console and the tweet is successfully sent out!

If I do visit my endpoint within a 4-5 hour as the last time that I did:
the b64content of my image does not appear in the console (server.js:123:2) and tries to upload the media/upload function
"Error: media type unrecognized."

As far as I can tell, I’ve double checked that I’m not rate-limited by twitter, according to the output of:
twurl /1.1/application/rate_limit_status.json | jq .

I’ve read glitch’s restrictions
and I don’t think that I’m encountering any of them (the image that is download to the same file name)
but I’m not sure…

Any ideas on the glitch side of things that I am doing incorrectly?
Am I being rate limited by glitch of downloading photos into my account?
Is this a twitter API rate limit?

Lastly, the twit library, a popular twitter api library for node, has many outstanding PRs, so I’m using a modified fork of it that has the PRs merged in. (I can’t post more than 2 links, so see package.json for the library link)

Thanks in advance for any assistance.

I’ve figured it out!

What was happening was that I was attempting to upload the image before the image was finished downloading to glitch

The relevant code that fixed it is as follows:

request(imageURL)
  .pipe(fs.createWriteStream("dooddle.jpg"))
  .on("finish", () => {
    console.log(`The file is finished downloading.`);
    uploadPhoto(descriptionText, altTextRefined, "dooddle.jpg");
  });

In this code, the request method (provided by request module/package) takes the imageURL and pipe method (I think the pipe method is a method native to the Node API so it can be used in any node project) takes that image and creates a file named “dooddle.jpg”

Then, the ‘finish’ event (https://nodejs.org/api/stream.html#stream_event_finish)
is used so that, once the action is (creating the file named “doodle.jpg” ) is finished, run the
using an arrow function that says to run the uploadPhoto function.

The full code is at https://spiral-tennis.glitch.me/

1 Like

Hey @skorasaurus,

Sorry for the late reply, but glad to know that you’ve solved it!