jarun / ddgr

:duck: DuckDuckGo from the terminal
GNU General Public License v3.0
2.92k stars 139 forks source link

bang query string not quoted #121

Closed benhormann closed 3 years ago

benhormann commented 3 years ago

cmd: BROWSER=echo ddgr --np --show-browser-logs '!? tom & jerry' or: BROWSER=echo ddgr --show-browser-logs !? tom & jerry

actual: https://duckduckgo.com/?q=!? tom & jerry

expected: https://duckduckgo.com/?q=%21%3F+tom+%26+jerry or, also legal: https://duckduckgo.com/?q=!%3F+tom+%26+jerry


Updating url.full() works, is this sufficient?:

@@ -439,9 +439,9 @@ class DdgUrl:
         q = ''
         if self._keywords:
             if isinstance(self._keywords, list):
-                q += '+'.join(list(self._keywords))
+                q += '+'.join(map(urllib.parse.quote_plus, self._keywords))
             else:
-                q += self._keywords
+                q += urllib.parse.quote_plus(self._keywords)

         url = (self.scheme + ':') if self.scheme else ''
         url += '//' + self.netloc + '/?q=' + q
jarun commented 3 years ago

Do examples 3, 4 and 8 help?

Also, for these kind of changes we need more testing. For example, are results for normal searches the same? Google takes tokens within quotes as occurring together and/or must present. Are those kind of side-effects creeping in?

benhormann commented 3 years ago

I agree, it needs proper testing. I didn't find any reference to that method besides handling a bang. Normal searching works fine with ?, &, and = characters.

Do examples 3, 4 and 8 help?

!, ? and & need escaped in most shells, but are OK in a string. In most shells ddgr '!? tom & jerry' and ddgr \!\?\ tom\ \&\ jerry are equivalent (though the latter won't work in PowerShell, for example). Typing the search, !? tom & jerry, at the ddgr prompt is the easiest way to avoid shell specific escaping.

jarun commented 3 years ago

I didn't find any reference to that method

There is no documented method. Please use the same search strings with and without your change and see if the results are same. Also do it in debug mode so you can see the query string as well.

jarun commented 3 years ago

Please raise the PR when you are done with testing.

jarun commented 3 years ago

Closing this as there's no update on the PR.