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
def search245(resultsList): """perform SRU title/author search -- for non-matched items (this needs to be integrated)""" lcabsHeld = [] lcabsNotHeld = [] for numCodes, item in enumerate(resultsList): if len(item)>0: #empty items happen and they don't help if len(item[6]) == 1: #title for matches (ugly code) shortTitle = item[6] else: try:# make a short title string to search because the full title prolly won't match shortTitleWords = item[6].split(' ')[0:2] #try 1, 2, or 3 words shortTitle = ' '.join(shortTitleWords) except IndexError: #some titles have only one word shortTitle = item[6].split(' ')[0] except TypeError: print('TypeError: {}').format(item) author = item[-1].split(' ')[0].strip(',') #author's last name query = '({} = "{}*") and ({}="{}*") and (srw.li="{}")'.format('srw.ti',shortTitle,'srw.au',author,LIBS) #Ex: sru.args['query'] = '(srw.no="122347155") and (srw.li="HDC")' print ('Title Search: {} / {} {}{}').format(numCodes, len(resultsList),query,'\r'), sys.stdout.flush() sru.args['query'] = query # set the query results = pymarc_extract(sru.get_response().data) # send the query if len(results) == 0: #Honnold has no holdings 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
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
def search(lCodes): """input: list of (ISBN) codes. Query WC SRU with them. Return enhanced lists of held and not held items""" lcabsHeld = [] lcabsNotHeld = [] numCodes = 0 for numCodes, lCode in enumerate(lCodes):#search HDC by ISBN first. Build lists of hits and misses print ('ISBN Search: {} / {} {}').format(numCodes, len(lCodes),'\r'),#print status sys.stdout.flush() query = '({}="{}") and (srw.li="{}")'.format(SRUELEM,lCode,LIBS) #Ex: sru.args['query'] = '(srw.no="122347155") and (srw.li="HDC")' sru.args['query'] = query # set the query results = pymarc_extract(sru.get_response().data) # send the query if len(results) == 0: #Honnold has no holdings, open the search to worldwide libraries libs='ALL' query = '({}="{}")'.format(SRUELEM,lCode) #no library code; search all libs sru.args['query'] = query # set the query results = pymarc_extract(sru.get_response().data) # send the query lcabsNotHeld.append(makeItemList(results,libs,lCode)) else: #Honnold has holdings libs = LIBS lcabsHeld.append(makeItemList(results,libs,lCode)) return lcabsHeld, lcabsNotHeld
def _get_records(self, q): records = {} query = self._build_query(q) subj = q['subj'] self.log("Executing query %s of %s '%s'" % (self.queries.index(q)+1, len(self.queries), subj)) responseData = self._sru_request(query) if responseData is not None: records.update(self._build_recs(pymarc_extract(responseData))) nextPosition = self._get_next_id(responseData) else: nextPosition = None while nextPosition is not None and len(records) < MAXIMUM_RECORDS: responseData = self._sru_request(query, nextPosition) if responseData is not None: records.update(self._build_recs(pymarc_extract(responseData))) nextPosition = self._get_next_id(responseData) else: nextPosition = None return records
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(')')