Skip to content

Commit

Permalink
Merge pull request #6 from curtiscook/c/wildcard
Browse files Browse the repository at this point in the history
Allow multiple string algos
  • Loading branch information
Yandawl authored Jul 10, 2020
2 parents 011b4cc + b730453 commit 2b970fe
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 13 deletions.
7 changes: 4 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ import pyxivapi
from pyxivapi.models import Filter, Sort


async def fetch_example_results(session):
async def fetch_example_results():
client = pyxivapi.XIVAPIClient(api_key="your_key_here")

# Search Lodestone for a character
Expand Down Expand Up @@ -113,7 +113,8 @@ async def fetch_example_results(session):
name="Defiance",
indexes=["Action", "PvPAction", "CraftAction"],
columns=["ID", "Name", "Icon", "Description", "ClassJobCategory.Name", "ClassJobLevel", "ActionCategory.Name"],
filters=filters
filters=filters,
string_algo="match"
)

# Search ingame data for matches against a given query. Includes item, minion, mount & achievement descriptions, quest dialog & more.
Expand Down Expand Up @@ -141,7 +142,7 @@ async def fetch_example_results(session):
# Get all categories of posts from the Lodestone (cached evert 15 minutes)
lodestone = await client.lodestone_all()

await session.close()
await client.session.close()


if __name__ == '__main__':
Expand Down
2 changes: 1 addition & 1 deletion pyxivapi/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
__author__ = 'Lethys'
__license__ = 'MIT'
__copyright__ = 'Copyright 2019 (c) Lethys'
__version__ = '0.1.2'
__version__ = '0.2.0'

from .client import XIVAPIClient
from .exceptions import *
32 changes: 24 additions & 8 deletions pyxivapi/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@

from aiohttp import ClientSession

from .exceptions import XIVAPIBadRequest, XIVAPIForbidden, XIVAPINotFound, XIVAPIServiceUnavailable, XIVAPIInvalidLanguage, XIVAPIError, XIVAPIInvalidIndex, XIVAPIInvalidColumns
from .exceptions import XIVAPIBadRequest, XIVAPIForbidden, XIVAPINotFound, XIVAPIServiceUnavailable, \
XIVAPIInvalidLanguage, XIVAPIError, XIVAPIInvalidIndex, XIVAPIInvalidColumns, XIVAPIInvalidAlgo
from .decorators import timed
from .models import Filter, Sort

Expand All @@ -27,12 +28,21 @@ def __init__(self, api_key: str, session: Optional[ClientSession] = None) -> Non
self.api_key = api_key
self._session = session

self.base_url = "https://xivapi.com"
self.languages = ["en", "fr", "de", "ja"]
self.string_algos = [
"custom", "wildcard", "wildcard_plus", "fuzzy", "term", "prefix", "match", "match_phrase",
"match_phrase_prefix", "multi_match", "query_string"
]


@property
def session(self) -> ClientSession:
if self._session is None or self._session.closed:
self._session = ClientSession()
return self._session


@timed
async def character_search(self, world, forename, surname, page=1):
"""|coro|
Expand Down Expand Up @@ -248,7 +258,7 @@ async def pvpteam_by_id(self, lodestone_id):


@timed
async def index_search(self, name, indexes=(), columns=(), filters: List[Filter]=(), sort: Sort=None, page=1, language="en"):
async def index_search(self, name, indexes=(), columns=(), filters: List[Filter]=(), sort: Sort=None, page=1, language="en", string_algo="match"):
"""|coro|
Search for data from on specific indexes.
Parameters
Expand All @@ -271,6 +281,10 @@ async def index_search(self, name, indexes=(), columns=(), filters: List[Filter]
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"
Optional[string_algo: str]
The search algorithm to use for string matching (default = "match")
Valid values are "custom", "wildcard", "wildcard_plus", "fuzzy", "term", "prefix", "match", "match_phrase",
"match_phrase_prefix", "multi_match", "query_string"
"""

if len(indexes) == 0:
Expand All @@ -282,14 +296,17 @@ async def index_search(self, name, indexes=(), columns=(), filters: List[Filter]
if len(columns) == 0:
raise XIVAPIInvalidColumns("Please specify at least one column to return in the resulting data.")

if string_algo not in self.string_algos:
raise XIVAPIInvalidAlgo(f'"{string_algo}" is not a supported string_algo for XIVAPI')

body = {
"indexes": ",".join(list(set(indexes))),
"columns": "ID",
"body" : {
"body": {
"query": {
"bool": {
"should": [{
"match": {
string_algo: {
"NameCombined_en": {
"query": name,
"fuzziness": "AUTO",
Expand All @@ -298,7 +315,7 @@ async def index_search(self, name, indexes=(), columns=(), filters: List[Filter]
}
}
}, {
"match": {
string_algo: {
"NameCombined_de": {
"query": name,
"fuzziness": "AUTO",
Expand All @@ -307,7 +324,7 @@ async def index_search(self, name, indexes=(), columns=(), filters: List[Filter]
}
}
}, {
"match": {
string_algo: {
"NameCombined_fr": {
"query": name,
"fuzziness": "AUTO",
Expand All @@ -316,7 +333,7 @@ async def index_search(self, name, indexes=(), columns=(), filters: List[Filter]
}
}
}, {
"match": {
string_algo: {
"NameCombined_ja": {
"query": name,
"fuzziness": "AUTO",
Expand Down Expand Up @@ -391,7 +408,6 @@ async def index_by_id(self, index, content_id: int, columns=(), language="en"):
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|
Expand Down
9 changes: 8 additions & 1 deletion pyxivapi/exceptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -72,4 +72,11 @@ class XIVAPIError(Exception):
"""
XIVAPI error
"""
pass
pass


class XIVAPIInvalidAlgo(Exception):
"""
Invalid String Algo
"""
pass

0 comments on commit 2b970fe

Please sign in to comment.