Beispiel #1
0
 def set_target(self, snak, value):
     if value in ('somevalue', 'novalue'):
         snak.setSnakType(value)
         return True
     if snak.type == 'wikibase-item':
         snak.setTarget(pywikibot.ItemPage(self.repo, value))
         return True
     elif snak.type == 'wikibase-property':
         snak.setTarget(pywikibot.PropertyPage(self.repo, value))
         return True
     elif snak.type == 'quantity':
         match = self.quantityR.fullmatch(value)
         if match:
             amount, error, unit = match.groups()
         else:
             match = self.quantity_oldR.fullmatch(value)
             if match:
                 amount, lower, upper, unit = match.groups()
                 error = upper, lower  # it *is* the other way around
         if match:
             if unit:
                 unit = pywikibot.ItemPage(self.repo, 'Q' + unit)
             quantity = WbQuantity(amount, unit, error, site=self.repo)
             snak.setTarget(quantity)
             return True
     elif snak.type == 'time':
         iso, _, prec = value.rpartition('/')
         if iso:
             time = WbTime.fromTimestr(iso,
                                       precision=int(prec),
                                       site=self.repo)
             snak.setTarget(time)
             return True
     elif snak.type in ('string', 'external-id', 'url', 'math'):
         if value.startswith('"') and value.endswith('"'):
             snak.setTarget(value[1:-1])
             return True
     elif snak.type == 'commonsMedia':
         if value.startswith('"') and value.endswith('"'):
             repo = self.repo.image_repository()
             snak.setTarget(pywikibot.FilePage(repo, value[1:-1]))
             return True
     #elif snak.type in ('geo-shape', 'tabular-data'):
     elif snak.type == 'monolingualtext':
         lang, _, text = value.partition(':')
         if text and text.startswith('"') and text.endswith('"'):
             monotext = WbMonolingualText(text[1:-1], lang)
             snak.setTarget(monotext)
             return True
     elif snak.type == 'globe-coordinate':
         match = self.globeR.fullmatch(value)
         if match:
             coord = Coordinate(*map(float, match.groups()), site=self.repo)
             snak.setTarget(coord)
             return True
     return False
Beispiel #2
0
 def set_target(self, snak, value):
     if value in ('somevalue', 'novalue'):
         snak.setSnakType(value)
         return True
     if snak.type == 'wikibase-item':
         snak.setTarget(pywikibot.ItemPage(self.repo, value))
         return True
     elif snak.type == 'wikibase-property':
         snak.setTarget(pywikibot.PropertyPage(self.repo, value))
         return True
     elif snak.type == 'quantity':
         match = self.quantityR.fullmatch(value)
         if match:
             amount, error, unit = match.groups()
         else:
             match = self.quantity_oldR.fullmatch(value)
             if match:
                 amount, lower, upper, unit = match.groups()
                 error = upper, lower  # it *is* the other way around
         if match:
             if unit:
                 unit = pywikibot.ItemPage(self.repo, 'Q' + unit)
             quantity = WbQuantity(amount, unit, error, site=self.repo)
             snak.setTarget(quantity)
             return True
     elif snak.type == 'time':
         iso, _, prec = value.rpartition('/')
         if iso:
             time = WbTime.fromTimestr(
                 iso, precision=int(prec), site=self.repo)
             snak.setTarget(time)
             return True
     elif snak.type in ('string', 'external-id', 'url', 'math'):
         if value.startswith('"') and value.endswith('"'):
             snak.setTarget(value[1:-1])
             return True
     elif snak.type == 'commonsMedia':
         if value.startswith('"') and value.endswith('"'):
             repo = self.repo.image_repository()
             snak.setTarget(pywikibot.FilePage(repo, value[1:-1]))
             return True
     #elif snak.type in ('geo-shape', 'tabular-data'):
     elif snak.type == 'monolingualtext':
         lang, _, text = value.partition(':')
         if text and text.startswith('"') and text.endswith('"'):
             monotext = WbMonolingualText(text[1:-1], lang)
             snak.setTarget(monotext)
             return True
     elif snak.type == 'globe-coordinate':
         match = self.globeR.fullmatch(value)
         if match:
             coord = Coordinate(*map(float, match.groups()), site=self.repo)
             snak.setTarget(coord)
             return True
     return False
def string_to_wddate(isotimestamp: str) -> WbTime:
    """
    Create a wikidata compatible wikibase date from an ISO 8601 timestamp
    """
    date = WbTime.fromTimestr(isotimestamp,
                              calendarmodel=Settings.calendarmodel)
    date.hour = 0
    date.minute = 0
    date.second = 0
    date.precision = WbTime.PRECISION["day"]
    return date
def try_get_year_from_property_timestamp(entity_dict: Dict, property_id: str,
                                         oab_type: str) -> int:
    """Function for extracting the year from an inception timestamp

    Args:
        entity_dict: JSON response from wikidata
        property_id: Wikidata property id e. g. 'P18' stands for the property image
        oab_type: openArtBrowser type e. g. 'artwork', 'motif' important for logging

    Returns:
        Year from timestamp
    """
    timestr = entity_dict[CLAIMS][property_id][0][MAINSNAK][DATAVALUE][VALUE][
        TIME]
    return WbTime.fromTimestr(timestr).year
Beispiel #5
0
    def _set_target(self, snak, value):
        if value in ('somevalue', 'novalue'):
            snak.setSnakType(value)
            return True

        def invalid_report():
            pywikibot.warning('Invalid value "{}" for {} datatype'.format(
                value, snak.type))

        if snak.type in self.entity_types:
            target = self.parse_entity(value)
            if target is None:
                pywikibot.warning('"LAST" magic word used without "CREATE"')
            else:
                snak.setTarget(target)
                return True
        elif snak.type == 'quantity':
            match = self.quantity_errR.fullmatch(value)
            if match:
                amount, error, unit = match.groups()
            else:
                match = self.quantity_boundsR.fullmatch(value)
                if match:
                    groups = list(match.groups())
                    unit = groups.pop()
                    amount, lower, upper = map(Decimal, groups)
                    if lower > upper:
                        error = amount - lower, upper - amount
                    else:
                        error = upper - amount, amount - lower
            if match:
                if unit:
                    unit = pywikibot.ItemPage(self.repo, 'Q' + unit)
                quantity = WbQuantity(amount, unit, error, site=self.repo)
                snak.setTarget(quantity)
                return True
            else:
                invalid_report()
        elif snak.type == 'time':
            iso, _, prec = value.rpartition('/')
            if iso:
                time = WbTime.fromTimestr(iso,
                                          precision=int(prec),
                                          site=self.repo)
                snak.setTarget(time)
                return True
            else:
                invalid_report()
        elif snak.type in ('string', 'external-id', 'url', 'math'):
            literal = self.valid_text_literal(value)
            if literal:
                snak.setTarget(literal)
                return True
            else:
                invalid_report()
        elif snak.type == 'commonsMedia':
            literal = self.valid_text_literal(value)
            if literal:
                image_repo = self.repo.image_repository()
                snak.setTarget(pywikibot.FilePage(image_repo, literal))
                return True
            else:
                invalid_report()
        # todo: elif snak.type in ('geo-shape', 'tabular-data'):
        elif snak.type == 'monolingualtext':
            lang, _, text = value.partition(':')
            literal = self.valid_text_literal(text)
            if literal:
                monotext = WbMonolingualText(literal, lang)
                snak.setTarget(monotext)
                return True
            else:
                invalid_report()
        elif snak.type == 'globe-coordinate':
            match = self.globeR.fullmatch(value)
            if match:
                coord = Coordinate(
                    *map(float, match.groups()),
                    precision=1e-4,  # hardcoded as in claimit.py
                    site=self.repo)
                snak.setTarget(coord)
                return True
            else:
                invalid_report()
        else:
            pywikibot.warning('"{}" datatype is not supported yet'.format(
                snak.type))

        return False
def try_get_significant_events(
        result: Dict,
        oab_type: Optional[str] = SIGNIFICANT_EVENT) -> List[Dict]:
    """Maps the wikidata response for significant events to a list of dicts which is appended to an object

    Args:
        result: wikidata response
        oab_type: OpenArtBrowser type. Defaults to SIGNIFICANT_EVENT.

    Returns:
        List of JSON objects which represent significant events
    """
    significant_events = []
    qid = result[ID]
    for event in result[CLAIMS][
            PROPERTY_NAME_TO_PROPERTY_ID[SIGNIFICANT_EVENT]]:
        event_dict = {LABEL[SINGULAR]: event[MAINSNAK][DATAVALUE][VALUE][ID]}
        for qualifiers in event[QUALIFIERS].values():
            datatype = qualifiers[0][DATATYPE]
            property_id = qualifiers[0][PROPERTY]
            # Get property name from dict, if the name is not in the dict ignore it and take the id
            property = PROPERTY_ID_TO_PROPERTY_NAME.get(
                property_id, property_id)
            if datatype == TIME:
                event_dict.update({
                    property:
                    WbTime.fromTimestr(
                        qualifiers[0][DATAVALUE][VALUE][TIME]).year
                })
            elif datatype == WIKIBASE_ITEM:
                event_dict.update({
                    property:
                    list(
                        map(
                            lambda qualifier: qualifier[DATAVALUE][VALUE][ID],
                            qualifiers,
                        ))
                })
            elif datatype == QUANTITY:
                event_dict.update(
                    {property: float(qualifiers[0][DATAVALUE][VALUE][AMOUNT])})
                event_dict.update({
                    f"{property}_{UNIT}":
                    qualifiers[0][DATAVALUE][VALUE].get(UNIT, "").replace(
                        WIKIDATA_ENTITY_URL, "")
                })
            elif datatype in [STRING, URL]:
                event_dict.update({property: qualifiers[0][DATAVALUE][VALUE]})
            elif datatype == MONOLINGUALTEXT:
                event_dict.update(
                    {property: qualifiers[0][DATAVALUE][VALUE][TEXT]})
            elif datatype == COMMONS_MEDIA:
                logger.error(
                    f"commonsMedia type not supported in significant events on item {qid}"
                )
            else:
                logger.error(f"Unknown datatype: {datatype} on item {qid}")
        event_dict.update({TYPE: SIGNIFICANT_EVENT})
        significant_events.append(event_dict)

    return significant_events