2

I'm working on a home automation project that uses python-twitter to post update information. When I try to post a shorter-than-140 string with no links, I end up getting a TwitterError: Text must be less than or equal to 140 characters.

Here's some shell output describing the problem:

>>> import twitter
>>> api = twitter.Api(
...     consumer_key=os.environ['TW_API_KEY'],
...     consumer_secret=os.environ['TW_API_SECRET'],
...     access_token_key=os.environ['TW_ACCESS_KEY'],
...     access_token_secret=os.environ['TW_ACCESS_SECRET']
...     )
>>> m = 'The house is now inside the comfort range. Conditions: 28.4 C (83.1 F) at 50.0% humidity. It feels like 84 F.'
>>> len(m)
109
>>> api.PostUpdate(m)
Traceback (most recent call last):
  File "<console>", line 1, in <module>
  File "/home/pi/.virtualenvs/site/local/lib/python2.7/site-packages/twitter/api.py", line 952, in PostUpdate
    raise TwitterError("Text must be less than or equal to 140 characters.")
TwitterError: Text must be less than or equal to 140 characters.

I tried converting the string to unicode using api.PostUpdate(u'{0}'.format(m)) but unsurprisingly no help there either.

I assume what's happening is that python-twitter is interpreting the decimal numbers as links, thereby decreasing the amount of characters I have left to use, but I don't know enough about the api to know if that's true or not.

Given that I am not planning on including links in these tweets, is there a way to force python-twitter to ignore the decimals without actually getting rid of them? I would prefer not to settle for that workaround, as I kind of like being able to report in decimal notation. I would also prefer not to have to go with tweepy but I suppose I'd swallow that pill if I needed to.

Update: I've found a way to turn off the api's length check using the verify_status_length=False flag in PostUpdate(), which allows me to tweet the above string m with no errors. It doesn't feel that elegant but I will stick with it for now until I find a better solution.

Ian
  • 172
  • 1
  • 13

0 Answers0