File size: 10,171 Bytes
00c29b3
 
 
 
 
aec563a
a4877a2
 
00c29b3
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
aec563a
 
 
 
 
 
 
 
 
 
 
 
a4877a2
 
aec563a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
00c29b3
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
aec563a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
import requests
import logging
from wikibaseintegrator import WikibaseIntegrator, datatypes,  wbi_helpers
from wikibaseintegrator.wbi_config import config
from wikibaseintegrator.wbi_exceptions import MWApiError
from wikibaseintegrator.wbi_helpers import mediawiki_api_call_helper, SearchError
from wikibaseintegrator import WikibaseIntegrator, wbi_login
login_wikibase = wbi_login.Login(user="Traopia", password="diqfiz-wodnI4-jafwax", mediawiki_api_url="https://fashionwiki.wikibase.cloud/w/api.php")

wikibase_api_url = 'https://fashionwiki.wikibase.cloud/w/api.php'
config = {
"SPARQL_ENDPOINT_URL": "https://fashionwiki.wikibase.cloud/query/sparql",
'USER_AGENT':  'YourBotName/1.0 (https://yourwebsite.org/bot-info)',
'WIKIBASE_URL': wikibase_api_url,
}




# List of valid language codes (can be expanded)
VALID_LANGUAGE_CODES = ['en']

def get_property_id_by_label(property_label, api_url):
    """
    Resolve the property label to its corresponding property ID from Wikibase.
    
    Args:
        property_label (str): The label of the property to search.
        api_url (str): The API URL of the target Wikibase or Wikidata.
    
    Returns:
        str: The property ID if found, otherwise None.
    """
    url = f'{api_url}/w/api.php?action=wbsearchentities&search={property_label}&language=en&type=property&format=json'
    response = requests.get(url)
    
    if response.status_code == 200:
        search_results = response.json()
        if 'search' in search_results and search_results['search']:
            # Return the first matching property ID
            return search_results['search'][0]['id']
        else:
            logging.info(f"No property found for label: {property_label}")
            return None
    else:
        logging.error(f"Failed to search for property by label in the target Wikibase. HTTP Status Code: {response.status_code}")
        return None
    

def get_entity_id_by_label(search_string,wiki, dict_result=False) -> list:
    """
    Performs a search for entities in the Wikibase instance using labels and aliases.
    You can have more information on the parameters in the MediaWiki API help (https://www.wikidata.org/w/api.php?action=help&modules=wbsearchentities)

    :param search_string: A string which should be searched for in the Wikibase instance (labels and aliases)
    :param wiki: The wiki to search in. It can be "wikidata" or "wikibase"
    :param dict_result: If True, the result will be a list of dictionaries with the keys 'id', 'label', 'match', 'description' and 'aliases'. If False, the result will be a list of strings with the entity IDs.
    :return: A list of dictionaries or strings with the search results
    """

    if wiki == "wikibase":
        login = login_wikibase
        mediawiki_api_url = wikibase_api_url

    language = "en"
    strict_language = False

    params = {
        'action': 'wbsearchentities',
        'search': search_string,
        'language': language,
        'type': "item",
        'limit': 50,
        'format': 'json',
    }

    if strict_language:
        params.update({'strict_language': ''})

    cont_count = 0
    results = []

    while True:
        params.update({'continue': cont_count})
        search_results = mediawiki_api_call_helper(data=params, login = login, mediawiki_api_url=mediawiki_api_url, user_agent = config['USER_AGENT'])
        if search_results['success'] != 1:
            raise SearchError('Wikibase API wbsearchentities failed')

        for i in search_results['search']:
            if dict_result:
                description = i['description'] if 'description' in i else None
                aliases = i['aliases'] if 'aliases' in i else None
                results.append({
                    'id': i['id'],
                    'label': i['label'],
                    'match': i['match'],
                    'description': description,
                    'aliases': aliases
                })
            else:
                results.append(i['id'])

        if 'search-continue' not in search_results:
            break
        cont_count = search_results['search-continue']
        if cont_count >= 50:
            break
    return results


wikibase_properties_id = {"instance of": get_property_id_by_label("instance of", wikibase_api_url),
                    "reference URL": get_property_id_by_label("reference URL", wikibase_api_url),
                    "start time": get_property_id_by_label("start time", wikibase_api_url),
                    "end time": get_property_id_by_label("end time", wikibase_api_url),
                    "occupation title": get_property_id_by_label("occupation title", wikibase_api_url),
                    "educated at": get_property_id_by_label("educated at", wikibase_api_url),
                    "employer": get_property_id_by_label("employer", wikibase_api_url),
                    "work location": get_property_id_by_label("work location", wikibase_api_url),
                    "award received": get_property_id_by_label("award received", wikibase_api_url),
                    "point in time": get_property_id_by_label("point in time", wikibase_api_url),
                    "exact match": get_property_id_by_label("exact match", wikibase_api_url),
                    "date of birth": get_property_id_by_label("date of birth", wikibase_api_url),
                    "place of birth": get_property_id_by_label("place of birth", wikibase_api_url),
                    "date of death": get_property_id_by_label("date of death", wikibase_api_url),
                    "country of citizenship": get_property_id_by_label("country of citizenship", wikibase_api_url),
                    "occupation": get_property_id_by_label("occupation", wikibase_api_url),
                    "sex or gender": get_property_id_by_label("sex or gender", wikibase_api_url),
                    "official website": get_property_id_by_label("official website", wikibase_api_url),
                    "perfumes": get_property_id_by_label("perfumes", wikibase_api_url),
                    "who wears it": get_property_id_by_label("who wears it", wikibase_api_url),
                    "inception": get_property_id_by_label("inception", wikibase_api_url),
                    "headquarters location": get_property_id_by_label("headquarters location", wikibase_api_url),
                    "parent organization": get_property_id_by_label("parent organization", wikibase_api_url),
                    "founded by": get_property_id_by_label("founded by", wikibase_api_url),
                    "owned by": get_property_id_by_label("owned by", wikibase_api_url),
                    "industry": get_property_id_by_label("industry", wikibase_api_url),
                    "country": get_property_id_by_label("country", wikibase_api_url),
                    "total revenue": get_property_id_by_label("total revenue", wikibase_api_url),
                    "designer employed": get_property_id_by_label("designer employed", wikibase_api_url),
                    "country of origin": get_property_id_by_label("country of origin", wikibase_api_url),
                    "fashion collection": get_property_id_by_label("fashion collection", wikibase_api_url),
                    "fashion season": get_property_id_by_label("fashion season", wikibase_api_url),
                    "fashion show location": get_property_id_by_label("fashion show location", wikibase_api_url),
                    "description of fashion collection": get_property_id_by_label("description of fashion collection", wikibase_api_url),
                    "image of fashion collection": get_property_id_by_label("image of fashion collection", wikibase_api_url),
                    "editor of fashion collection description": get_property_id_by_label("editor of fashion collection description", wikibase_api_url),
                    "date of fashion collection": get_property_id_by_label("date of fashion collection", wikibase_api_url),
                    "fashion show category": get_property_id_by_label("fashion show category", wikibase_api_url),
                    "fashion house X fashion collection": get_property_id_by_label("fashion house X fashion collection", wikibase_api_url),
                    "designer of collection": get_property_id_by_label("designer of collection", wikibase_api_url)}


classes_wikibase = {"fashion designer": get_entity_id_by_label("fashion designer", "wikibase")[0],
                        "fashion house": get_entity_id_by_label("fashion house", "wikibase")[0],
                        "business": get_entity_id_by_label("business", "wikibase")[0],
                        "academic institution": get_entity_id_by_label("academic institution", "wikibase")[0],
                        "geographic location": get_entity_id_by_label("geographic location", "wikibase")[0],
                        "fashion award": get_entity_id_by_label("fashion award","wikibase")[0],
                        "gender":get_entity_id_by_label("gender","wikibase")[0] , 
                        "occupation": get_entity_id_by_label("occupation","wikibase")[0],
                        "human": get_entity_id_by_label("human","wikibase")[0],
                        "organization": get_entity_id_by_label("organization","wikibase")[0],
                        "brand": get_entity_id_by_label("brand","wikibase")[0],
                        "lifestyle brand": get_entity_id_by_label("lifestyle brand","wikibase")[0],
                        "privately held company": get_entity_id_by_label("privately held company","wikibase")[0],
                        "fashion award": get_entity_id_by_label("fashion award", "wikibase")[0],
                        "fashion season": get_entity_id_by_label("fashion season", "wikibase")[0],
                        "fashion show category": get_entity_id_by_label("fashion show category", "wikibase")[0],
                        "fashion season collection": get_entity_id_by_label("fashion season collection", "wikibase")[0],
                        "fashion journalist": get_entity_id_by_label("fashion journalist", "wikibase")[0],
                        }