Python-Twitter Issue where the full URL is counted against char count

python
api
urls

#1

Hello,

I’m new to the community and I’m just trying my first Twitter program that will tweet out some scraped data that I collect from an HTML site. I have this data saved in a MongoDB database. I’m using the Python-Twitter library for this project. I’m trying to iterate through some MongoDB documents and post them in a certain format. Here goes:

       #From the MongoDB Document  
	url = bill['url']
        #Cap the name at 91 characters
	if (len(name) > 91):
		name = name[0:88]
		name = name + "..."
        #That's 25 chars + 23 for a shortened URL = 91 left for name
	to_tweet = "#DonaldTrump has signed: " + name + url

Okay, this will tweet some bills the Prez has signed. As you can see, I should have exactly 140 characters at most. This is assuming that all URL’s are shortened/expanded to 23 characters. However, I’m getting an error that I’m over the 140 limit. I’m just not understanding why that is. Here is a print statement of to_tweet:

#DonaldTrump has signed: S.84 - A bill to provide for an exception to a limitation against appointment of persons...https://www.whitehouse.gov/legislation/s84-bill-provide-exception-limitation-against-appointment-persons-secretary-defense

A direct copy and paste of that puts me one character short of the limit in my web browser, and indeed, this exact same tweet works in a different python script. So does anyone have any advice as to how to solve this issue? Am I missing something so simple that I’ll feel really dumb? Any help would be greatly appreciated and thanks for having me in the community!


#2

Put a space between the … And the http.


#3

If only that were it. It’s been tried. I should have noted that.


#4

It looks like it should be fine. Are you adding any additional parameters to the request? Replying to anything?


#5

No.

The text I’m sending is literally the only thing being sent. As I said, it works with the same code in another program. It’s like it just doesn’t shorten the text for me.


#6

Just to show you my code. For the Python Twitter library it’s as simple as:

status = api.PostUpdate(to_tweet)

That’s all.


#7

Python Twitter considers S.84 to be a URL. You should file a bug with them.

A temporary workaround would be to disable their length validation.

status = api.PostUpdate(to_tweet, verify_status_length=False)

If you run this it will print out each word, if it’s considered a URL, and the length it will use.

#DonaldTrump False 12
has False 3
signed: False 7
S.84 True 23
- False 1
A False 1
bill False 4
to False 2
provide False 7
for False 3
an False 2
exception False 9
to False 2
a False 1
limitation False 10
against False 7
appointment False 11
of False 2
persons...https://www.whitehouse.gov/legislation/s84-bill-provide-exception-limitation-against-appointment-persons-secretary-defense True 23

Note that it also considers the persons... as the same word as the URL but then miscalculates it and only returns the length of the URL. So that is actually another bug.


#8

Wow, thank you so much for figuring that out for me. Your workaround works great. I’ll definitely file a bug with Python-Twitter.