Skip to content

Commit

Permalink
creating quote tweets: append tweet url *after* truncating
Browse files Browse the repository at this point in the history
  • Loading branch information
snarfed committed Feb 2, 2017
1 parent bb0161a commit f36f061
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 10 deletions.
20 changes: 20 additions & 0 deletions granary/test/test_twitter.py
Original file line number Diff line number Diff line change
Expand Up @@ -1979,6 +1979,26 @@ def test_create_quote_tweet(self):
<a href="https://twitter.com/snarfed_org/status/100">this tweet</a>:""",
preview.description)

def test_create_quote_tweet_strips_quotation(self):
self.expect_urlopen(twitter.API_POST_TWEET, {}, params={
'status': 'I agree with this https://twitter.com/snarfed_org/status/100',
})
self.mox.ReplayAll()

obj = copy.deepcopy(QUOTE_ACTIVITY['object'])
obj['content'] = 'I\tagree\n<cite class="u-quotation-of h-cite">foo</cite>\nwith this'
created = self.twitter.create(obj)

def test_create_quote_tweet_truncated_content(self):
self.expect_urlopen(twitter.API_POST_TWEET, {}, params={
'status':(u'X X X X X X X X X X X X X X X X X X X X X X X X X X X X X X X X X X X X X X X X X X X X X X X X X X X X X X X X X X… https://twitter.com/snarfed_org/status/100').encode('utf-8'),
})
self.mox.ReplayAll()

obj = copy.deepcopy(QUOTE_ACTIVITY['object'])
obj['content'] = u'X X X X X X X X X X X X X X X X X X X X X X X X X X X X X X X X X X X X X X X X X X X X X X X X X X X X X X X X X X X X X X X X X X X X X X'
created = self.twitter.create(obj)

def test_create_unsupported_type(self):
for fn in self.twitter.create, self.twitter.preview_create:
result = fn({'objectType': 'activity', 'verb': 'rsvp-yes'})
Expand Down
32 changes: 22 additions & 10 deletions granary/twitter.py
Original file line number Diff line number Diff line change
Expand Up @@ -611,22 +611,23 @@ def _create(self, obj, preview=None, include_link=source.OMIT_LINK,
base_url = self.base_object(obj).get('url')
prefer_content = type == 'note' or (base_url and (type == 'comment'
or obj.get('inReplyTo')))
content = self._content_for_create(obj, ignore_formatting=ignore_formatting,
prefer_name=not prefer_content,
strip_first_video_tag=bool(video_url))

preview_description = ''
quote_tweet_url = None
for att in obj.get('attachments', []):
url = self.URL_CANONICALIZER(att.get('url', ''))
if url and TWEET_URL_RE.match(url):
content += ' ' + url
quote_tweet_url = url
preview_description += """\
<span class="verb">quote</span>
<a href="%s">this tweet</a>:<br>
%s
<br>and """ % (url, self.embed_post(att))
break

content = self._content_for_create(
obj, ignore_formatting=ignore_formatting, prefer_name=not prefer_content,
strip_first_video_tag=bool(video_url), strip_quotations=bool(quote_tweet_url))

if not content:
if type == 'activity':
content = verb
Expand Down Expand Up @@ -675,8 +676,8 @@ def _create(self, obj, preview=None, include_link=source.OMIT_LINK,

# truncate and ellipsize content if it's over the character
# count. URLs will be t.co-wrapped, so include that when counting.
content = self._truncate(
content, obj.get('url'), include_link, type)
content = self._truncate(content, obj.get('url'), include_link, type,
quote_tweet=quote_tweet_url)

# linkify defaults to Twitter's link shortening behavior
preview_content = util.linkify(content, pretty=True, skip_bare_cc_tlds=True)
Expand Down Expand Up @@ -798,14 +799,16 @@ def _create(self, obj, preview=None, include_link=source.OMIT_LINK,

return source.creation_result(resp)

def _truncate(self, content, url, include_link, type):
def _truncate(self, content, url, include_link, type, quote_tweet=None):
"""Shorten tweet content to fit within the 140 character limit.
Args:
content: string
url: string
include_link: string
type: string: 'article', 'note', etc.
quote_tweet: string URL, optional. If provided,
it will be appended to the content, *after* truncating.
Return: string, the possibly shortened and ellipsized tweet text
"""
Expand All @@ -814,13 +817,22 @@ def _truncate(self, content, url, include_link, type):
else:
format = brevity.FORMAT_NOTE

return brevity.shorten(
target_length = MAX_TWEET_LENGTH
if quote_tweet:
target_length -= (TCO_LENGTH + 1)

truncated = brevity.shorten(
content,
# permalink is included only when the text is truncated
permalink=url if include_link != source.OMIT_LINK else None,
# permashortlink is always included
permashortlink=url if include_link == source.INCLUDE_LINK else None,
target_length=MAX_TWEET_LENGTH, link_length=TCO_LENGTH, format=format)
target_length=target_length, link_length=TCO_LENGTH, format=format)

if quote_tweet:
truncated += ' ' + quote_tweet

return truncated

def upload_images(self, urls):
"""Uploads one or more images from web URLs.
Expand Down

0 comments on commit f36f061

Please sign in to comment.