예제 #1
0
    def get(self, entity_ids, attributes=None, languages=None):
        """Get the data of one or multiple Wikibase entities

        :param entity_ids: Entity identifier(s) (e.g. ``"Q1"`` or ``["Q1", "Q2"]``)
        :type entity_ids: str or list(str)
        :param attributes: Names of the attributes to be fetched from each entity (e.g.
            ``"claims"``)
        :type attributes: list(str)
        :param languages: Languages to return the fetched data in (e.g. ``"en"``)
        :type languages: list(str)
        :return: Response
        :rtype: dict
        """
        if isinstance(entity_ids, str):
            ids_encoded = entity_ids
        else:
            ids_encoded = "|".join(entity_ids)
        params = {"action": "wbgetentities", "ids": ids_encoded}

        if languages is not None:
            for lang in languages:
                validate_value(lang, "language")
            params["languages"] = "|".join(attributes)

        if attributes is not None:
            for prop in attributes:
                validate_value(prop, "attribute")
            params["props"] = "|".join(attributes)

        return self.api.get(params)
예제 #2
0
    def replace_all(self, entity_id, aliases, language):
        """Replace all existing aliases with the specified one(s) for an entity

        :param entity_id: Entity identifier (e.g. ``"Q1"``)
        :type entity_id: str
        :param aliases: Aliases to add after deleting all existing ones
        :type aliases: str or list(str)
        :param language: Language of the description (e.g. ``"en"``)
        :type language: str
        :return: Response
        :rtype: dict
        """
        validate_value(language, "language")

        if isinstance(aliases, str):
            aliases_encoded = aliases
        else:
            aliases_encoded = "|".join(aliases)

        params = {
            "action": "wbsetaliases",
            "id": entity_id,
            "set": aliases_encoded,
            "language": language,
        }
        return self.api.post(params)
예제 #3
0
    def remove(self, entity_id, aliases, language):
        """Remove one or multiple aliases from the specified entity

        :param entity_id: Entity identifier (e.g. ``"Q1"``)
        :type entity_id: str
        :param aliases: Existing aliases to remove
        :type aliases: str or list(str)
        :param language: Language of the description (e.g. ``"en"``)
        :type language: str
        :return: Response
        :rtype: dict
        """
        validate_value(language, "language")

        if isinstance(aliases, str):
            aliases_encoded = aliases
        else:
            aliases_encoded = "|".join(aliases)

        params = {
            "action": "wbsetaliases",
            "id": entity_id,
            "remove": aliases_encoded,
            "language": language,
        }
        return self.api.post(params)
예제 #4
0
    def set(self, entity_id, label, language):
        """Set the label in the specified language for an entity

        :param entity_id: Entity identifier (e.g. ``"Q1"``)
        :type entity_id: str
        :param label: Value to set the label (site title) to (e.g. ``"Universe"``)
        :type label: str
        :param language: Language of the description (e.g. ``"en"``)
        :type language: str
        :return: Response
        :rtype: dict
        """
        validate_value(language, "language")
        params = {
            "action": "wbsetlabel",
            "id": entity_id,
            "language": language,
            "value": label
        }
        return self.api.post(params)
예제 #5
0
    def add(self, entity_type, content=None):
        """Create a new Wikibase entity

        :param entity_type: Type of entity to be created (e.g. ``"item"``)
        :type entity_type: str
        :param content: Content of the new entity
        :type content: dict
        :return: Response
        :rtype: dict
        """
        validate_value(entity_type, "entity")
        if content is None:
            content = {}
        content_str = json.dumps(content)
        params = {
            "action": "wbeditentity",
            "new": entity_type,
            "data": content_str
        }
        return self.api.post(params)
예제 #6
0
    def set(self, entity_id, description, language):
        """Set the title in the specified language for an entity

        :param entity_id: Entity identifier (e.g. ``"Q1"``)
        :type entity_id: str
        :param description: Value to set the description to (e.g. ``"third planet from the Sun in
            the Solar System"``)
        :type description: str
        :param language: Language of the description (e.g. ``"en"``)
        :type language: str
        :return: Response
        :rtype: dict
        """
        validate_value(language, "language")

        params = {
            "action": "wbsetdescription",
            "id": entity_id,
            "language": language,
            "value": description,
        }
        return self.api.post(params)
예제 #7
0
    def search(self,
               search_key,
               language,
               entity_type="item",
               limit=10,
               offset=0):
        """Search for entities based on their labels and aliases

        :param search_key: String for which Wikibase entities' labels and aliases are searched
        :type search_key: str
        :param language: Languages to search in (e.g. ``"en"``)
        :type language: str
        :param entity_type: Type of entities to search for. Default: "item"
        :type entity_type: str
        :param limit: Maximum number of results to return. Default: 10
        :type limit: int
        :param offset: Offset where to continue a search. Default: 0
        :type offset: int
        :return: Response
        :rtype: dict
        """
        validate_value(language, "language")
        if entity_type is not None:
            validate_value(entity_type, "entity")

        params = {
            "action": "wbsearchentities",
            "search": search_key,
            "language": language,
            "limit": str(limit),
        }

        if entity_type is not None:
            params["type"] = entity_type
        if offset is not None:
            params["offset"] = str(offset)

        return self.api.get(params)