def find_items(keywords): # example use: # keyword should be a list of terms # DPLA.find_items(keywords=['term1', 'term2']) api = Bibs() qry = 'api_key->%s:q->%s' % ( DPLA.API_KEY, ' OR '.join(keywords) ) #qry from unicode string to regular string qry = qry.encode("utf8", "ignore") logger.debug('dpla query: %s' % qry) # TODO: restrict to image only, or at least things with preview image start = time.time() results = api.search(qry, 'dplav2', 'items') # TODO: error handling... logger.info('dpla query completed in %.2f sec' % (time.time() - start)) items = [] for doc in results['docs']: src_res = doc['sourceResource'] # for now, just skip items without an image url if not doc.get('object', None): continue i = DisplayItem( title=src_res.get('title', None), format=src_res.get('type', None), source=doc['provider'].get('name', None), # collection or provider here? src_rec['collection']['title'] # NOTE: collection apparently not set for all items thumbnail=doc.get('object', None), # according to dpla docs, should be url preview for item # docs reference a field for object mimetype, not seeing in results # url on provider's website with context url=doc.get('isShownAt', None) ) if 'date' in src_res: i.date = src_res['date'].get('displayDate', None) if 'spatial' in src_res and src_res['spatial']: # sometimes a list but not always if isinstance(src_res['spatial'], list): space = src_res['spatial'][0] else: space = src_res['spatial'] # country? state? coords? i.location = space.get('name', None) # Add the aggregator for reference i.aggregator = DPLA.name items.append(i) return items
def test_coins_citation_info(self): # minimal record item = DisplayItem(title='Hippo', url='http://some.url/to/a/hippo/pic') info = item.coins_citation_info self.assert_('rfr_id' in info, 'referrer id should be set in COinS info') self.assert_('rft_val_fmt' in info, 'format is specified in COinS info') self.assertEqual(item.title, info['rft.title']) self.assertEqual(item.url, info['rft.identifier']) for key in ['rft.date', 'rft.place', 'rft.source', 'rft.format']: self.assert_(key not in info, 'unavailable data should not be set in COinS info') # add all fields to simulate a complete record item.date = '1887' item.format = 'Image' item.source = 'Smithsonian' item.location = 'USA' info = item.coins_citation_info self.assertEqual(item.date, info['rft.date']) self.assertEqual(item.format, info['rft.format']) self.assertEqual(item.source, info['rft.source']) self.assertEqual(item.location, info['rft.place'])
def find_items(keywords): # example use: # keyword should be a list of terms # DPLA.find_items(keywords=['term1', 'term2']) api = Bibs() qry = 'api_key->%s:q->%s' % (DPLA.API_KEY, ' OR '.join(keywords)) #qry from unicode string to regular string qry = qry.encode("utf8", "ignore") logger.debug('dpla query: %s' % qry) # TODO: restrict to image only, or at least things with preview image start = time.time() results = api.search(qry, 'dplav2', 'items') # TODO: error handling... logger.info('dpla query completed in %.2f sec' % (time.time() - start)) items = [] for doc in results['docs']: src_res = doc['sourceResource'] # for now, just skip items without an image url if not doc.get('object', None): continue # url on DPLA site item_url = '%sitem/%s' % (url, doc.get('id')) i = DisplayItem( title=src_res.get('title', None), format=src_res.get('type', None), source=doc['provider'].get('name', None), # collection or provider here? src_rec['collection']['title'] # NOTE: collection apparently not set for all items thumbnail=doc.get('object', None), # according to dpla docs, should be url preview for item # docs reference a field for object mimetype, not seeing in results # url on DPLA site url=item_url) if 'date' in src_res: i.date = src_res['date'].get('displayDate', None) if 'spatial' in src_res and src_res['spatial']: # sometimes a list but not always if isinstance(src_res['spatial'], list): space = src_res['spatial'][0] else: space = src_res['spatial'] # country? state? coords? i.location = space.get('name', None) # Add the aggregator for reference i.aggregator = DPLA.name items.append(i) return items