CmdLine Google Search Results in New Firefox Tab

chriswelborn 0 Tallied Votes 300 Views Share

This is a script that was supposed to be very basic, just running a command with my scripts arguments attached. I didn't realize that if user 'cj' opens firefox, when user 'root' does 'firefox -new-tab' it doesn't work. Firefox will just open a new window, and thats exactly what I didn't want to happen. The whole point was to open in a new-tab. So finding the user that logged onto the desktop, when the script was ran by root became the challenge. You can find out who ran the script alot of ways, but thats not what I wanted. Sometimes i'm doing things in my terminal as root and I want to just "gsearch Something" and have it open in a new tab, not a new window. The 'commands' module came in handy here, but if there is a breaking point for this script, it will be in using the 'users' command to get the logged in desktop user.
Another thing about this is it uses 'urllib' to make the search terms compatible URLS. Like:"This Is My Search" becomes"This%20Is%20My%20Search"

Anyway, once again, this is my second week with Python and I'm loving it, there may be better ways to do some of this stuff, but I am still learning. It's part of the process. Part of the reason I'm posting this code is so maybe someone might find something in it and say "hey, you could've just did this in 2 lines", or maybe someone new like me can use it for good. After all, I had to do alot of searching myself to figure some of this stuff out.

#!/usr/bin/env python

# Google Command Line Search using Firefox
# ...opens results in New Tab
# ...If this script is ran as root, we will use su to try to run firefox
#    as the regular logged-on user, because if firefox is already
#    running it was probably ran by the regular user, and 
#    'firefox -new-tab' will not work correctly if ran by root when
#    normal user's firefox is already open. does that make sense?
#    (it opens a second firefox window, and we don't want that)
# -Christopher Joseph Welborn ( 2012

# Imports
import urllib   # for making URL's compatible
import os       # for getting User ID
import sys      # for getting arguments
import commands # for getting logged in user (not user that ran the script)

# Get normally logged on user (hopefully not root)
str_user = os.environ["USER"]
# If we got root we need to try one more thing to get a normal user
if str_user == "root":
	# get output of shell command 'users'
	str_usercmd = commands.getoutput('users')
	# get list of all users logged into desktop
	str_users = str_usercmd.split(" ")
	# go until we find one that is not root?
	for u in str_users:
		if u != "root":
			# we found one that isn't root, so save it.
			str_user = u
# Pre Firefox Command (hopefully, to make sure we're not running as root)
str_precmd = "su " + str_user + " -c "
# Initialize Search String
str_searchterm = ""

# FIREFOX SEARCH                                                 FIREFOX
def search_firefox():
	# build Shell command with URL and SearchTerm
	sCmd = "firefox -new-tab\"" + str_searchterm + "\""

	# Running as root? Fix the command to use SU to CJ...
	if os.geteuid() == 0:
		# Status
		print "Currently running as root, using su to " + str_user + "..."
		# Add SU to CJ, and enclose regular command in ' 's
		sCmd = str_precmd + "'" + sCmd + "'"

	# Shell firefox with our google search URL
		iret = os.system(sCmd)
		print "Error running command: " + sCmd
	# firefox exit status?
	if iret > 0:
		print "Firefox exited with status: " + str(iRet)
	print "Finished..."
	return 0
# START OF SCRIPT                                                  START
if __name__ == '__main__':
	# Check Arguments
	if len(sys.argv) < 2:
		print "No search term entered!"
	# root warning...
	if str_user == "root":
		print "You are currently running as root," \
			  " this may not work properly."
	# Get first search argument
	str_searchterm = sys.argv[1]

	# Cycle thru all arguments	
	for i in range(len(sys.argv)):
		# don't count scriptname as argument ( >1)
		if i > 1:
			# insert space before everything but the first item
			str_searchterm = str_searchterm + " " + sys.argv[i]

	# Status
	print "Searching For \"" + str_searchterm + "\"..."

	# Fix Search Term URL
	str_searchterm = urllib.quote(str_searchterm)

	# Do firefox Search, by default right now
TrustyTony 888 pyMod Team Colleague Featured Poster

I would actually use a shell variable set and exported when starting each desktop. I would then use this saved username for current desktop (screen) to invoke browser, probably by having a statup script for browser using su with the shell variable name to open passed in url in a new tab. Then also it would work in multiuser Linux/x (including multiple X servers like Xvnc also running) and windows enpvironment and also without Firefox installed. So it would be generally applicable and system independent.

yeh, i knew this was kinda limited, it would probably break in certain cases where more than 1 person is logged on, the shell variable sounds good. I would do that for my own system definately.

as long as the user isn't running root it's a very basic script.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.