Home > Blockchain >  How to upload images on twitter with their original name?
How to upload images on twitter with their original name?

Time:06-27

I have created a bot that can upload images on twitter but the problem is that I have to give a caption to every picture myself. What I want is that the bot should detect the name of the image (in this case "world") and use it as caption and I don't have to manually write it in tweet. The code I'm using is:

import tweepy
from tweepy.streaming import Stream
from tweepy import Stream

consumer_key = 'XXXX'
consumer_secret = 'XXXX'
access_token = 'XXXX'
access_token_secret = 'XXXX'

auth = tweepy.OAuthHandler(consumer_key, consumer_secret)
auth.set_access_token(access_token, access_token_secret)
api = tweepy.API(auth, wait_on_rate_limit = True)

def upload_media(text, filename):
    media = api.media_upload(filename)
    api.update_status(text, media_ids = [media.media_id_string])

upload_media('World is amazing,','world is amazing.png')

CodePudding user response:

<you may want to try splitting your filename on "." and taking the first element like so :

def upload_media(text, filename):
    media = api.media_upload(filename)
    text = filename.split(".")[0]
    api.update_status(text, media_ids = [media.media_id_string])

However, this only works when you have a file without its path. If you have a path provided, you better do it like so :

def upload_media(text, filename):
    media = api.media_upload(filename)
    text = filename.split(".")[0].split("\\")[-1]
    api.update_status(text, media_ids = [media.media_id_string])

Please note that with these both solutions, you are no longer using the argument text.

  • Related