forked from xivapi/xivapi-py
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclient.py
532 lines (441 loc) · 18.5 KB
/
client.py
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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
import logging
from typing import List
from .exceptions import XIVAPIBadRequest, XIVAPIForbidden, XIVAPINotFound, XIVAPIServiceUnavailable, XIVAPIInvalidLanguage, XIVAPIError, XIVAPIInvalidIndex, XIVAPIInvalidColumns
from .decorators import timed
from .models import Filter, Sort
__log__ = logging.getLogger(__name__)
class XIVAPIClient:
"""
Asynchronous client for accessing XIVAPI's endpoints.
Parameters
------------
session: aiohttp.ClientSession()
The aiohttp session used with which to make http requests
api_key: str
The API key used for identifying your application with XIVAPI.com.
"""
def __init__(self, session, api_key):
self.session = session
self.api_key = api_key
self.base_url = "https://xivapi.com"
self.languages = ["en", "fr", "de", "ja"]
@timed
async def character_search(self, world, forename, surname, page=1):
"""|coro|
Search for character data directly from the Lodestone.
Parameters
------------
world: str
The world that the character is attributed to.
forename: str
The character's forename.
surname: str
The character's surname.
Optional[page: int]
The page of results to return. Defaults to 1.
"""
url = f'{self.base_url}/character/search?name={forename}%20{surname}&server={world}&page={page}&private_key={self.api_key}'
async with self.session.get(url) as response:
return await self.process_response(response)
@timed
async def character_by_id(self, lodestone_id: int, extended=False, include_achievements=False, include_classjobs=False, include_freecompany=False, include_freecompany_members=False, include_pvpteam=False, language="en"):
"""|coro|
Request character data from XIVAPI.com
Please see XIVAPI documentation for more information about character sync state https://xivapi.com/docs/Character#character
Parameters
------------
lodestone_id: int
The character's Lodestone ID.
"""
params = {
"private_key": self.api_key,
"language": language
}
if language.lower() not in self.languages:
raise XIVAPIInvalidLanguage(f'"{language}" is not a valid language code for XIVAPI.')
if extended is True:
params["extended"] = 1
data = []
if include_achievements is True:
data.append("AC")
if include_classjobs is True:
data.append("CJ")
if include_freecompany is True:
data.append("FC")
if include_freecompany_members is True:
data.append("FCM")
if include_pvpteam is True:
data.append("PVP")
if len(data) > 0:
params["data"] = ",".join(data)
url = f'{self.base_url}/character/{lodestone_id}'
async with self.session.get(url, params=params) as response:
return await self.process_response(response)
@timed
async def character_verify(self, lodestone_id: int, token):
"""|coro|
Request character data from XIVAPI.com
Parameters
------------
lodestone_id: int
The character's Lodestone ID.
token: str
The string token on a character's Lodestone profile to test against
"""
params = {
"private_key": self.api_key,
"token": token
}
url = f'{self.base_url}/character/{lodestone_id}/verification'
async with self.session.get(url, params=params) as response:
return await self.process_response(response)
@timed
async def character_update(self, lodestone_id: int):
"""|coro|
Request a character to be updated as soon as possible
Parameters
------------
lodestone_id: int
The character's Lodestone ID.
"""
url = f'{self.base_url}/character/{lodestone_id}/update?private_key={self.api_key}'
async with self.session.get(url) as response:
return await self.process_response(response)
@timed
async def freecompany_search(self, world, name, page=1):
"""|coro|
Search for Free Company data directly from the Lodestone.
Parameters
------------
world: str
The world that the Free Company is attributed to.
name: str
The Free Company's name.
Optional[page: int]
The page of results to return. Defaults to 1.
"""
url = f'{self.base_url}/freecompany/search?name={name}&server={world}&page={page}&private_key={self.api_key}'
async with self.session.get(url) as response:
return await self.process_response(response)
@timed
async def freecompany_by_id(self, lodestone_id: int, extended=False, include_freecompany_members=False):
"""|coro|
Request Free Company data from XIVAPI.com by Lodestone ID
Please see XIVAPI documentation for more information about Free Company info at https://xivapi.com/docs/Free-Company#profile
Parameters
------------
lodestone_id: int
The Free Company's Lodestone ID.
"""
params = {
"private_key": self.api_key
}
if extended is True:
params["extended"] = 1
data = []
if include_freecompany_members is True:
data.append("FCM")
if len(data) > 0:
params["data"] = ",".join(data)
url = f'{self.base_url}/freecompany/{lodestone_id}'
async with self.session.get(url, params=params) as response:
return await self.process_response(response)
@timed
async def linkshell_search(self, world, name, page=1):
"""|coro|
Search for Linkshell data directly from the Lodestone.
Parameters
------------
world: str
The world that the Linkshell is attributed to.
name: str
The Linkshell's name.
Optional[page: int]
The page of results to return. Defaults to 1.
"""
url = f'{self.base_url}/linkshell/search?name={name}&server={world}&page={page}&private_key={self.api_key}'
async with self.session.get(url) as response:
return await self.process_response(response)
@timed
async def linkshell_by_id(self, lodestone_id: int):
"""|coro|
Request Linkshell data from XIVAPI.com by Lodestone ID
Parameters
------------
lodestone_id: int
The Linkshell's Lodestone ID.
"""
url = f'{self.base_url}/linkshell/{lodestone_id}?private_key={self.api_key}'
async with self.session.get(url) as response:
return await self.process_response(response)
@timed
async def pvpteam_search(self, world, name, page=1):
"""|coro|
Search for PvPTeam data directly from the Lodestone.
Parameters
------------
world: str
The world that the PvPTeam is attributed to.
name: str
The PvPTeam's name.
Optional[page: int]
The page of results to return. Defaults to 1.
"""
url = f'{self.base_url}/pvpteam/search?name={name}&server={world}&page={page}&private_key={self.api_key}'
async with self.session.get(url) as response:
return await self.process_response(response)
@timed
async def pvpteam_by_id(self, lodestone_id):
"""|coro|
Request PvPTeam data from XIVAPI.com by Lodestone ID
Parameters
------------
lodestone_id: str
The PvPTeam's Lodestone ID.
"""
url = f'{self.base_url}/pvpteam/{lodestone_id}?private_key={self.api_key}'
async with self.session.get(url) as response:
return await self.process_response(response)
@timed
async def index_search(self, name, indexes=(), columns=(), filters: List[Filter]=(), sort: Sort=None, page=1, language="en"):
"""|coro|
Search for data from on specific indexes.
Parameters
------------
name: str
The name of the item to retrieve the recipe data for.
indexes: list
A named list of indexes to search XIVAPI. At least one must be specified.
e.g. ["Recipe", "Item"]
Optional[columns: list]
A named list of columns to return in the response. ID, Name, Icon & ItemDescription will be returned by default.
e.g. ["ID", "Name", "Icon"]
Optional[filters: list]
A list of type Filter. Filter must be initialised with Field, Comparison (e.g. lt, lte, gt, gte) and value.
e.g. filters = [ Filter("LevelItem", "gte", 100) ]
Optional[sort: Sort]
The name of the column to sort on.
Optional[page: int]
The page of results to return. Defaults to 1.
Optional[language: str]
The two character length language code that indicates the language to return the response in. Defaults to English (en).
Valid values are "en", "fr", "de" & "ja"
"""
if len(indexes) == 0:
raise XIVAPIInvalidIndex("Please specify at least one index to search for, e.g. [\"Recipe\"]")
if language.lower() not in self.languages:
raise XIVAPIInvalidLanguage(f'"{language}" is not a valid language code for XIVAPI.')
if len(columns) == 0:
raise XIVAPIInvalidColumns("Please specify at least one column to return in the resulting data.")
body = {
"indexes": ",".join(list(set(indexes))),
"columns": "ID",
"body" : {
"query": {
"bool": {
"should": [{
"match": {
"NameCombined_en": {
"query": name,
"fuzziness": "AUTO",
"prefix_length": 1,
"max_expansions": 50
}
}
}, {
"match": {
"NameCombined_de": {
"query": name,
"fuzziness": "AUTO",
"prefix_length": 1,
"max_expansions": 50
}
}
}, {
"match": {
"NameCombined_fr": {
"query": name,
"fuzziness": "AUTO",
"prefix_length": 1,
"max_expansions": 50
}
}
}, {
"match": {
"NameCombined_ja": {
"query": name,
"fuzziness": "AUTO",
"prefix_length": 1,
"max_expansions": 50
}
}
}]
}
}
}
}
if len(columns) > 0:
body["columns"] = ",".join(list(set(columns)))
if len(filters) > 0:
filts = []
for f in filters:
filts.append({
"range": {
f.Field: {
f.Comparison: f.Value
}
}
})
body["body"]["query"]["bool"]["filter"] = filts
if sort:
body["body"]["sort"] = [{
sort.Field: "asc" if sort.Ascending else "desc"
}]
url = f'{self.base_url}/search?language={language}&private_key={self.api_key}'
async with self.session.post(url, json=body) as response:
return await self.process_response(response)
@timed
async def index_by_id(self, index, content_id: int, columns=(), language="en"):
"""|coro|
Request data from a given index by ID.
Parameters
------------
index: str
The index to which the content is attributed.
content_id: int
The ID of the content
Optional[columns: list]
A named list of columns to return in the response. ID, Name, Icon & ItemDescription will be returned by default.
e.g. ["ID", "Name", "Icon"]
Optional[language: str]
The two character length language code that indicates the language to return the response in. Defaults to English (en).
Valid values are "en", "fr", "de" & "ja"
"""
if index == "":
raise XIVAPIInvalidIndex("Please specify an index to search on, e.g. \"Item\"")
if len(columns) == 0:
raise XIVAPIInvalidColumns("Please specify at least one column to return in the resulting data.")
params = {
"private_key": self.api_key,
"language": language
}
if len(columns) > 0:
params["columns"] = ",".join(list(set(columns)))
url = f'{self.base_url}/{index}/{content_id}'
async with self.session.get(url, params=params) as response:
return await self.process_response(response)
@timed
async def lore_search(self, query, language="en"):
"""|coro|
Search cutscene subtitles, quest dialog, item, achievement, mount & minion descriptions and more for any text that matches query.
Parameters
------------
query: str
The text to search game content for.
Optional[language: str]
The two character length language code that indicates the language to return the response in. Defaults to English (en).
Valid values are "en", "fr", "de" & "ja"
"""
params = {
"private_key": self.api_key,
"language": language,
"string": query
}
url = f'{self.base_url}/lore'
async with self.session.get(url, params=params) as response:
return await self.process_response(response)
@timed
async def lodestone_all(self):
"""|coro|
Request all categories of Lodestone posts. This function is recommended because it returns a cached (every 15 minutes) collection of
information and will return much quicker.
"""
url = f'{self.base_url}/lodestone?private_key={self.api_key}'
async with self.session.get(url) as response:
return await self.process_response(response)
@timed
async def lodestone_news(self):
"""|coro|
Request posts under the topics Lodestone category.
"""
url = f'{self.base_url}/lodestone/news?private_key={self.api_key}'
async with self.session.get(url) as response:
return await self.process_response(response)
@timed
async def lodestone_notices(self):
"""|coro|
Request posts under the notices Lodestone category.
"""
url = f'{self.base_url}/lodestone/notices?private_key={self.api_key}'
async with self.session.get(url) as response:
return await self.process_response(response)
@timed
async def lodestone_maintenance(self):
"""|coro|
Request posts under the maintenance Lodestone category.
"""
url = f'{self.base_url}/lodestone/maintenance?private_key={self.api_key}'
async with self.session.get(url) as response:
return await self.process_response(response)
@timed
async def lodestone_updates(self):
"""|coro|
Request posts under the updates Lodestone category.
"""
url = f'{self.base_url}/lodestone/updates?private_key={self.api_key}'
async with self.session.get(url) as response:
return await self.process_response(response)
@timed
async def lodestone_worldstatus(self):
"""|coro|
Request world status post from the Lodestone.
"""
url = f'{self.base_url}/lodestone/worldstatus?private_key={self.api_key}'
async with self.session.get(url) as response:
return await self.process_response(response)
@timed
async def lodestone_devblog(self):
"""|coro|
Request posts under the developer blog Lodestone category.
"""
url = f'{self.base_url}/lodestone/devblog?private_key={self.api_key}'
async with self.session.get(url) as response:
return await self.process_response(response)
@timed
async def lodestone_devposts(self):
"""|coro|
Request developer posrs from the official FFXIV forums.
"""
url = f'{self.base_url}/lodestone/devposts?private_key={self.api_key}'
async with self.session.get(url) as response:
return await self.process_response(response)
@timed
async def lodestone_deepdungeon(self):
"""|coro|
Request Deep Dungeon post from the Lodestone.
"""
url = f'{self.base_url}/lodestone/deepdungeon?private_key={self.api_key}'
async with self.session.get(url) as response:
return await self.process_response(response)
@timed
async def lodestone_feasts(self):
"""|coro|
Request Feast post from the Lodestone.
"""
url = f'{self.base_url}/lodestone/feasts?private_key={self.api_key}'
async with self.session.get(url) as response:
return await self.process_response(response)
async def process_response(self, response):
__log__.info(f'{response.status} from {response.url}')
if response.status == 200:
return await response.json()
if response.status == 400:
raise XIVAPIBadRequest("Request was bad. Please check your parameters.")
if response.status == 401:
raise XIVAPIForbidden("Request was refused. Possibly due to an invalid API key.")
if response.status == 404:
raise XIVAPINotFound("Resource not found.")
if response.status == 500:
raise XIVAPIError("An internal server error has occured on XIVAPI.")
if response.status == 503:
raise XIVAPIServiceUnavailable("Service is unavailable. This could be because the Lodestone is under maintenance.")