Skip to content
This repository was archived by the owner on Aug 7, 2024. It is now read-only.

Commit b39075e

Browse files
authored
Merge pull request #416 from bear/fix/issue415
Fix calc_expected_status_length (issue 415)
2 parents ae88240 + eeac783 commit b39075e

3 files changed

Lines changed: 24 additions & 7 deletions

File tree

tests/test_twitter_utils.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import twitter
66

77
from twitter.twitter_utils import (
8+
calc_expected_status_length,
89
parse_media_file
910
)
1011

@@ -58,3 +59,18 @@ def test_utils_error_checking(self):
5859
self.assertRaises(
5960
twitter.TwitterError,
6061
lambda: twitter.twitter_utils.enf_type('test', int, 'hi'))
62+
63+
def test_calc_expected_status_length(self):
64+
status = 'hi a tweet there'
65+
len_status = calc_expected_status_length(status)
66+
self.assertEqual(len_status, 16)
67+
68+
def test_calc_expected_status_length_with_url(self):
69+
status = 'hi a tweet there example.com'
70+
len_status = calc_expected_status_length(status)
71+
self.assertEqual(len_status, 40)
72+
73+
def test_calc_expected_status_length_with_url_and_extra_spaces(self):
74+
status = 'hi a tweet there example.com'
75+
len_status = calc_expected_status_length(status)
76+
self.assertEqual(len_status, 63)

twitter/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
__email__ = 'python-twitter@googlegroups.com'
2424
__copyright__ = 'Copyright (c) 2007-2016 The Python-Twitter Developers'
2525
__license__ = 'Apache License 2.0'
26-
__version__ = '3.2'
26+
__version__ = '3.2.1'
2727
__url__ = 'https://github.com/bear/python-twitter'
2828
__download_url__ = 'https://pypi.python.org/pypi/python-twitter'
2929
__description__ = 'A Python wrapper around the Twitter API'

twitter/twitter_utils.py

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -161,12 +161,13 @@ def calc_expected_status_length(status, short_url_length=23):
161161
Expected length of the status message as an integer.
162162
163163
"""
164-
replaced_chars = 0
165-
status_length = len(status)
166-
match = re.findall(URL_REGEXP, status)
167-
if len(match) >= 1:
168-
replaced_chars = len(''.join(match))
169-
status_length = status_length - replaced_chars + (short_url_length * len(match))
164+
status_length = 0
165+
for word in re.split(r'\s', status):
166+
if is_url(word):
167+
status_length += short_url_length
168+
else:
169+
status_length += len(word)
170+
status_length += len(re.findall(r'\s', status))
170171
return status_length
171172

172173

0 commit comments

Comments
 (0)