Skip to content

Commit c4a812e

Browse files
authored
Merge pull request #1995 from sopel-irc/modules-use-bot.say-trailing
modules: use `bot.say()`'s `trailing` parameter
2 parents dc2b493 + 54aa021 commit c4a812e

File tree

5 files changed

+40
-51
lines changed

5 files changed

+40
-51
lines changed

sopel/modules/instagram.py

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -45,11 +45,11 @@ def instaparse(bot, trigger, match):
4545
# Get the embedded JSON
4646
json = get_insta_json(instagram_url)
4747
try:
48-
bot.say(parse_insta_json(json))
48+
bot.say(parse_insta_json(json), trailing='…')
4949
except ParseError:
5050
try:
5151
json = get_oembed_json(instagram_url)
52-
bot.say(parse_oembed_json(json))
52+
bot.say(parse_oembed_json(json), trailing='…')
5353
except ParseError:
5454
LOGGER.exception(
5555
"Unable to find Instagram's payload for URL %s", instagram_url)
@@ -121,19 +121,6 @@ def parse_insta_json(json):
121121
else:
122122
parts.append('%s unknown user' % title)
123123

124-
# Media caption
125-
try:
126-
icap = needed['edge_media_to_caption']['edges'][0]['node']['text']
127-
# Strip newlines
128-
icap = icap.replace('\n', ' ')
129-
# Truncate caption
130-
icap = (icap[:256] + '…') if len(icap) > 256 else icap
131-
except (KeyError, IndexError):
132-
icap = None
133-
134-
if icap:
135-
parts.append(icap)
136-
137124
# Media width and height
138125
iwidth = dimensions.get('width') or None
139126
iheight = dimensions.get('height') or None
@@ -162,6 +149,19 @@ def parse_insta_json(json):
162149
pubdate = datetime.utcfromtimestamp(idate).strftime(dateformat)
163150
parts.append('Uploaded: %s' % pubdate)
164151

152+
# Media caption
153+
# Goes last so Sopel can take care of truncating it automatically
154+
# instead of hard-coding an arbitrary maximum length.
155+
try:
156+
icap = needed['edge_media_to_caption']['edges'][0]['node']['text']
157+
# Strip newlines
158+
icap = icap.replace('\n', ' ')
159+
except (KeyError, IndexError):
160+
icap = None
161+
finally:
162+
if icap:
163+
parts.append(icap)
164+
165165
# Build the message
166166
return ' | '.join(parts)
167167

sopel/modules/reddit.py

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@
1313
import datetime as dt
1414
import re
1515
import sys
16-
import textwrap
1716

1817
import praw
1918
import prawcore
@@ -238,15 +237,12 @@ def comment_info(bot, trigger, match):
238237

239238
# stolen from the function I (dgw) wrote for our github plugin
240239
lines = [line for line in c.body.splitlines() if line and line[0] != '>']
241-
short = textwrap.wrap(lines[0], 250)[0]
242-
if len(lines) > 1 or short != lines[0]:
243-
short += ' […]'
244240

245241
message = message.format(
246242
author=author, points=c.score, points_text=points_text,
247-
posted=posted, comment=short)
243+
posted=posted, comment=' '.join(lines))
248244

249-
bot.say(message)
245+
bot.say(message, trailing=' […]')
250246

251247

252248
def subreddit_info(bot, trigger, match, commanded=False):
@@ -309,7 +305,8 @@ def subreddit_info(bot, trigger, match, commanded=False):
309305
message = message.format(
310306
link=link, nsfw=nsfw, subscribers='{:,}'.format(s.subscribers),
311307
created=created, public_description=s.public_description)
312-
bot.say(message)
308+
309+
bot.say(message, trailing=' […]')
313310

314311

315312
def redditor_info(bot, trigger, match, commanded=False):

sopel/modules/tld.py

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -338,11 +338,8 @@ def gettld(bot, trigger):
338338
items.append('{}: {}'.format(field, value))
339339

340340
message = ' | '.join(items)
341-
usable, excess = tools.get_sendable_message(message)
342-
if excess:
343-
message = usable + ' […]'
344341

345-
bot.say(message)
342+
bot.say(message, trailing=' […]')
346343

347344

348345
@plugin.command('tldcache')

sopel/modules/wikipedia.py

Lines changed: 16 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -152,26 +152,33 @@ def mw_search(server, query, num):
152152
def say_snippet(bot, trigger, server, query, show_url=True):
153153
page_name = query.replace('_', ' ')
154154
query = quote(query.replace(' ', '_'))
155+
url = 'https://{}/wiki/{}'.format(server, query)
156+
157+
# If the trigger looks like another instance of this plugin, assume it is
158+
if trigger.startswith(PLUGIN_OUTPUT_PREFIX) and trigger.endswith(' | ' + url):
159+
return
160+
155161
try:
156162
snippet = mw_snippet(server, query)
157163
except KeyError:
158164
if show_url:
159165
bot.reply("Error fetching snippet for \"{}\".".format(page_name))
160166
return
161-
msg = '{} | "{}"'.format(page_name, snippet)
162-
msg_url = msg + ' | https://{}/wiki/{}'.format(server, query)
163-
if msg_url == trigger: # prevents triggering on another instance of Sopel
164-
return
167+
168+
msg = '{} | "{}'.format(page_name, snippet)
169+
170+
trailing = '"'
165171
if show_url:
166-
msg = msg_url
167-
bot.say(msg)
172+
trailing += ' | ' + url
173+
174+
bot.say(msg, trailing=' […]' + trailing)
168175

169176

170177
def mw_snippet(server, query):
171178
"""Retrieves a snippet of the given page from the given MediaWiki server."""
172179
snippet_url = ('https://' + server + '/w/api.php?format=json'
173180
'&action=query&prop=extracts&exintro&explaintext'
174-
'&exchars=300&redirects&titles=')
181+
'&exchars=500&redirects&titles=')
175182
snippet_url += query
176183
snippet = get(snippet_url).json()
177184
snippet = snippet['query']['pages']
@@ -193,7 +200,7 @@ def say_section(bot, trigger, server, query, section):
193200
return
194201

195202
msg = '{} - {} | "{}"'.format(page_name, section.replace('_', ' '), snippet)
196-
bot.say(msg)
203+
bot.say(msg, trailing=' […]"')
197204

198205

199206
def mw_section(server, query, section):
@@ -225,15 +232,7 @@ def mw_section(server, query, section):
225232
parser = WikiParser(section.replace('_', ' '))
226233
parser.feed(data['parse']['text']['*'])
227234
text = parser.get_result()
228-
text = ' '.join(text.split()) # collapse multiple whitespace chars
229-
230-
trimmed = False
231-
232-
while len(text) > (420 - len(query) - len(section) - 18):
233-
text = text.rsplit(None, 1)[0]
234-
trimmed = True
235-
if trimmed:
236-
text += '...'
235+
text = ' '.join(text.split()) # collapse multiple whitespace chars
237236

238237
return text
239238

sopel/modules/wiktionary.py

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -119,14 +119,12 @@ def wiktionary(bot, trigger):
119119
return
120120

121121
result = format(word, definitions)
122-
if len(result) < 150:
122+
if len(result) < 300:
123123
result = format(word, definitions, 3)
124-
if len(result) < 150:
124+
if len(result) < 300:
125125
result = format(word, definitions, 5)
126126

127-
if len(result) > 300:
128-
result = result[:295] + '[…]'
129-
bot.say(result)
127+
bot.say(result, trailing=' […]')
130128

131129

132130
@plugin.command('ety')
@@ -146,6 +144,4 @@ def wiktionary_ety(bot, trigger):
146144

147145
result = "{}: {}".format(word, etymology)
148146

149-
if len(result) > 300:
150-
result = result[:295] + '[...]'
151-
bot.say(result)
147+
bot.say(result, trailing=' […]')

0 commit comments

Comments
 (0)