Browse Source

Update web-search.py

Coder 4 years ago
parent
commit
84f530d03e
1 changed files with 51 additions and 49 deletions
  1. 51 49
      web-search.py

+ 51 - 49
web-search.py

@@ -7,61 +7,63 @@
 
 #########################################################################################
 
-
-from time import time
-from googlesearch import search
-import sys
-import webbrowser
-
-#Print title using variables and delete after using variables to clear up RAM
-width = 40
-titletxt = 'Web-Search'
-title = titletxt.center(width)
-print(title)
-del width
-del titletxt
-del title
-
-search_query = input('\nWhat do you want to search for on the web?: ')
-result_num = input('How many results do you want to retrieve from the web?: ')
-
-#Verify user input
 try:
-    int_result_num = int(result_num)
-except:
-    sys.exit('The number of results you wanted to retrieve was not a number. Exiting program...')
-
-#Display web results
-print('The top %d results from the web are:' %(int_result_num))
+    from time import time
+    from googlesearch import search
+    import sys
+    import webbrowser
+
+    #Print title using variables and delete after using variables to clear up RAM
+    width = 40
+    titletxt = 'Web-Search'
+    title = titletxt.center(width)
+    print(title)
+    del width
+    del titletxt
+    del title
+
+    search_query = input('\nWhat do you want to search for on the web?: ')
+    result_num = input('How many results do you want to retrieve from the web?: ')
+
+    #Verify user input
+    try:
+        int_result_num = int(result_num)
+    except:
+        sys.exit('The number of results you wanted to retrieve was not a number. Exiting program...')
+
+    #Display web results
+    print('The top %d results from the web are:' %(int_result_num))
+
+    start = time()
+    for url in search(search_query, tld='com', stop=int_result_num):
+        print(url + " - %s" %(url.split('/')[2]))
+    end = time()
 
-start = time()
-for url in search(search_query, tld='com', stop=int_result_num):
-    print(url + " - %s" %(url.split('/')[2]))
-end = time()
+    print('Found %d results from the web in %s' %(int_result_num, end - start) + ' seconds.')
 
-print('Found %d results from the web in %s' %(int_result_num, end - start) + ' seconds.')
+    #Delete time variables to clear RAM
+    del start
+    del end
 
-#Delete time variables to clear RAM
-del start
-del end
+    #Ask user if they want to open the links in the web browser with validation
+    open_in_browser = input('Do you want to open the given URLs in your web browser (Y/N)? ')
 
-#Ask user if they want to open the links in the web browser with validation
-open_in_browser = input('Do you want to open the given URLs in your web browser (Y/N)? ')
+    if open_in_browser.upper() == 'Y':
+        for url in search(search_query, tld='com', stop=int_result_num):
+            webbrowser.open(url)
 
-if open_in_browser.upper() == 'Y':
-    for url in search(search_query, tld='com', stop=int_result_num):
-        webbrowser.open(url)
+    elif open_in_browser.upper() == 'N':
+        sys.exit('Ok! Bye!')
 
-elif open_in_browser.upper() == 'N':
-    sys.exit('Ok! Bye!')
+    else:
+        while open_in_browser.upper() != 'Y' and open_in_browser.upper() != 'N':
+            open_in_browser = input('Invalid response! Do you want to open the given URLs in your web browser (Y/N)? ')
 
-else:
-    while open_in_browser.upper() != 'Y' and open_in_browser.upper() != 'N':
-        open_in_browser = input('Invalid response! Do you want to open the given URLs in your web browser (Y/N)? ')
+            if open_in_browser.upper() == 'Y':
+                for url in search(search_query, tld='com', stop=int_result_num):
+                    webbrowser.open(url)
 
-        if open_in_browser.upper() == 'Y':
-            for url in search(search_query, tld='com', stop=int_result_num):
-                webbrowser.open(url)
-
-        elif open_in_browser.upper() == 'N':
-            sys.exit('Ok! Bye!')
+            elif open_in_browser.upper() == 'N':
+                sys.exit('Ok! Bye!')
+except:
+    print("\033[1;31;40mSomething went wrong.")