geduldig / TwitterAPI

Minimal python wrapper for Twitter's REST and Streaming APIs
938 stars 263 forks source link

Dupilcate tweet on Unix [Linux Lite] #166

Closed PersonMeetup closed 3 years ago

PersonMeetup commented 3 years ago

I'm hosting a Discord Bot that forwards messages to a Twitter account on a build of Linux Lite. I've recently added functionality that allows for images to be posted with tweets as well, but I've found that I get duplicate tweets. two tweets are sent out at once, one with the string and photo while the other has just the string. I expect that this is an error on my end as the example code provided in the repo works fine, but I don't understand why this is occurring. I have tested the bot on Windows, and it works correctly.

For reference, here is my code.

    def distweet(message):
        """Decompiles a `discord.Message` method and posts its content to Twitter"""
        status = message.content
        if message.attachments:
            logging.info(f'Message contains attachment, extracting URL... [{(message.attachments[0]).url}]')
            r = requests.get((message.attachments[0]).url)
            with open('image.png','wb') as f:
                f.write(r.content)

            file = open('./image.png', 'rb')
            data = file.read()
            r = api.request('media/upload', None, {'media': data})
            #print('UPLOAD MEDIA SUCCESS' if r.status_code == 200 else 'UPLOAD MEDIA FAILURE: ' + r.text)

            if r.status_code == 200:
                media_id = r.json()['media_id']
                logging.info('Media upload success, Tweeting string...')
                tweet = api.request('statuses/update', {'status':status[:280], 'media_ids':media_id})
                return tweet.status_code

        else:
            tweet = api.request('statuses/update', {'status':status[:280]})
            return tweet.status_code
geduldig commented 3 years ago

Are you uploading (tweeting) a duplicate tweet? Twitter will not allow the same account to tweet identical content within 24 hours.

PersonMeetup commented 3 years ago

Yes, but not on purpose. The code is supposed to tweet only once, and I bet it gets around the identical content filter with the inclusion of the photo in the second tweet. Let me know if you need any other details.

geduldig commented 3 years ago

Did I answer your question? The code you attached does not check if it is tweeting duplicate content. You would need to fix that.

PersonMeetup commented 3 years ago

Alright, I'll look into seeing if any content is duplicated then, sorry about that.

There's still some confusion though. As I stated, this only happens with the Linux Lite build I'm using to host the bots on (running the same code on Windows does not produce this problem), and it seems like it only happens with the bot's code. Could just be some jank that comes from running Python code on Linux, but who knows.

geduldig commented 3 years ago

If you have access to the bot's code, you might just search the code for 'statuses/update' and see if it checks for duplicates before tweeting. But for this to work, the bot would need to save every tweet from the past 24 hours to check against.

PersonMeetup commented 3 years ago

I would guess it should be as simple as keeping a cache of sent tweet ids available, so that should be doable. Probably would be more in-depth compared to logging functions as well, since those didn't report 2 tweets being sent out.

geduldig commented 3 years ago

Saving the tweet id is not going to work. Try tweeting "blah blah" twice. The second will not go through even though their tweet ids are different.

PersonMeetup commented 3 years ago

Alright, I've managed to fix the bot so that it's not creating duplicate tweets anymore (I had the discord file handle downloading images instead), but now I'm coming to the problem that media isn't being sent out with the status message anymore. If wanted I can give the updated code, but other than that I'm going to be closing the issue since the original problem has been solved. (Thanks for trying to help btw. Feel like I might have been annoying to respond to.)

Honestly, the solution might be to take a hammer to Linux.