Commits

Ian Huston  committed 4e16d2f

Add in beginning of query function

  • Participants
  • Parent commits bd02ba5

Comments (0)

Files changed (1)

File pyinspire/pyinspire.py

 Released under the modified BSD license.
 '''
 import sys
-import urllib2
+import urllib
 from bs4 import BeautifulSoup
 import feedparser
 import time
 import optparse
 import logging
 
-APIURL = "http://inspirehep.net/rss?ln=en&p="
-log = None
-
+APIURL = "http://inspirehep.net/search?" 
+logging.basicConfig()
+log = logging.getLogger("pyinspire")
+ 
 def time_to_datetime(timetuple):
     return datetime.datetime.fromtimestamp(time.mktime(timetuple))
 
         InspireClient.lasttime = time.time()
         return result
 
+def query_inspire(search="", bibtex=False, citations=False):
+    """Query the INSPIRE HEP database and return the response.
+
+    Parameters
+    ----------
+    search : string
+             search string to use in query
+
+    bibtex : boolean
+             if True output is in bibtex format (citation info in comments)
+
+    citations : boolean
+                if True output information about citations
+    """
+    inspireoptions = dict(action_search="Search",
+                          rg=100, #number of results to return in one page
+                          of="hb", #brief format by default 
+                          ln="en", #language
+                          p="" # search string
+                          )
+    if bibtex:
+        inspireoptions["of"] = "hx"
+    inspireoptions["p"] = search
+
+    url = APIURL + urllib.urlencode(inspireoptions)
+    log.debug("Query URL is %s", str(url))
+    
+    try:
+        f = urllib.urlopen(url)
+        data = f.read()
+    except IOError, e:
+        log.error("Error retrieving results: %s", str(e))
+        raise
+    soup = BeautifulSoup(data)
+
+    return soup.prettify()
 
 def main(argv=None):
     """ Main method to deal with command line arguments.
                   help="log lots of debugging information")
             
     (options, args) = parser.parse_args(args=argv[1:])
-
-    logging.basicConfig(level=options.loglevel)
-    log = logging.getLogger()
+    
+    log.setLevel(options.loglevel)
     log.debug("pyinspire called with the following options:\n %s", str(options)) 
 
     try:
-        result = query_inspire(**options)
+        result = query_inspire(options.search, options.bibtex, options.citations)
         print(result)
     except Exception, e:
         log.error("Error during retrieval of results: %s", str(e))