3
3
4
4
from aiohttp import ClientSession
5
5
6
- from .exceptions import XIVAPIBadRequest , XIVAPIForbidden , XIVAPINotFound , XIVAPIServiceUnavailable , XIVAPIInvalidLanguage , XIVAPIError , XIVAPIInvalidIndex , XIVAPIInvalidColumns
6
+ from .exceptions import XIVAPIBadRequest , XIVAPIForbidden , XIVAPINotFound , XIVAPIServiceUnavailable , \
7
+ XIVAPIInvalidLanguage , XIVAPIError , XIVAPIInvalidIndex , XIVAPIInvalidColumns , XIVAPIInvalidAlgo
7
8
from .decorators import timed
8
9
from .models import Filter , Sort
9
10
@@ -27,12 +28,21 @@ def __init__(self, api_key: str, session: Optional[ClientSession] = None) -> Non
27
28
self .api_key = api_key
28
29
self ._session = session
29
30
31
+ self .base_url = "https://xivapi.com"
32
+ self .languages = ["en" , "fr" , "de" , "ja" ]
33
+ self .string_algos = [
34
+ "custom" , "wildcard" , "wildcard_plus" , "fuzzy" , "term" , "prefix" , "match" , "match_phrase" ,
35
+ "match_phrase_prefix" , "multi_match" , "query_string"
36
+ ]
37
+
38
+
30
39
@property
31
40
def session (self ) -> ClientSession :
32
41
if self ._session is None or self ._session .closed :
33
42
self ._session = ClientSession ()
34
43
return self ._session
35
44
45
+
36
46
@timed
37
47
async def character_search (self , world , forename , surname , page = 1 ):
38
48
"""|coro|
@@ -248,7 +258,7 @@ async def pvpteam_by_id(self, lodestone_id):
248
258
249
259
250
260
@timed
251
- async def index_search (self , name , indexes = (), columns = (), filters : List [Filter ]= (), sort : Sort = None , page = 1 , language = "en" ):
261
+ async def index_search (self , name , indexes = (), columns = (), filters : List [Filter ]= (), sort : Sort = None , page = 1 , language = "en" , string_algo = "match" ):
252
262
"""|coro|
253
263
Search for data from on specific indexes.
254
264
Parameters
@@ -271,6 +281,10 @@ async def index_search(self, name, indexes=(), columns=(), filters: List[Filter]
271
281
Optional[language: str]
272
282
The two character length language code that indicates the language to return the response in. Defaults to English (en).
273
283
Valid values are "en", "fr", "de" & "ja"
284
+ Optional[string_algo: str]
285
+ The search algorithm to use for string matching (default = "match")
286
+ Valid values are "custom", "wildcard", "wildcard_plus", "fuzzy", "term", "prefix", "match", "match_phrase",
287
+ "match_phrase_prefix", "multi_match", "query_string"
274
288
"""
275
289
276
290
if len (indexes ) == 0 :
@@ -282,14 +296,17 @@ async def index_search(self, name, indexes=(), columns=(), filters: List[Filter]
282
296
if len (columns ) == 0 :
283
297
raise XIVAPIInvalidColumns ("Please specify at least one column to return in the resulting data." )
284
298
299
+ if string_algo not in self .string_algos :
300
+ raise XIVAPIInvalidAlgo (f'"{ string_algo } " is not a supported string_algo for XIVAPI' )
301
+
285
302
body = {
286
303
"indexes" : "," .join (list (set (indexes ))),
287
304
"columns" : "ID" ,
288
- "body" : {
305
+ "body" : {
289
306
"query" : {
290
307
"bool" : {
291
308
"should" : [{
292
- "match" : {
309
+ string_algo : {
293
310
"NameCombined_en" : {
294
311
"query" : name ,
295
312
"fuzziness" : "AUTO" ,
@@ -298,7 +315,7 @@ async def index_search(self, name, indexes=(), columns=(), filters: List[Filter]
298
315
}
299
316
}
300
317
}, {
301
- "match" : {
318
+ string_algo : {
302
319
"NameCombined_de" : {
303
320
"query" : name ,
304
321
"fuzziness" : "AUTO" ,
@@ -307,7 +324,7 @@ async def index_search(self, name, indexes=(), columns=(), filters: List[Filter]
307
324
}
308
325
}
309
326
}, {
310
- "match" : {
327
+ string_algo : {
311
328
"NameCombined_fr" : {
312
329
"query" : name ,
313
330
"fuzziness" : "AUTO" ,
@@ -316,7 +333,7 @@ async def index_search(self, name, indexes=(), columns=(), filters: List[Filter]
316
333
}
317
334
}
318
335
}, {
319
- "match" : {
336
+ string_algo : {
320
337
"NameCombined_ja" : {
321
338
"query" : name ,
322
339
"fuzziness" : "AUTO" ,
@@ -391,7 +408,6 @@ async def index_by_id(self, index, content_id: int, columns=(), language="en"):
391
408
async with self .session .get (url , params = params ) as response :
392
409
return await self .process_response (response )
393
410
394
-
395
411
@timed
396
412
async def lore_search (self , query , language = "en" ):
397
413
"""|coro|
0 commit comments