traopia commited on
Commit
aec563a
·
1 Parent(s): 92bc0bd
Files changed (1) hide show
  1. src/wikibase_helpers.py +86 -1
src/wikibase_helpers.py CHANGED
@@ -3,6 +3,7 @@ import logging
3
  from wikibaseintegrator import WikibaseIntegrator, datatypes, wbi_helpers
4
  from wikibaseintegrator.wbi_config import config
5
  from wikibaseintegrator.wbi_exceptions import MWApiError
 
6
 
7
  wikibase_api_url = 'https://fashionwiki.wikibase.cloud/w/api.php'
8
  config = {
@@ -42,6 +43,69 @@ def get_property_id_by_label(property_label, api_url):
42
  else:
43
  logging.error(f"Failed to search for property by label in the target Wikibase. HTTP Status Code: {response.status_code}")
44
  return None
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
45
 
46
 
47
  wikibase_properties_id = {"instance of": get_property_id_by_label("instance of", wikibase_api_url),
@@ -83,4 +147,25 @@ wikibase_properties_id = {"instance of": get_property_id_by_label("instance of",
83
  "date of fashion collection": get_property_id_by_label("date of fashion collection", wikibase_api_url),
84
  "fashion show category": get_property_id_by_label("fashion show category", wikibase_api_url),
85
  "fashion house X fashion collection": get_property_id_by_label("fashion house X fashion collection", wikibase_api_url),
86
- "designer of collection": get_property_id_by_label("designer of collection", wikibase_api_url)}
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
3
  from wikibaseintegrator import WikibaseIntegrator, datatypes, wbi_helpers
4
  from wikibaseintegrator.wbi_config import config
5
  from wikibaseintegrator.wbi_exceptions import MWApiError
6
+ from wikibaseintegrator.wbi_helpers import mediawiki_api_call_helper, SearchError
7
 
8
  wikibase_api_url = 'https://fashionwiki.wikibase.cloud/w/api.php'
9
  config = {
 
43
  else:
44
  logging.error(f"Failed to search for property by label in the target Wikibase. HTTP Status Code: {response.status_code}")
45
  return None
46
+
47
+
48
+ def get_entity_id_by_label(search_string,wiki, dict_result=False) -> list:
49
+ """
50
+ Performs a search for entities in the Wikibase instance using labels and aliases.
51
+ You can have more information on the parameters in the MediaWiki API help (https://www.wikidata.org/w/api.php?action=help&modules=wbsearchentities)
52
+
53
+ :param search_string: A string which should be searched for in the Wikibase instance (labels and aliases)
54
+ :param wiki: The wiki to search in. It can be "wikidata" or "wikibase"
55
+ :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.
56
+ :return: A list of dictionaries or strings with the search results
57
+ """
58
+ if wiki == "wikidata":
59
+ login = login_wikidata
60
+ mediawiki_api_url = wikidata_api_url
61
+ elif wiki == "wikibase":
62
+ login = login_wikibase
63
+ mediawiki_api_url = wikibase_api_url
64
+
65
+ language = "en"
66
+ strict_language = False
67
+
68
+ params = {
69
+ 'action': 'wbsearchentities',
70
+ 'search': search_string,
71
+ 'language': language,
72
+ 'type': "item",
73
+ 'limit': 50,
74
+ 'format': 'json',
75
+ }
76
+
77
+ if strict_language:
78
+ params.update({'strict_language': ''})
79
+
80
+ cont_count = 0
81
+ results = []
82
+
83
+ while True:
84
+ params.update({'continue': cont_count})
85
+ search_results = mediawiki_api_call_helper(data=params, login = login, mediawiki_api_url=mediawiki_api_url, user_agent = config['USER_AGENT'])
86
+ if search_results['success'] != 1:
87
+ raise SearchError('Wikibase API wbsearchentities failed')
88
+
89
+ for i in search_results['search']:
90
+ if dict_result:
91
+ description = i['description'] if 'description' in i else None
92
+ aliases = i['aliases'] if 'aliases' in i else None
93
+ results.append({
94
+ 'id': i['id'],
95
+ 'label': i['label'],
96
+ 'match': i['match'],
97
+ 'description': description,
98
+ 'aliases': aliases
99
+ })
100
+ else:
101
+ results.append(i['id'])
102
+
103
+ if 'search-continue' not in search_results:
104
+ break
105
+ cont_count = search_results['search-continue']
106
+ if cont_count >= 50:
107
+ break
108
+ return results
109
 
110
 
111
  wikibase_properties_id = {"instance of": get_property_id_by_label("instance of", wikibase_api_url),
 
147
  "date of fashion collection": get_property_id_by_label("date of fashion collection", wikibase_api_url),
148
  "fashion show category": get_property_id_by_label("fashion show category", wikibase_api_url),
149
  "fashion house X fashion collection": get_property_id_by_label("fashion house X fashion collection", wikibase_api_url),
150
+ "designer of collection": get_property_id_by_label("designer of collection", wikibase_api_url)}
151
+
152
+
153
+ classes_wikibase = {"fashion designer": get_entity_id_by_label("fashion designer", "wikibase")[0],
154
+ "fashion house": get_entity_id_by_label("fashion house", "wikibase")[0],
155
+ "business": get_entity_id_by_label("business", "wikibase")[0],
156
+ "academic institution": get_entity_id_by_label("academic institution", "wikibase")[0],
157
+ "geographic location": get_entity_id_by_label("geographic location", "wikibase")[0],
158
+ "fashion award": get_entity_id_by_label("fashion award","wikibase")[0],
159
+ "gender":get_entity_id_by_label("gender","wikibase")[0] ,
160
+ "occupation": get_entity_id_by_label("occupation","wikibase")[0],
161
+ "human": get_entity_id_by_label("human","wikibase")[0],
162
+ "organization": get_entity_id_by_label("organization","wikibase")[0],
163
+ "brand": get_entity_id_by_label("brand","wikibase")[0],
164
+ "lifestyle brand": get_entity_id_by_label("lifestyle brand","wikibase")[0],
165
+ "privately held company": get_entity_id_by_label("privately held company","wikibase")[0],
166
+ "fashion award": get_entity_id_by_label("fashion award", "wikibase")[0],
167
+ "fashion season": get_entity_id_by_label("fashion season", "wikibase")[0],
168
+ "fashion show category": get_entity_id_by_label("fashion show category", "wikibase")[0],
169
+ "fashion season collection": get_entity_id_by_label("fashion season collection", "wikibase")[0],
170
+ "fashion journalist": get_entity_id_by_label("fashion journalist", "wikibase")[0],
171
+ }