[Python-checkins] cpython: Issue #26830: Refactor Tools/scripts/google.py

berker.peksag python-checkins at python.org
Wed Sep 14 03:58:37 EDT 2016


https://hg.python.org/cpython/rev/f8c11b61cfb7
changeset:   103792:f8c11b61cfb7
user:        Berker Peksag <berker.peksag at gmail.com>
date:        Wed Sep 14 10:59:27 2016 +0300
summary:
  Issue #26830: Refactor Tools/scripts/google.py

Patch by Francisco Couzo.

files:
  Tools/scripts/google.py |  32 +++++++++++++++-------------
  1 files changed, 17 insertions(+), 15 deletions(-)


diff --git a/Tools/scripts/google.py b/Tools/scripts/google.py
--- a/Tools/scripts/google.py
+++ b/Tools/scripts/google.py
@@ -1,23 +1,25 @@
 #! /usr/bin/env python3
 
-import sys, webbrowser
+"""Script to search with Google
 
-def main():
-    args = sys.argv[1:]
-    if not args:
-        print("Usage: %s querystring" % sys.argv[0])
-        return
-    list = []
-    for arg in args:
-        if '+' in arg:
-            arg = arg.replace('+', '%2B')
+Usage:
+    python3 google.py [search terms]
+"""
+
+import sys
+import urllib.parse
+import webbrowser
+
+
+def main(args):
+    def quote(arg):
         if ' ' in arg:
             arg = '"%s"' % arg
-        arg = arg.replace(' ', '+')
-        list.append(arg)
-    s = '+'.join(list)
-    url = "http://www.google.com/search?q=%s" % s
+        return urllib.parse.quote_plus(arg)
+
+    qstring = '+'.join(quote(arg) for arg in args)
+    url = urllib.parse.urljoin('https://www.google.com/search', '?q=' + qstring)
     webbrowser.open(url)
 
 if __name__ == '__main__':
-    main()
+    main(sys.argv[1:])

-- 
Repository URL: https://hg.python.org/cpython


More information about the Python-checkins mailing list