Skip to content

Commit

Permalink
Merge pull request #2513 from sopel-irc/search-suggest-single
Browse files Browse the repository at this point in the history
search: fix cases where `.suggest` gets only one item back
  • Loading branch information
dgw authored Oct 6, 2023
2 parents 03dd79b + 0823852 commit df2009e
Show file tree
Hide file tree
Showing 4 changed files with 155 additions and 48 deletions.
14 changes: 12 additions & 2 deletions sopel/modules/search.py
Original file line number Diff line number Diff line change
Expand Up @@ -186,8 +186,9 @@ def search(bot, trigger):


@plugin.command('suggest')
@plugin.example('.suggest wikip', 'wikipedia', online=True, vcr=True)
@plugin.example('.suggest', '.suggest what?')
@plugin.example('.suggest sopel irc', 'sopel irc', online=True, vcr=True)
@plugin.example('.suggest wikip', 'wikipedia', online=True, vcr=True, user_help=True)
@plugin.example('.suggest lkashdfiauwgaef', 'Sorry, no result.', online=True, vcr=True)
@plugin.output_prefix(PLUGIN_OUTPUT_PREFIX)
def suggest(bot, trigger):
Expand All @@ -213,9 +214,18 @@ def suggest(bot, trigger):
answer = xmltodict.parse(response.text)['toplevel']

try:
answer = answer['CompleteSuggestion'][0]['suggestion']['@data']
answer = answer['CompleteSuggestion']

try:
answer = answer[0]
except KeyError:
# only one suggestion; don't need to extract first item
pass

answer = answer['suggestion']['@data']
except TypeError:
answer = None

if answer:
bot.say(answer)
else:
Expand Down
63 changes: 42 additions & 21 deletions test/vcr/modules/search/test_example_suggest_0.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,32 +2,53 @@ interactions:
- request:
body: null
headers:
Accept: ['*/*']
Accept-Encoding: ['gzip, deflate']
Connection: [keep-alive]
User-Agent: [python-requests/2.24.0]
Accept:
- '*/*'
Accept-Encoding:
- gzip, deflate, br
Connection:
- keep-alive
User-Agent:
- python-requests/2.31.0
method: GET
uri: https://suggestqueries.google.com/complete/search?output=toolbar&hl=en&q=lkashdfiauwgaef
response:
body:
string: !!binary |
H4sIAAAAAAAC/7Oxr8jNUShLLSrOzM+zVTLUM1Cyt7MpyS/ISS1LzbGz0YczAYEc0EoqAAAA
headers:
Alt-Svc: ['h3-Q050=":443"; ma=2592000,h3-29=":443"; ma=2592000,h3-27=":443";
ma=2592000,h3-T051=":443"; ma=2592000,h3-T050=":443"; ma=2592000,h3-Q046=":443";
ma=2592000,h3-Q043=":443"; ma=2592000,quic=":443"; ma=2592000; v="46,43"']
Cache-Control: ['private, max-age=3600']
Content-Encoding: [gzip]
Content-Type: [text/xml; charset=ISO-8859-1]
Date: ['Fri, 09 Oct 2020 04:40:44 GMT']
Expires: ['Fri, 09 Oct 2020 04:40:44 GMT']
P3P: [CP="This is not a P3P policy! See g.co/p3phelp for more info."]
Server: [gws]
Set-Cookie: ['1P_JAR=2020-10-09-04; expires=Sun, 08-Nov-2020 04:40:44 GMT; path=/;
domain=.google.com; Secure', 'NID=204=Pj4KwB7kHso7RFBcgEZMNDzLIhhuv8pFbIEWxp58KBEvUO9jquasGnvLCLcy0EfiJpPNvRfn_BIff0nkUzyqRJm4wSlrZ9tvmo7ohYEmAZerMGePW9wVWHCgMhCxArooUvVQ9W804M3QGhRlQ4ZZxwXnqGnfcSAA9h7DDDuRKLg;
expires=Sat, 10-Apr-2021 04:40:44 GMT; path=/; domain=.google.com; HttpOnly']
Transfer-Encoding: [chunked]
X-Frame-Options: [SAMEORIGIN]
X-XSS-Protection: ['0']
status: {code: 200, message: OK}
Alt-Svc:
- h3=":443"; ma=2592000,h3-29=":443"; ma=2592000
Cache-Control:
- private, max-age=3600
Content-Encoding:
- gzip
Content-Security-Policy:
- 'object-src ''none'';base-uri ''self'';script-src ''nonce-bqn-bde_sURx7gg6smeyow''
''strict-dynamic'' ''report-sample'' ''unsafe-eval'' ''unsafe-inline'' https:
http:;report-uri https://csp.withgoogle.com/csp/gws/xsrp'
Content-Type:
- text/xml; charset=ISO-8859-1
Date:
- Sun, 01 Oct 2023 22:08:12 GMT
Expires:
- Sun, 01 Oct 2023 22:08:12 GMT
P3P:
- CP="This is not a P3P policy! See g.co/p3phelp for more info."
Server:
- gws
Set-Cookie:
- 1P_JAR=2023-10-01-22; expires=Tue, 31-Oct-2023 22:08:12 GMT; path=/; domain=.google.com;
Secure
- NID=511=dO-EpOs4FOklsnc4Pp3npRl1SVjfpgKMbw8to-Rx9usNNf_zbI02DRK-uIdRBYYvAVDOdu_zksAU1aRpNDsefpbsuAf54C2fz4RMgIrUH5i-MEnJ5kFBbrq-d8Of2g-Cp2BZDB0-_XyI1WT5VWdwpNprOyrNJVs4wtfLPxPe_DQ;
expires=Mon, 01-Apr-2024 22:08:12 GMT; path=/; domain=.google.com; HttpOnly
Transfer-Encoding:
- chunked
X-Frame-Options:
- SAMEORIGIN
X-XSS-Protection:
- '0'
status:
code: 200
message: OK
version: 1
56 changes: 56 additions & 0 deletions test/vcr/modules/search/test_example_suggest_1.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
interactions:
- request:
body: null
headers:
Accept:
- '*/*'
Accept-Encoding:
- gzip, deflate, br
Connection:
- keep-alive
User-Agent:
- python-requests/2.31.0
method: GET
uri: https://suggestqueries.google.com/complete/search?output=toolbar&hl=en&q=wikip
response:
body:
string: !!binary |
H4sIAAAAAAAC/63TPQrDMAwF4KsYH6Bpd8cZeoSeQMQPR8R/2E6a4zdTpnYoaHuD9METyExHDGpH
bZzTqB+3u56s6bkE7AjWPHM8Y8dr8x6tn0PWtCsrR51G/eaVCxyTHqwZvq38x6gGqvMipSH5wE2M
8xQhVrQArm5J9nCKk3TpXArSAo6oUqSjuqqYndgxKyWXo6LaeQ7C6k9tuH7lA1+7OeZLAwAA
headers:
Alt-Svc:
- h3=":443"; ma=2592000,h3-29=":443"; ma=2592000
Cache-Control:
- private, max-age=3600
Content-Encoding:
- gzip
Content-Security-Policy:
- 'object-src ''none'';base-uri ''self'';script-src ''nonce-tweQn7Ss8hox9zQe-xYVHA''
''strict-dynamic'' ''report-sample'' ''unsafe-eval'' ''unsafe-inline'' https:
http:;report-uri https://csp.withgoogle.com/csp/gws/xsrp'
Content-Type:
- text/xml; charset=ISO-8859-1
Date:
- Sun, 01 Oct 2023 22:08:12 GMT
Expires:
- Sun, 01 Oct 2023 22:08:12 GMT
P3P:
- CP="This is not a P3P policy! See g.co/p3phelp for more info."
Server:
- gws
Set-Cookie:
- 1P_JAR=2023-10-01-22; expires=Tue, 31-Oct-2023 22:08:12 GMT; path=/; domain=.google.com;
Secure
- NID=511=GKP1S5d2fEPy67A69aYS0ZwYbNd_e7leYIAx6IRB4HxNWKE6YG-6ydKKf73w7khkbQ_LvVsrCodezyg_WiNpixCKw22_O0v3YtaiBlEGJ5tjKg7xQgW2wdGhVXo_svbIoz-SXgCyIsf-iMIGrKeUYQzTybvG5Jpc3PmvYcn62VQ;
expires=Mon, 01-Apr-2024 22:08:12 GMT; path=/; domain=.google.com; HttpOnly
Transfer-Encoding:
- chunked
X-Frame-Options:
- SAMEORIGIN
X-XSS-Protection:
- '0'
status:
code: 200
message: OK
version: 1
70 changes: 45 additions & 25 deletions test/vcr/modules/search/test_example_suggest_2.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,34 +2,54 @@ interactions:
- request:
body: null
headers:
Accept: ['*/*']
Accept-Encoding: ['gzip, deflate']
Connection: [keep-alive]
User-Agent: [python-requests/2.24.0]
Accept:
- '*/*'
Accept-Encoding:
- gzip, deflate, br
Connection:
- keep-alive
User-Agent:
- python-requests/2.31.0
method: GET
uri: https://suggestqueries.google.com/complete/search?output=toolbar&hl=en&q=wikip
uri: https://suggestqueries.google.com/complete/search?output=toolbar&hl=en&q=sopel+irc
response:
body:
string: !!binary |
H4sIAAAAAAAC/62TMQrDMAxFr2J8gNrN7DhDj9ATiFo4prYVbDXp8ZtSyFQoBW0fJD3e8OWmZ8lq
xdYT1VGfT1ZP3jEtGVfM3l2o7JHx+ogRO+9L3vUjqwAMo97SPS0YEmjjnfl28h9GYY059VkKF6jC
eyDFi1BQzA2B564GO1gpZIMaqChonG5ZTPRDFaBJCVVktVFjsZr8rrE5HuMFb2WKaDgDAAA=
H4sIAAAAAAAC/7Oxr8jNUShLLSrOzM+zVTLUM1Cyt7MpyS/ISS1LzbGzcc7PBTJLUoNL09NTi0uA
iuxsiuFshZTEkkRbpeL8gtQchcyiZCV9Oxt9bFr04SYCAKTyHI5xAAAA
headers:
Alt-Svc: ['h3-Q050=":443"; ma=2592000,h3-29=":443"; ma=2592000,h3-27=":443";
ma=2592000,h3-T051=":443"; ma=2592000,h3-T050=":443"; ma=2592000,h3-Q046=":443";
ma=2592000,h3-Q043=":443"; ma=2592000,quic=":443"; ma=2592000; v="46,43"']
Cache-Control: ['private, max-age=3600']
Content-Encoding: [gzip]
Content-Type: [text/xml; charset=ISO-8859-1]
Date: ['Fri, 09 Oct 2020 04:40:44 GMT']
Expires: ['Fri, 09 Oct 2020 04:40:44 GMT']
P3P: [CP="This is not a P3P policy! See g.co/p3phelp for more info."]
Server: [gws]
Set-Cookie: ['1P_JAR=2020-10-09-04; expires=Sun, 08-Nov-2020 04:40:44 GMT; path=/;
domain=.google.com; Secure', 'NID=204=xXUg2Mca4vSebU-u9utspHIfO-BbrJxaBASr5MUl5QdWU2MrASkdcw-iv_JpqhKoUDtgtJOnEIzx285qNJFJ0JNdXFvW63UXtfchTOsypgHlvQzF2W0VkkC9CvLQoo0TKj6NdY0weKL0TEF-LIqv-FsTEYyE_btmw0v9bLmyFY8;
expires=Sat, 10-Apr-2021 04:40:44 GMT; path=/; domain=.google.com; HttpOnly']
Transfer-Encoding: [chunked]
X-Frame-Options: [SAMEORIGIN]
X-XSS-Protection: ['0']
status: {code: 200, message: OK}
Alt-Svc:
- h3=":443"; ma=2592000,h3-29=":443"; ma=2592000
Cache-Control:
- private, max-age=3600
Content-Encoding:
- gzip
Content-Security-Policy:
- 'object-src ''none'';base-uri ''self'';script-src ''nonce-0QjS0PS1bkaLNV-ZwUrXtw''
''strict-dynamic'' ''report-sample'' ''unsafe-eval'' ''unsafe-inline'' https:
http:;report-uri https://csp.withgoogle.com/csp/gws/xsrp'
Content-Type:
- text/xml; charset=ISO-8859-1
Date:
- Sun, 01 Oct 2023 22:08:13 GMT
Expires:
- Sun, 01 Oct 2023 22:08:13 GMT
P3P:
- CP="This is not a P3P policy! See g.co/p3phelp for more info."
Server:
- gws
Set-Cookie:
- 1P_JAR=2023-10-01-22; expires=Tue, 31-Oct-2023 22:08:13 GMT; path=/; domain=.google.com;
Secure
- NID=511=R9yxS4Vqfw-TpuqZFOmLmSnTAI9dE_3a_3kI-t4dINKpdeJb12NLhoIAA9S5O8uE1Lz1Qj00FYzA1bVuMEASytLNDl920QJDA8u7Yp-YiskoGG9T0FRMK4-QzMSAy4gBwmY-e2MT_5uYFM_FgFY7EoaeWpo6hotXXbbopRdg9Co;
expires=Mon, 01-Apr-2024 22:08:13 GMT; path=/; domain=.google.com; HttpOnly
Transfer-Encoding:
- chunked
X-Frame-Options:
- SAMEORIGIN
X-XSS-Protection:
- '0'
status:
code: 200
message: OK
version: 1

0 comments on commit df2009e

Please sign in to comment.