jrkong / pySearch

Simple command line python script that opens a new browser tab to search the input string
BSD 3-Clause "New" or "Revised" License
4 stars 5 forks source link

Create a ping function to verify url validity #9

Open jrkong opened 5 years ago

jrkong commented 5 years ago

pySearch currently creates and opens links that may not exist. For example, current behavior defaults the domain to .ca however there is a chance that the domain is unsupported by the search engine (consider http://stackoverflow.ca/search?q=test.

In these cases the script should give the user a message stating that the built link is invalid. After this the code should try different popular domain extensions to see if any work. If a valid domain extension exists then provide a message which informs the user of the found domain and opens the link in the browser.

I believe the most lightweight solution would be a calling ping to the subsystem shell using os.system() subprocess (instructions on replacing os.system()/example here). In theory a successful ping should return error code 0 and all other returns should signify an error.

alexander-ponomaroff commented 5 years ago

I would like to work on this.

jrkong commented 5 years ago

Go for it!

Quick FYI in case you haven't seen the update I made to the issue. Originally I said os.subsystem may be a good idea however the subprocess module is a newer module which is intended to replace the os.system function. Python's docs mention how to update your code to replace os.subsystem with subprocess.

jrkong commented 5 years ago

@alexander-ponomaroff just wanted to bring this portion of our conversation off Slack so others can provide input on this as there may be better ways to do this.

So the subsystem ping route is a no go because ping doesn't support pinging resources so we need a way to check if a search URL is valid.

My suggestion would be to have an OS check at the beginning of the function and call curl or an equivalent depending on what's supported by the OS.

On Windows we can get the functionality we want by using Powershell's Invoke-RestMethod. I've got an example call that works below.

subprocess.call(["C:\\WINDOWS\\system32\\WindowsPowerShell\\v1.0\\powershell.exe", "Invoke-RestMethod -Uri www.stackoverflow.ca/search?q=test"])

subprocess.call will return a 0 if the command succeeds. Anything else is a failure which means the URL is invalid.