Esempio n. 1
0
    def entity(self, uri, project=None, raw=False, article=None, **kwargs):
        logger.info(
            f'entity={uri} project={project} raw={raw} article={article}')
        refresh = str(kwargs.pop('refresh', 'false')).lower() in ('', 'true')

        cache_key = f'{uri}-{project}'
        entity = self.cache.get(cache_key) if not refresh and not raw else None
        if entity:
            entity['fromCache'] = True
            return entity

        secondary = None
        if uri.startswith('http://kg.jstor.org/'):
            primary = self._entity_from_wikibase(uri)
            primary['id'] = f'jstor:{primary["id"]}'
            if primary and 'Wikidata entity ID' in primary.get('claims', {}):
                secondary = self._entity_from_wikibase(primary['claims'].pop(
                    'Wikidata entity ID')[0]['value']['url'])
        elif uri.startswith('http://www.wikidata.org/'):
            primary = self._entity_from_wikibase(uri)
            primary['id'] = f'wd:{primary["id"]}'
        else:
            uri = uri if uri.endswith('.json') else f'{uri}.json'
            primary = self._entity_from_url(uri)
            wd_id = None
            statements = []
            for stmt in primary.get('statements', []):
                if stmt['claim']['property'] == 'Wikidata entity ID':
                    wd_id = stmt['claim']['value']
                else:
                    statements.append(stmt)
            if wd_id:
                primary['statements'] = stmt
                secondary = self._entity_from_wikibase(
                    f'http://www.wikidata.org/entity/{wd_id}')

        if secondary:  # merge primary and secondary
            entity = {'id': primary['id']}
            for fld in ('labels', 'descriptions', 'aliases', 'claims'):
                entity[fld] = {
                    **secondary.get(fld, {}),
                    **primary.get(fld, {})
                }
        else:
            entity = primary

        if not raw:
            self._add_summary_text(entity, project, article, **kwargs)

            entity = self._add_id_labels(
                entity, get_fingerprints(self._find_ids(entity)))

            self.cache[cache_key] = entity

        entity['fromCache'] = False

        return entity
Esempio n. 2
0
def fingerprints():
    site, acct, repo, ref, path, qargs = _context()
    if request.method == 'OPTIONS':
        return ('', 204, cors_headers)
    else:
        logger.info(f'fingerprints: qargs={qargs}')
        if 'qids' in qargs:
            qids = set()
            for qid in qargs['qids'].split(','):
                # ensure qids are namespaced
                ns, qid = qid.split(':') if ':' in qid else ('wd', qid)
                qids.add(f'{ns.strip()}:{qid.strip()}')
        fingerprints = get_fingerprints(qids, qargs.get('language', 'en'))
        return fingerprints, 200, cors_headers
Esempio n. 3
0
def fingerprints():
    kwargs = dict([(k, request.args.get(k)) for k in request.args])
    _set_logging_level(kwargs)
    if request.method == 'OPTIONS':
        return ('', 204, cors_headers)
    else:
        logger.info(f'fingerprints: kwargs={kwargs}')
        if 'qids' in kwargs:
            qids = set()
            for qid in kwargs['qids'].split(','):
                # ensure qids are namespaced
                ns, qid = qid.split(':') if ':' in qid else ('wd', qid)
                qids.add(f'{ns.strip()}:{qid.strip()}')
        fingerprints = get_fingerprints(qids, kwargs.get('language', 'en'))
        return (fingerprints, 200, cors_headers)