コード例 #1
0
def add_images_to_depicts_lookup(hits):
    qid_to_item = {hit['qid']: hit for hit in hits}
    all_qids = [hit['qid'] for hit in hits]
    entities = mediawiki.get_entities_with_cache(all_qids)

    for entity in entities:
        qid = entity['id']
        item = qid_to_item[qid]
        item.entity = entity
    database.session.commit()

    for hit in hits:
        item = qid_to_item[hit['qid']]
        if item.entity:
            image_filename = wikibase.first_datavalue(item.entity, 'P18')
            hit['image_filename'] = image_filename

    filenames = [
        hit['image_filename'] for hit in hits if hit.get('image_filename')
    ]
    filenames = filenames[:50]
    thumbwidth = 200
    detail = commons.image_detail(filenames, thumbwidth=thumbwidth)

    for hit in hits:
        filename = hit.get('image_filename')
        if not filename or filename not in detail:
            continue
        hit['image'] = detail[filename]
コード例 #2
0
def catalog_page():
    params = get_artwork_params()
    bindings = filter_artwork(params)
    page = utils.get_int_arg('page') or 1
    page_size = 45

    item_ids = set()
    for row in bindings:
        item_id = wdqs.row_id(row)
        item_ids.add(item_id)

    qids = [f'Q{item_id}' for item_id in sorted(item_ids)]

    items = [Item.query.get(item_id) for item_id in item_ids]

    entities = mediawiki.get_entities_with_cache(qids)

    items = []
    other_items = set()
    for entity in entities:
        other_items.update(build_other_set(entity))
        continue

        item = {
            'label': wikibase.get_entity_label(entity),
            'qid': entity['id'],
            'item_id': int(entity['id'][1:]),
            'image_filename': wikibase.first_datavalue(entity, 'P18'),
            'entity': entity,
        }
        items.append(item)

    other = get_labels(other_items)

    flat = '_'.join(f'{pid}={qid}' for pid, qid in params)
    thumbwidth = 400
    # FIXME cache_name can be too long for filesystem
    cache_name = f'{flat}_{page}_{page_size}_{thumbwidth}'
    detail = get_image_detail_with_cache(items,
                                         cache_name,
                                         thumbwidth=thumbwidth)

    for item in items:
        item['url'] = url_for('item_page', item_id=item['item_id'])
        item['image'] = detail[item['image_filename']]

    item_labels = get_labels(qid for pid, qid in params)
    title = ' / '.join(find_more_props[pid] + ': ' + item_labels[qid]
                       for pid, qid in params)

    return render_template('catalog.html',
                           labels=find_more_props,
                           items=items,
                           other=other,
                           title=title)