def search(query):
    """
    Search against Worldcat Search API
    """

    matches = []

    wskey = os.environ['WSKEY']
    s = SRURequest(wskey=wskey, query='srw.ti = "' + query + '"', maximumRecords='3')
    o = s.get_response()

    for r in pymarc_extract(o.data):
        title = ''
        control_num = 0
        for f in r.get_fields():
            if ( f.tag == '245' ):
                title = f.format_field()
                app.logger.info( title )
            if ( f.tag == '001' ):
                control_num = f.format_field()
                app.logger.info(control_num)

        matches.append({ "id": json.dumps(control_num), "name": json.dumps(title), "score": 100, "match": False, "type": [ {"id": "/entry/title", "name": "Entry Title"} ] })

    return matches
Beispiel #2
0
def get_related(oclcnum):
    bib = BibRequest(wskey=WSKEY, rec_num=oclcnum).get_response()
    record = pymarc_extract(bib.data)
    subjects = []
    if len(record) == 0:
        return 'record %s not found' % oclcnum
    else:
        # if record[0].author() is not None:
        #         subjects.append(record[0].author())
        for s in record[0].subjects():
            if s.indicator2 == '0':
                subjects.append('"%s"' % s.format_field())
            elif s.indicator2 == '7' and s.tag == '655':
                subjects.append('"%s"' % s['a'])
        # if len(subjects) > 5:
        #     subjects = subjects[0:5]
        #     print 'more than 5 subjects - truncating'
        #print subjects
        # 'not srw.no = oclcnum'
        if len(subjects) > 0:
            sru = SRURequest(wskey=WSKEY)
            sru.args['query'] = '(srw.kw = %s) not srw.no = "%s"' % \
                                    (" or srw.kw = ".join(subjects), oclcnum)
            # sru.args['servicelevel'] = 'full'
            sru.args['sortKeys'] = 'LibraryCount,,0 relevance'
            #print sru.args['query']
            results = pymarc_extract(sru.get_response().data)
            results = [r['001'].format_field() for r in results]
            out = []
            for r in results:
                c = CitationRequest(wskey=WSKEY, rec_num=r)
                out.append(c.get_response().data)
            return "".join(out)
        else:
            return None
Beispiel #3
0
def get_related(oclcnum):
    bib = BibRequest(wskey=WSKEY, rec_num=oclcnum).get_response()
    record = pymarc_extract(bib.data)
    subjects = []
    if len(record) == 0:
        return 'record %s not found' % oclcnum
    else:
        # if record[0].author() is not None:
        #         subjects.append(record[0].author())
        for s in record[0].subjects():
            if s.indicator2 == '0':
                subjects.append('"%s"' % s.format_field())
            elif s.indicator2 == '7' and s.tag == '655':
                subjects.append('"%s"' % s['a'])
        # if len(subjects) > 5:
        #     subjects = subjects[0:5]
        #     print 'more than 5 subjects - truncating'
        #print subjects
        # 'not srw.no = oclcnum'
        if len(subjects) > 0:
            sru = SRURequest(wskey=WSKEY)
            sru.args['query'] = '(srw.kw = %s) not srw.no = "%s"' % \
                                    (" or srw.kw = ".join(subjects), oclcnum)
            # sru.args['servicelevel'] = 'full'
            sru.args['sortKeys'] = 'LibraryCount,,0 relevance'
            #print sru.args['query']
            results = pymarc_extract(sru.get_response().data)
            results = [r['001'].format_field() for r in results]
            out = []
            for r in results:
                c = CitationRequest(wskey=WSKEY, rec_num=r)
                out.append(c.get_response().data)
            return "".join(out)
        else:
            return None
Beispiel #4
0
    def _sru_request(self, query, startNum = 1):
        # wait a random amount of time
        self._wait()

        req = SRURequest(wskey = self.apikey)
        req.args['query'] = query
        req.args['maximumRecords'] = 50
        if startNum > 1:
            req.args['startRecord'] = startNum
        try:
            return req.get_response().data
        except (urllib2.HTTPError, urllib2.URLError), errstr:
            self._stopped = True
            self.log("Error: %s" % errstr)
            return None
Beispiel #5
0
 def get(self):
   query = self.request.get('query', 'srw.ti = "IISG"')
   jsonp_callback = self.request.get('jsonp_callback')
   maximumRecords = self.request.get('maximumRecords', 30)
   wskey='You Need to stick your WS_KEY from OCLC in here'
   s = SRURequest(wskey=wskey, query=query, maximumRecords=maximumRecords)
   o = s.get_response()
   
   self.response.headers['Content-Type'] = 'application/json'
   buf = []
   for r in pymarc_extract(o.data):
       rr = {}
       for f in r.get_fields():
           rr[f.tag] = f.format_field()
       buf.append(rr)
   if jsonp_callback: self.response.out.write('%s(' % jsonp_callback)
   self.response.out.write(simplejson.dumps(buf))
   if jsonp_callback: self.response.out.write(')')
Beispiel #6
0
 def generate_srurequest(self, query):
     '''
     Generates the SRURequest used to query OCLC API.
     Most of the values are static, except for query.
     '''
     bib_rec = SRURequest(wskey=WSKEY,
                          sortKeys=sortkeys,
                          recordSchema=recordschema,
                          recordPacking=recordpacking,
                          servicelevel=servicelevel,
                          query=query,
                          startRecord=1,
                          maximumRecords=MAX_RECORDS,
                          frbrGrouping=frbrgrouping)
     return bib_rec
Beispiel #7
0
                libs='ALL'
                lcabsNotHeld.append(makeItemList(results,libs,item[1]))
            else: #Honnold holdings-ish
                libs = 'HDC-ish'
                lcabsHeld.append(makeItemList(results,libs,item[1]))
    return lcabsHeld, lcabsNotHeld
 
if __name__ == "__main__":
    config = getYAMLConfig(cfgFileName) #read in config values
    WSKEY = config['Auth']['WSKEY_SEARCH']
    LIBS = config['Config']['LIBS']
    SVCLVL = config['Config']['SVCLVL']
    SRUELEM = config['Config']['SRUELEM']
    MARCCODES = config['Config']['MARCCODES']
    lCodes=[]
    sru = SRURequest(wskey=WSKEY)     # Set up the worldcat SRU request object
    sru.args['servicelevel'] = SVCLVL
    sru.args['FRBRgrouping'] = FRBRgrouping
    sru.url = 'http://www.worldcat.org/webservices/catalog/search/worldcat/sru'
    fileIn = sys.argv[1]
    fileOut = sys.argv[2]    
    csvHdr = ['Library','ISBN']+MARCCODES
    with open(fileIn, 'r') as cabCodes , open(fileOut, 'w') as bibsOut:
        csvOut = csv.writer(bibsOut, quoting=csv.QUOTE_NONNUMERIC)# set up a csv file
        csvOut.writerow(csvHdr)
        bList = codesList(cabCodes) # get the codes from the file
        lCodes = bList.listed() #a de-duped list of the codes in the file
        matches, nonmatches = search(lCodes) # look them all up by ISBN, return worldcat bib data 
        for row in matches: # write the direct matches (on ISBN) to a csv file
            csvOut.writerow(row) 
        titlematches, nontitlematches = search245(nonmatches) #look for title/author matches