pubQuery App

A simple cli (and eventually GUI, as well) interface to search PubMed using the Google search engine (or search engine of choice)

This app also provides a simple example of a self-editing file


pqVars.py
Define variables for use in app

In [14]:
#! python3
# pqVars.py
# functions to import for pubQuery.py 

## CONSTANTS
urls = {"Google": "https://www.google.com/search?q=site%3Ancbi.nlm.nih.gov+", "Google Scholar": "https://scholar.google.com/scholar?q=site:ncbi.nlm.nih.gov+", "DuckDuckGo": "https://duckduckgo.com", "Yahoo": "https://yahoo.com"}



## MUTABLE VARIABLES
searchEngine = 'Google'

## create file within jupyterNB to prevent "file not found" error
with open("pqVars.py","w") as fh:
    fh.write("""
#! python3
# pqVars.py
# functions to import for pubQuery.py 

## CONSTANTS
urls = {"Google": "https://www.google.com/search?q=site%3Ancbi.nlm.nih.gov+", "Google Scholar": "https://scholar.google.com/scholar?q=site:ncbi.nlm.nih.gov+", "DuckDuckGo": "https://duckduckgo.com", "Yahoo": "https://yahoo.com"}

                

## MUTABLE VARIABLES
searchEngine = 'Google'
""")


pqFunc.py
Define functions for use in app

In [15]:
#! python3
# pqFuncs.py
# functions to import for pubQuery.py 

## FUNCTIONS
def change_search_engine():
    validInp = False

    while validInp == False:
        site = input("Please choose your Search Engine: \n(1) Google \n(2) Google Scholar \n(3) DuckDuckGo \n(4) Yahoo \nWhich site would you like to search?     ")
    
        if site== "1" or site.upper() == "GOOGLE":
            search_engine = "Google"
            validInp = True
        elif site == "2":
            search_engine = "Google Scholar"
            validInp = True
        elif site == "3":
            print("Option still under construction...please restart...")
        elif site == "4":
            print("Option still under construction...please restart...")
        else:
            print("Invalid entry...please restart...")
    return search_engine



def update_var(filename, var_name, var):
    #var_name = str(var)
    i = 0
    new_lines = []
    with open(filename,"r") as fh:
        lines = fh.readlines()
        for line in lines:
            i += 1
            print("CURRENT LINE ({1}):  {0}".format(line, i))
            if var_name in line.strip():
                print("Overwriting line {} in temporary storage...".format(i))
                updated_line = "{} = '{}'".format(var_name,var)
                new_lines.append(updated_line)
                print("Line {1} is now: {0}".format(updated_line, i))
            else:
                new_lines.append(line)
    with open(filename, "w") as fd:
        fd.seek(0)
        fd.truncate()
        for l in new_lines:
            fd.write(l)


Main run script
NOTE: import of pqVars and pqFuncs has been commented out for Jupyter purposes; uncomment for use as separate files or combine all variables and functions into pubQuery.py

In [17]:
#! python3
# pubQuery.py
# uses google (or search engine of choice) to search within pubmed instead of pubmed search bar

import webbrowser as w
#import random as r
#from pqVars import *
#from pqFuncs import *

    
"""   
## TODO:
    # add GUI input field using tkinter
# importing tkinter 
from tkinter import * 
from tkinter import ttk 
from tkinter.messagebox import askyesno 
  
# creating root 
root = Tk() 
  
# specifying geometry 
root.geometry('200x100') 
  
# This is used to take input from user 
# and show it in Entry Widget. 
# Whatever data that we get from keyboard 
# will be treated as string. 
input_text = StringVar() 
  
entry1 = ttk.Entry(root, textvariable = input_text, justify = CENTER) 
  
# focus_force is used to take focus 
# as soon as application starts 
entry1.focus_force() 
entry1.pack(side = TOP, ipadx = 30, ipady = 6) 
  
save = ttk.Button(root, text = 'Save', command = lambda : askyesno( 
                                'Confirm', 'Do you want to save?')) 
save.pack(side = TOP, pady = 10) 
  
root.mainloop()
"""
"________________________________________"

"""
## TODO:
    # open text/csv file
    with open("file_name") as fhand:
        # do something?
## TODO:
    # gather url's of top x# of citations
    # search for all permutations of 
    stPerm  = r.combo(term)     # ** find or write function
    stPerm = combo(term)
    print("Gathering citations for keyword combination: '{}'".format(stPerm))
    
    
## TODO:
    # filter out duplicate url's
    print("Checking for duplicates...")
## TODO:
    # write to file
    print("Saving results to file '{}'...".format(fhand)
    
## TODO >> finish fn
def combo(search_term):
    words = search_term.split()
    # mix order of words
    
    newST = newOrder.join(" ")
    return newST
    
    
    
## NEXT STEP: gather pdf's when possible
    try:
    except: 
    
## NEXT STEP: 
    # Gather text of abstract along with formatted title and author citation 
    # create visual way to present abstracts with citations (1 page per citation/abstract? list of citations/abstracts?)
"""


print("_"*60)
print("Search engine is currently set to: {}\nTo change search engine, enter 'X' or just hit 'Enter'".format(searchEngine))
print("_"*60)

term = input("Search term:    ")

if searchEngine == "":
    seSelect = change_search_engine()
    update_var("pqVars.py","searchEngine", seSelect)
elif term == "" or term.upper() == "X":
    seSelect = change_search_engine()
    update_var("pqVars.py","searchEngine", seSelect)
else:
    w.open(urls[searchEngine]+term)
____________________________________________________________
Search engine is currently set to: Google
To change search engine, enter 'X' or just hit 'Enter'
____________________________________________________________
Option still under construction...please restart...
CURRENT LINE (1):  

CURRENT LINE (2):  #! python3

CURRENT LINE (3):  # pqVars.py

CURRENT LINE (4):  # functions to import for pubQuery.py 

CURRENT LINE (5):  

CURRENT LINE (6):  ## CONSTANTS

CURRENT LINE (7):  urls = {"Google": "https://www.google.com/search?q=site%3Ancbi.nlm.nih.gov+", "Google Scholar": "https://scholar.google.com/scholar?q=site:ncbi.nlm.nih.gov+", "DuckDuckGo": "https://duckduckgo.com", "Yahoo": "https://yahoo.com"}

CURRENT LINE (8):  

CURRENT LINE (9):  

CURRENT LINE (10):  

CURRENT LINE (11):  ## MUTABLE VARIABLES

CURRENT LINE (12):  searchEngine = 'Google Scholar'
Overwriting line 12 in temporary storage...
Line 12 is now: searchEngine = 'Google Scholar'
In [ ]: