Code

Simple Python Script To Search Wikipedia From The Command Line by Joseph Palumbo

If you’re like me, you don’t like to take your fingers off the keyboard. And if, like me, you’re a keyboard cowboy, you probably do a lot of work from the command line.

I’m always looking for ways to increase my productivity by adding more tools and functionality to my command line. So here’s a very simply python script using Selenium to search Wikipedia from CLI.

from selenium import webdriver
from sys import argv # Include file to upload
import sys

# unpacks the arguments passed on the command line
script = argv[0]

# joins multiple strings into one to create search_term
search_term = ' '.join(sys.argv[1:])

# test is variable is set
try:
    search_term
except NameError:
    print 'Please enter search term'
else: 
    browser = webdriver.Firefox()
    browser.get('https://www.wikipedia.org')
    browser.find_element_by_id('searchInput').send_keys(search_term)
    # content = driver.find_element_by_css_selector('p.content')
    browser.find_element_by_css_selector('button.pure-button').click()

Once you have this you need to crease a bash alias to be able to access it easily. I created the following:

alias wiki='python /Users/palumbo/code/tools/search_wikipedia.py'

Now anytime I want to look up something in Wikipedia, I just need to type wiki <search term> into my terminal and it brings up a new Firefox window with the wikipedia page for that term.

JavaScript Hack: Prevent outputs from stacking in an HTML element by Joseph Palumbo

I'll often write a JavaScript function that populates an empty HTML element with some kind of output, either text or HTML. That's easy enough. The problem is that when/if function runs a second time, the second output is appended to the original output. 

I never had a good way to "reset" the target DIV when the function runs a second or third time. So I'm stuck with a long list of output stacked on each other. 

Last night I found a way to use an if statement to check if the target element already has something in it, and if it does, empty it out. 

Here it is. 

Screen Shot 2017-12-02 at 12.18.15 PM.jpg

The function in question outputs an HTML unordered list to a div with the id 'adv'. When the function runs, this logic checks the length of the HTML in the div and if it is greater than 0, it will empty it out. 

Definitely not saying this is the best way to handle a problem like this, but so far it has worked very nice for me so I thought I'd share.